WndProc() 没有收到我发送给它的消息
WndProc() is not getting the messages I sent to it
我做了一个创建DLL
的项目。该项目使用 WFS
方法,它们访问一些硬件(设备(来获取信息或执行某些命令。
在我的项目中,我首先打开这些设备,然后注册它们,然后使用其他方法来获取信息或执行。
HRESULT extern WINAPI WFSOpen ( LPSTR lpszLogicalName, HAPP hApp, LPSTR lpszAppID, DWORD dwTraceLevel, DWORD dwTimeOut, DWORD dwSrvcVersionsRequired, LPWFSVERSION lpSrvcVersion, LPWFSVERSION lpSPIVersion, LPHSERVICE lphService);
HRESULT extern WINAPI WFSRegister ( HSERVICE hService, DWORD dwEventClass, HWND hWndReg);
如您所见,WFSRegister
需要HWND
作为参数。 WFSRegister
使用此参数向其发送事件或消息。
我的项目不是 MFC 项目,我没有窗口。我决定创建一个窗口并将正确的HWND
分配给WFSRegister
。我还创建了WndProc
来获取WFS
方法稍后将发送给我的消息。
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WFS_EXECUTE_EVENT:
cout<<"WFS_EXECUTE_EVENT";
break;
case WFS_SERVICE_EVENT:
cout<<"WFS_EXECUTE_EVENT";
break;
case WFS_USER_EVENT:
cout<<"WFS_USER_EVENT";
break;
case WFS_SYSTEM_EVENT:
cout<<"WFS_SYSTEM_EVENT";
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam );
}
void Init_Window()
{
WNDCLASS Wclass;
Wclass.hInstance = gHinstance;
Wclass.cbClsExtra = 0;
Wclass.cbWndExtra = 0;
Wclass.lpszClassName = TEXT("Device_Manager_Class_Name");
Wclass.lpszMenuName = NULL;
Wclass.lpfnWndProc = WndProc;
Wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
Wclass.hCursor = LoadIcon(NULL, IDC_ARROW);
Wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Wclass.style = CS_OWNDC;
if(!RegisterClass(&Wclass))
{
cout<<"Unable to Register Class";
}
ULONG Window_Width;
ULONG Window_Height;
DWORD style;
Window_Width = SCREEN_WIDTH;
Window_Height = SCREEN_HEIGHT;
style = WS_OVERLAPPED|WS_SYSMENU;
gHwnd = CreateWindow(TEXT("Device_Manager_Class_Name")
, TEXT("Device_Manager_Class_Title")
, style
, 0
, 0
, Window_Width
, Window_Height
, GetDesktopWindow()
, NULL
, gHinstance
, NULL);
if(!gHwnd){
cout<<"Unable to create the main window";
}
ShowWindow(gHwnd, SW_SHOW);
UpdateWindow(gHwnd);
SetFocus(gHwnd);
}
Init_Window()
成功创建窗口,我在这里没有问题。
当我想注册我的设备时,我调用以下代码以获取正确的HWND
:
HWND windows_handle = FindWindow(TEXT("Device_Manager_Class_Name"), 0);
HRESULT result = WFSRegister(wfsRes.hService, WFS_EXECUTE_EVENT || WFS_SERVICE_EVENT || WFS_USER_EVENT || WFS_SYSTEM_EVENT , windows_handle);
result
S_OK
(表示设备已成功注册(,windows_handle
指的是我在Init_Window()
中创建的同一HWND
。例如,两者都具有0x00100a58
值。
现在我更改了设备上的一些属性,我希望在我的WndProc()
上收到这些消息,但它不起作用。
WndProc()
以某种方式工作并收到一些消息,但不是我想要的消息(不是设备发送给它的消息(。
我确定设备发送消息(作为事件(,因为我可以通过读取日志看到它们这样做。
例如:
2013/09/25 16:46:29 HService : 44 发送的事件 WFS_SRVE_SIU_PORT_STATUS 用于 HWND = 330d1c hResult = WFS_SUCCESS
日志中的HWND
是指我在 Init_Window()
和 windows_handle
中创建的相同HWND
。
另外,你们都得到了我想做的事情。如果您有任何其他解决方案,请随时提及。
我找到了解决方案,这要归功于亲爱的伊戈尔·坦德特尼克
我需要做的就是添加GetMessage()
MSG msg;
BOOL bRet;
HWND windows_handle = FindWindow(TEXT("Device_Manager_Class_Name"), 0);
while( (bRet = GetMessage( &msg, windows_handle, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg); //<< This line send msg to WndProc()
}
}
- boost::进程间消息队列引发错误
- 在线编译器中的分段C++没有打印消息
- C++错误消息*成员参考.**初学者*
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何通过参数抛出错误消息
- 从服务器传输到客户端的消息不会出现
- ROS2 动态消息模板
- C++秘密消息学校作业
- glad 导致 glfwSwapBuffers 返回错误消息
- C++入门 5 版:类消息和文件夹
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何处理从一个对象传递到另一个在C++中具有公共抽象类的对象的消息
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 如何让 wndproc 处理发送到多个窗口的消息
- 将 WndProc 替换为另一个函数来处理消息
- 如何在 wndproc 消息处理程序中在窗口下触发 jit 调试器
- WndProc() 没有收到我发送给它的消息
- 为什么 SetScrollInfo() 函数向 WndProc() 发送WM_SIZE消息
- 如何从 WndProc 的参数中检索消息发送对象?