GetMessage不检索邮件

时间:2015-12-04 19:58:20

标签: winapi windows-messages message-pump

当我运行我的程序(下面的代码)并通过USB电缆插入硬盘时,WindowProcedure会针对设备更改事件类型WM_DEVICECHANGE调用DBT_DEVICEARRIVAL消息。

但是,GetMessage不会返回。 documentation for GetMessageGetMessage

  

从调用线程的消息队列中检索消息。

因此,听起来线程的消息队列中没有消息。

为什么我的调用线程的消息队列中没有消息?

如果我的调用线程的消息队列中没有消息,我的WindowProcedure函数是如何/为什么调用WM_DEVICECHANGE消息的设备更改事件类型DBT_DEVICEARRIVAL

注意:我已经阅读了一些相关的帖子和​​页面。 This stackoverflow post似乎可能是相关的。如果是这样,我如何知道消息队列中实际放置了什么消息?

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;
}

1 个答案:

答案 0 :(得分:1)

WM_DEVICECHANGE的文档说:

  

窗口通过其WindowProc函数接收此消息。

这意味着这不是排队的消息。它没有放在消息队列中。 .no-padding{padding: 0 !important;} 未检索到它。

而是将其直接发送到窗口的窗口过程。该消息将广播到顶级窗口,并发送到注册RegisterDeviceNotification的窗口。