プログラム (以下のコード) を実行し、USB ケーブル経由でハード ドライブを挿入すると、device-change event typeWindowProcedure
のメッセージに対して が呼び出されます。WM_DEVICECHANGE
DBT_DEVICEARRIVAL
ただしGetMessage
返さない。のドキュメントはGetMessage
言うGetMessage
呼び出しスレッドのメッセージ キューからメッセージを取得します。
したがって、スレッドのメッセージ キューにメッセージがないように聞こえます。
呼び出しスレッドのメッセージ キューにメッセージがないのはなぜですか?
呼び出しスレッドのメッセージ キューにメッセージがない場合、デバイス変更イベント タイプWindowProcedure
のメッセージに対して関数がどのように/なぜ呼び出されるのですか?WM_DEVICECHANGE
DBT_DEVICEARRIVAL
注: 関連する投稿やページをいくつか読みました。 このスタックオーバーフローの投稿は関連しているようです。その場合、どのメッセージが実際にメッセージ キューに配置されるかを知るにはどうすればよいですか?
namespace {
LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if( uMsg == WM_DEVICECHANGE )
{
switch(wParam)
{
case DBT_DEVICEARRIVAL:
{
PostQuitMessage('L');
break;
}
case DBT_DEVNODES_CHANGED:
{
break;
}
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
BOOL DoRegisterDeviceInterfaceToHwnd(IN GUID InterfaceClassGuid, IN HWND hWnd, OUT HDEVNOTIFY *hDeviceNotify)
{
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
*hDeviceNotify = RegisterDeviceNotification(
hWnd, // events recipient
&NotificationFilter, // type of device
DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES // type of recipient handle
);
if ( NULL == *hDeviceNotify )
{
//ErrorHandler(TEXT("RegisterDeviceNotification"));
return FALSE;
}
return TRUE;
}
int processWindowsMessages()
{
WNDCLASS windowClass = {};
windowClass.lpfnWndProc = WindowProcedure;
LPCSTR windowClassName = "DetecatAndMountMessageOnlyWindow";;
windowClass.lpszClassName = windowClassName;
if (!RegisterClass(&windowClass)) {
std::cout << "Failed to register window class" << std::endl;
return 1;
}
HWND messageWindow = CreateWindow (windowClassName, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0);
//HWND messageWindow = CreateWindow (windowClassName, 0, 0, 0, 0, 0, 0, (HWND) NULL, 0, 0, 0);
if (!messageWindow) {
std::cout << "Failed to create message-only window" << std::endl;
return 1;
}
static HDEVNOTIFY hDeviceNotify;
DoRegisterDeviceInterfaceToHwnd(GUID_DEVINTERFACE_VOLUME, messageWindow, &hDeviceNotify);
MSG msg;
BOOL bRet;
std::cout << "before loop" << std::endl;
while ( (bRet = GetMessage (&msg, 0, 0, 0)) != 0 )
{
std::cout << "inside loop" << std::endl;
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
std::cout << msg.wParam << std::endl;
return msg.wParam;
}
int main()
{
int result = processWindowsMessages();
return 0;
}