gpt4 book ai didi

c++ - WM_DEVICECHANGE 消息未发送到 WndProc - C++

转载 作者:行者123 更新时间:2023-11-30 02:10:48 27 4
gpt4 key购买 nike

我的应用程序创建了一个窗口,用于处理 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com