怎么获得设备改变的消息(高手请进)
李V四 2003-04-08 04:47:09 在msdn上有以下方法:
我的问题是怎么获得class的GUID??
Registering for Device Notification
Applications can use the RegisterDeviceNotification function to register to receive notification messages from the system. The following example function demonstrates how to register for notification of events for the device interfaces which are members of the interface class whose GUID is passed to the function.
BOOL DoRegisterDeviceInterface(
GUID InterfaceClassGuid,
HDEVNOTIFY *hDevNotify
)
/*
Routine Description:
Registers for notification of changes in the device interfaces for
the specified interface class GUID.
Parameters:
InterfaceClassGuid - The interface class GUID for the device
interfaces.
hDevNotify - Receives the device notification handle. On failure,
this value is NULL.
Return Value:
If the function succeeds, the return value is TRUE.
If the function fails, the return value is FALSE.
*/
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
DWORD Err;
ZeroMemory( ¬ificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size =
sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
*hDevNotify = RegisterDeviceNotification( hWnd,
¬ificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE
);
if(!*hDevNotify)
{
Err = GetLastError();
printf( "RegisterDeviceNotification failed: %lx.\n", Err);
return FALSE;
}
return TRUE;
}
Then, in your main window procedure, check for messages announcing that a device has been attached, is about to be removed, or has been removed, as follows:
MyWindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
switch (nMsg)
{
case WM_DEVICECHANGE:
{
switch (wParam)
{
case DBT_DEVICEARRIVAL:
// Handle device arrival
break;
case DBT_DEVICEQUERYREMOVE:
// Handle device removal request
break;
case DBT_DEVICEREMOVECOMPLETE:
// Handle device removal
break;
}
}
.
.
.
}
}