在控制台应用程序中处理空的windows消息队列
Struggling with an empty windows message queue in a console application
在下面的(可编译)示例中,我试图在普通控制台应用程序中侦听windows消息队列,以便接收有关USB设备连接/断开连接的通知。我从这里获取了示例代码:在C++非GUI应用程序中检测USB插入/删除
但是,while
-子句中对GetMessage
的调用永远不会返回,因为Windows显然不会向我的队列发送任何消息。我做错了什么?这与UIPI有关吗?
我不经常使用MFC/WinAPI,所以请详细说明你的答案。
#define ANSI
#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <winuser.h>
#include <Dbt.h>
#include <string>
#include <iostream>
#include <stdexcept>
#define HID_CLASSGUID {0x4d1e55b2, 0xf16f, 0x11cf,{ 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30}}
#define CLS_NAME "DUMMY_CLASS"
#define HWND_MESSAGE ((HWND)-3)
LRESULT message_handler(HWND__* hwnd, UINT uint, WPARAM wparam, LPARAM lparam)
{
switch (uint)
{
case WM_NCCREATE: // before window creation
return true;
break;
case WM_CREATE: // the actual creation of the window
{
// you can get your creation params here..like GUID..
LPCREATESTRUCT params = (LPCREATESTRUCT) lparam;
GUID InterfaceClassGuid = *((GUID*)params->lpCreateParams);
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;
HDEVNOTIFY dev_notify = RegisterDeviceNotification(hwnd, &NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
if(dev_notify == NULL)
{
throw std::runtime_error("Could not register for devicenotifications!");
}
break;
}
case WM_DEVICECHANGE:
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lparam;
PDEV_BROADCAST_DEVICEINTERFACE lpdbv = (PDEV_BROADCAST_DEVICEINTERFACE) lpdb;
std::string path;
if (lpdb->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
{
path = std::string(lpdbv->dbcc_name);
switch (wparam)
{
case DBT_DEVICEARRIVAL:
std::cout << "new device connected: " << path << "n";
break;
case DBT_DEVICEREMOVECOMPLETE:
std::cout << "device disconnected: " << path << "n";
break;
}
}
break;
}
}
return 0L;
}
int main(int argc, char* argv[])
{
HWND hWnd = NULL;
WNDCLASSEX wx;
ZeroMemory(&wx, sizeof(wx));
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = reinterpret_cast<WNDPROC>(message_handler);
wx.hInstance = reinterpret_cast<HINSTANCE>(GetModuleHandle(0));
wx.style = CS_HREDRAW | CS_VREDRAW;
wx.hInstance = GetModuleHandle(0);
wx.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wx.lpszClassName = CLS_NAME;
GUID guid = HID_CLASSGUID;
if (RegisterClassEx(&wx))
{
hWnd = CreateWindow(CLS_NAME, "DevNotifWnd", WS_ICONIC,
0, 0, CW_USEDEFAULT, 0, HWND_MESSAGE,
NULL, GetModuleHandle(0), (void*)&guid);
}
if(hWnd == NULL)
{
throw std::runtime_error("Could not create message window!");
}
std::cout << "waiting for new devices..n";
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
仅消息窗口不接收WM_DEVICECHANGE
消息。因此,您需要制作一个不仅仅是消息的窗口。它不需要是可见的,只需要不只是消息。
将0
而不是HWND_MESSAGE
传递给CreateWindow
,您的程序将开始接收WM_DEVICECHANGE
消息。
相关文章:
- 通过JNA发送用户定义的Windows消息
- 什么是窗口最大化/最小化/恢复的WM Windows消息
- 注册以在 MFC 中发出 Win32 事件信号时接收 Windows 消息
- 在控制台应用程序中处理空的windows消息队列
- 处理Windows消息,以便我的应用程序正确响应
- 相对于 3D 应用程序中的 Windows 消息循环的计时错误
- infine windows消息循环-c++
- 如何使用QCoreApplication::winEventFilter处理Windows消息
- Sleep() in Windows 消息循环
- 奇怪的Windows消息序列WM_KEYDOWN/WM_KEYUP,同时按VK_MENU(Alt键)
- Windows 消息过程的函数指针数组
- winapi - 未记录的 Windows 消息0x0313稳定
- 来自 GetMessage 的 Windows 消息 9
- 如何正确处理来自MSFTEDIT_CLASS(RichEdit)控件的Windows消息
- 无限循环中的Windows消息泵
- Windows消息传递-获取非客户端区域的设备上下文的各种方法
- 处理来自子类的windows消息
- C++GetAsyncKeyState和GetCursorPos与Windows消息传递的比较
- 正在获取windows消息名称
- 使用Windows消息循环的回调实现