Вопрос

I made a project that creates a DLL. This project uses the WFS methods and they access some hardware (devices) to get information or execute some commands.

In my project, I first open these devices then register them, I later use other methods to get information or execute.

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);

As you can see, the WFSRegister requires HWND as a parameter. WFSRegister uses this parameter to send events or messages to it.

My project is not an MFC project and I have no windows. I decided to create a window and assign the correct HWND to WFSRegister. I also created WndProc to get the messages that the WFS methods will send to me later.

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() Successfully create window, I have no prob here.

When I want to register my device, I call the following code to get the correct 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 is S_OK(meaning the device registered successfully) and windows_handle refers to the same HWND I created in Init_Window(). For example, both have 0x00100a58 values.

Now I change some property on my devices and I expect to get these message on my WndProc(), but it's not working.

WndProc() working somehow and gets some messages, but not the ones I want (not the ones the devices send to it).

I'm sure the devices send message (as events) because I can see they do by reading their logs.

For example:

2013/09/25 16:46:29 HService : 44 Event WFS_SRVE_SIU_PORT_STATUS Sent for HWND = 330d1c hResult = WFS_SUCCESS

HWND in the log refers to same HWND I created in Init_Window() and windows_handle.

Also, you all got what I want to do. If you have any other solution please feel free to mention it.

Это было полезно?

Решение

I found solution thanks to dear Igor Tandetnik

All i needed to do is to add 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()
    }
} 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top