WndProc() 没有收到我发送给它的消息

WndProc() is not getting the messages I sent to it

本文关键字:消息 WndProc      更新时间:2023-10-16

我做了一个创建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()
    }
}