首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetMessage不检索消息

GetMessage不检索消息
EN

Stack Overflow用户
提问于 2015-12-04 19:58:19
回答 1查看 1.7K关注 0票数 1

当我运行我的程序(下面的代码)并通过USB电缆插入硬盘驱动器时,WindowProcedure被调用为设备更改事件类型DBT_DEVICEARRIVALWM_DEVICECHANGE消息。

但是,GetMessage不返回。GetMessageGetMessage

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

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

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

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

注:我读过一些相关的文章和网页。This stackoverflow post似乎是有关联的。如果是这样的话,我如何知道消息队列中实际放置了哪些消息?

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2015-12-04 20:13:52

WM_DEVICECHANGE的文档说:

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

这意味着这不是一个排队的消息。它没有放在消息队列中。GetMessage不检索它。

相反,它将直接发送到窗口的窗口过程。消息被广播到顶级窗口,并发送到向RegisterDeviceNotification注册的窗口.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34096446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档