Developing a Device Driver -> Device Driver Development Concepts -> Device Interfaces
The following code example shows a simple use of CreateMsgQueue, RequestDeviceNotifications, WaitForSingleObject, and StopDeviceNotifications.
#include <windows.h>
#include <msgqueue.h>
#include <pnp.h>
typedef union {
DEVDETAIL d;
char pad[sizeof(DEVDETAIL)+MAX_DEVCLASS_NAMELEN];
} MYDEV;
void EnumerateDevices (HANDLE h)
{
MYDEV detail;
DWORD flags;
DWORD size;
SetLastError(0);
while (ReadMsgQueue(h, &detail, sizeof(detail), &size, 1, &flags) == TRUE)
printf("Device notification: %S %s, f=0x%x\n", detail.d.szName, detail.d.fAttached ? "appeared" : "was removed", flags);
printf("Leaving Enumerate, error = %d\n", GetLastError());
}
main ()
{
GUID guid = {0}; // or any known and relevant device interface GUID
HANDLE hq, hn;
MSGQUEUEOPTIONS msgopts;
msgopts.dwFlags = MSGQUEUE_VARIABLESIZE | MSGQUEUE_MSGSIZE;
msgopts.dwMaxMessages = 0; //?
msgopts.cbMaxMessage = sizeof(MYDEV);
msgopts.cbMaxMsgQueue = 0; //?
msgopts.dwDesiredAccess = GENERIC_READ;
msgopts.dwShareMode = 0;
msgopts.dwCreationDisposition = CREATE_NEW;
hq = CreateMsgQueue(NULL, &msgopts);
printf("Created message queue, h = %08X\n", hq);
if (hq == 0) return 0;
hn = RequestDeviceNotifications(&guid, hq, TRUE);
printf("Registered for notifications, h = %08X\n", hn);
EnumerateDevices(hq);
printf("Completed initial notification pass.\n");
// do whatever
while (WaitForSingleObject(hq, 80000) == WAIT_OBJECT_0)
EnumerateDevices(hq);
printf("Commencing final enumeration\n");
EnumerateDevices(hq);
printf("Done.\n");
StopDeviceNotifications(hn);
CloseMsgQueue(hq);
return 0;
}
The following code example shows a simple use of CreateMsgQueue, RequestDeviceNotifications, WaitForSingleObject, and StopDeviceNotifications.
#include <windows.h>
#include <msgqueue.h>
#include <pnp.h>
typedef union {
DEVDETAIL d;
char pad[sizeof(DEVDETAIL)+MAX_DEVCLASS_NAMELEN];
} MYDEV;
void EnumerateDevices (HANDLE h)
{
MYDEV detail;
DWORD flags;
DWORD size;
SetLastError(0);
while (ReadMsgQueue(h, &detail, sizeof(detail), &size, 1, &flags) == TRUE)
printf("Device notification: %S %s, f=0x%x\n", detail.d.szName, detail.d.fAttached ? "appeared" : "was removed", flags);
printf("Leaving Enumerate, error = %d\n", GetLastError());
}
main ()
{
GUID guid = {0}; // or any known and relevant device interface GUID
HANDLE hq, hn;
MSGQUEUEOPTIONS msgopts;
msgopts.dwFlags = MSGQUEUE_VARIABLESIZE | MSGQUEUE_MSGSIZE;
msgopts.dwMaxMessages = 0; //?
msgopts.cbMaxMessage = sizeof(MYDEV);
msgopts.cbMaxMsgQueue = 0; //?
msgopts.dwDesiredAccess = GENERIC_READ;
msgopts.dwShareMode = 0;
msgopts.dwCreationDisposition = CREATE_NEW;
hq = CreateMsgQueue(NULL, &msgopts);
printf("Created message queue, h = %08X\n", hq);
if (hq == 0) return 0;
hn = RequestDeviceNotifications(&guid, hq, TRUE);
printf("Registered for notifications, h = %08X\n", hn);
EnumerateDevices(hq);
printf("Completed initial notification pass.\n");
// do whatever
while (WaitForSingleObject(hq, 80000) == WAIT_OBJECT_0)
EnumerateDevices(hq);
printf("Commencing final enumeration\n");
EnumerateDevices(hq);
printf("Done.\n");
StopDeviceNotifications(hn);
CloseMsgQueue(hq);
return 0;
}