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 = {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. dww.mode = 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 communications, 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;
}