- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序创建了一个窗口,用于处理 WM_DEVICECHANGE
Windows 消息。 WndProc
确实被调用了几次,直到我的应用程序调用一个函数来轮询键盘事件,但是无论出于何种原因,当我移除或插入 USB 设备时它都没有被调用。
这是我的 USB 设备的 GUID。我确定它是正确的:
static const GUID _guidForCP210xDevices = {
0xA2A39220, 0x39F4, 0x4B88, 0xAE, 0xCB, 0x3D, 0x86, 0xA3, 0x5D, 0xC7, 0x48
};
我的窗口是这样创建的:
m_hInstance = ::GetModuleHandle( NULL );
if ( m_hInstance == NULL )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to retrieve the module handle.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
m_wcx.cbSize = sizeof(WNDCLASSEX); // size of structure
m_wcx.style = CS_HREDRAW | CS_VREDRAW; // initially minimized
m_wcx.lpfnWndProc = &WndProc; // points to window procedure
m_wcx.cbClsExtra = 0; // no extra class memory
m_wcx.cbWndExtra = 0; // no extra window memory
m_wcx.hInstance = m_hInstance; // handle to instance
m_wcx.hIcon = ::LoadIcon( NULL, IDI_APPLICATION ); // default app icon
m_wcx.hCursor = ::LoadCursor( NULL, IDC_ARROW ); // standard arrow cursor
m_wcx.hbrBackground = NULL; // no background to paint
m_wcx.lpszMenuName = NULL; // no menu resource
m_wcx.lpszClassName = _pwcWindowClass; // name of window class
m_wcx.hIconSm = NULL; // search system resources for sm icon
m_atom = ::RegisterClassEx( &m_wcx );
if ( m_atom == 0 )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to register window class.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
m_hWnd = ::CreateWindow(
_pwcWindowClass,
_pwcWindowName,
WS_ICONIC,
0,
0,
CW_USEDEFAULT,
0,
NULL,
NULL,
m_hInstance,
NULL
);
if ( m_hWnd == NULL )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to create window.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
::ShowWindow( m_hWnd, SW_HIDE ); // function does not fail
if ( RegisterForNotification() != ERROR_SUCCESS )
{
TRACE(_T("CNotifyWindow::CNotifyWindow : Failed to register for device notification.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
THROW(::GetLastError());
}
这是我注册设备通知的方式:
static DEV_BROADCAST_DEVICEINTERFACE dbt = {0};
ASSERT(m_hWnd != NULL);
// Populate DEV_BROADCAST_DEVICEINTERFACE structure.
dbt.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
dbt.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
dbt.dbcc_classguid = _guidForCP210xDevices;
// Register for HID devic notifications
m_hNotify = RegisterDeviceNotification( m_hWnd, &dbt, DEVICE_NOTIFY_WINDOW_HANDLE );
if ( m_hNotify == NULL )
{
TRACE(_T("CNotifyWindow::RegisterForNotification : Failed to register for device notification.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), ::GetLastError(), __WFILE__, __LINE__);
return ::GetLastError();
}
return ERROR_SUCCESS;
我的 WndProc
函数如下所示:
static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
DEV_BROADCAST_HDR * pHeader = reinterpret_cast<DEV_BROADCAST_HDR *>(lParam);
switch ( uMsg )
{
case WM_DEVICECHANGE:
if ( pHeader != NULL )
{
if ( pHeader->dbch_devicetype == DBT_DEVTYP_PORT )
{
OnDeviceChange( wParam );
}
}
break;
default:
// Do nothing.
break;
}
return ::DefWindowProc( hWnd, uMsg, wParam, lParam );
}
有谁知道我做错了什么?谢谢。
最佳答案
您缺少一个消息泵来从队列中检索通知并将它们分派(dispatch)到您的 WndProc。消息泵实际上是一个循环,它检查消息并同步调用适当的 WndProc。 MSDN有一些关于他们的好信息。我不知道您的代码的上下文是什么,所以我不确定您是否只需要在 RegisterForNotification 之后插入一个泵,或者是否需要进行更大的架构更改。
关于c++ - WM_DEVICECHANGE 消息未发送到 WndProc - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4326954/
如何知道 WM_DEVICECHANGE 的到来? WndProc 被覆盖。我收到了所有消息,但没有一条是 WM_DEVICECHANGE 类型的。 RegisterDeviceNotificatio
我正在尝试检测 USB 到达事件。我试图覆盖 wndproc() 来获取我的消息。但是我遇到了 Windows 消息的错误。 错误是: The name 'WM_DEVICECHANGE' does
我想检测计算机中任何 USB 驱动器的插入和移除。我没有Windows GUI编程的经验,通常使用Qt,但这次我不得不使用Win32 API。 问题是 lParam 总是空的。 我读到我需要使用 Re
我想捕获 WM_DEVICECHANGE 的消息。但是,有一个我无法理解的问题。我想看看 usb 或 cd 何时插入。也许我的通知过滤器是错误的。我正在使用 radstudio 和它的 c 语言,还有
我正在开发一个 C# 程序,以便在插入设备后立即检索设备的信息。 我想问一下,有什么方法可以根据设备插入/拔出时触发的 WM_DEVICECHANGE 事件获取设备的信息(名称、ID、...) . 我
我正在开发一个 Windows 7 应用程序,它必须防止 WinDVD 在插入时触发新光盘可用性(即插入 DVD)。 背景信息: 我正在为一家公司开发这个小应用程序,该公司必须同时比较两个电影播放器
直到今天,我一直在使用以下代码来捕获应用程序主窗体中的 WM_DEVICECHANGE 消息,并且效果良好。但是,如果我尝试在自定义控件中使用它,我不会收到有关设备插入或删除的通知。发生了什么事?
我正在为 WPF 编写一个目录选择器控件,我想在目录树中添加/删除一个驱动器,当它被安装或卸载时或者当它准备好或未准备好时(例如,用户插入或删除 CD ).我正在寻找类似于 WM_DEVICECHAN
我的应用程序创建了一个窗口,用于处理 WM_DEVICECHANGE Windows 消息。 WndProc 确实被调用了几次,直到我的应用程序调用一个函数来轮询键盘事件,但是无论出于何种原因,当我移
我在我的消息循环中监听 WM_DEVICECHANGE,当我移除或插入我的网络摄像头时,WM_DEVICECHANGE 出现两次,具有相同的 MSG(具有相同的 HWND、UINT、WPARAM 和
我正在编写一个应用程序来检测某个 USB 大容量存储设备何时插入,何时拔出 - 通过监听 WM_DEVICECHANGE messages . 我还注册了我的应用程序以监听 DBT_DEVTYP_DE
我创建 Hydra 主机应用程序和 Hydra 插件。我在插件中放置了一个用于处理 Windows 消息的过程;但在这种情况下我们无法处理这个窗口消息。为了解决这个问题,我们可以在主机应用程序中处理它
我正在使用 C# .NET 3.5 Compact Framework 来编写代码。谁能告诉我如何为 WM_DEVICECHANGE 注册一个表单以及如何在 WindowProc< 中处理该消息..
我正在按照一个示例来检测 Windows 7 中的 USB 闪存驱动器插件和拔出检测。我确实收到了通知 WM_DEVICECHANGE 但没有收到 DBT_DEVICEARRIVAL,这是在 USB
我一直致力于检测 USB 插入/移除。我已经使用 CreateWindowEx() 实现了代码,通过我的窗口进程回调传递了一个 WNCLASSEX。在插入和移除 USB 时,我成功收到 WM_DEVI
我是一名优秀的程序员,十分优秀!