LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Capture Windows system messages using RegisterWinMsgCallback()

Hello ,

 

I have programmed a socket server  to capture the socket message, this code phase using the Windows API "WSAAsyncSelect()" to register some specific messages, and when the client connect the server, it can trigger some events. the code phase as follows:

 

int main ()

{

...

    net_addrs.sin_family = AF_INET;
    net_addrs.sin_port = htons(CTRL_PORT_PAS);
    net_addrs.sin_addr.S_un.S_addr = inet_addr("192.168.0.101");               
   
    res = bind(sockt, (struct sockaddr *)&net_addrs, (int)sizeof(struct sockaddr));
   
    listen(sockt, NUM_THD);
   
                  
    hwnd = (HWND)GetCVIWindowHandle();
    wmsg = RegisterWinMsgCallback(WindowsMsgProc, NULL, NULL, 0, &res, 1);/* Register windos call back*/

    res = WSAAsyncSelect (sockt, hwnd, wmsg, FD_READ|FD_ACCEPT|FD_CLOSE);
   
    DisplayPanel (dumyPanel);
    RunUserInterface ();
   
    closesocket(sockt);
    WSACleanup();
    
    DiscardPanel (dumyPanel);
    ...

}

 

 The callback function code :

 

void CVICALLBACK WindowsMsgProc (WinMsgWParam wParam, WinMsgLParam lParam, void *callbackData)
{
   
    if (wParam != sockt)
    {
        MessagePopup("Winsock callback error", "Not for the socket");
        GenLog("quit");
        return ;
    }
 
    switch (lParam)   
    {
        case FD_READ:
            break;
        case FD_ACCEPT:
            MessagePopup("ooook", "have a connect request");
            break;
        case FD_CLOSE:
             GenLog("quit");
             break;
        case WM_DESTROY:
        case WM_QUIT:
        case WM_CLOSE:
             GenLog("quit");
            //QuitUserInterface(0);
            break;
    }
}

 

The question is , if in the windows task manager to kill the process, Windows should send a WM_DESTORY or WM_QUIT message, but I find the message is never to capture by the callback function, because GenLog() never worked. Please give a support what is wrong in the above code. thanks.

 

David

 

0 Kudos
Message 1 of 3
(3,967 Views)

David,

 

In order to catch an event like WM_QUIT, you would want to use installWinMsgCallback instead of registerWinMsgCallback. InstallWinMsgCallback will look for windows messages while registerWinMsgCallback will look for user defined messages. Having said that, installWinMsgCallback cannot be used to capture WM_QUIT or WM_DESTROY. To catch an event of your application closing under normal circumstances, you should use the EVENT_CLOSE in the panel callback. Terminating the process with the End Process function of the task manager will not send a WM_DESTROY or WM_QUIT that can be caught by your application. Terminating the process is not a normal exit condition for an application and typically is not expected to be handled.

National Instruments
0 Kudos
Message 2 of 3
(3,942 Views)

Thanks Biel, I'll try it.

0 Kudos
Message 3 of 3
(3,940 Views)