FindWindow and SendMessage solve Process Communication

Source: Internet
Author: User

 In a monitoring project, I can simply say that "a.exe" is a main program, but sometimes I don't know how to monitor it in real time with "B .exe" to obtain the running status. I tried several methods of process communication. I think there is a simple and practical method, that is, using FindWindow and SendMessage. I would like to write down it here.

     First, FindWindow.
     FindWindow returns the window handle of the top-level window of the window class name or window name that matches the specified character creation. Function prototype:
     C ++:
     HWND FindWindow (maid, maid );
     The lpClassName is the name of the window class, which is not used in most cases. LpWindowName is the window name, that is, the window title. Any window has a title, and of course there will be the same title. It will only find the window at the top of z-order.
     
     Let's look at SendMessage, which is a little more complicated. It is the main force of communication.
     SendMessage sends the specified message to one or more windows. Function prototype:
     C ++:
     LRESULT SendMessage (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM IParam );
     HWnd is the handle of the window for receiving messages. Msg is the message to be sent. Both wParam and IParam specify the additional message information.

     Here we will only describe how to use them to implement process communication. In fact, there are still many functions that we will not detail in detail.
     To use SendMessage, you need to know the window handle. Of course, you need to know the message through FindWindow. Here, the message 0x004A is used. Generally, the variable is named WM_COPYDATA, there is also the message to be sent, that is, the specific content, you need a specific structure COPYDATASTRUCT, the prototype is:
     C ++:
     Struct COPYDATASTRUCT {ULONG_PTR dwData; DWORD cbData; PVOID lpData ;}
      Now you can send the message to another window. The Code is as follows:
     C ++:
     

Code
VoidSendFormMessage (LPCTSTRFormText,PVOIDMsg)
{
    HWNDHwnd = FindWindow (NULL, formText );
    If (hwnd! = 0)
    {
        COPYDATASTRUCTCd;
    Cd. dwData=100;
    Cd. cbData=100;
    Cd. lpData=Msg;
    SendMessage (hwnd,WM_COPYDATA,0,(LPARAM) (& cd ));
    }
}

     After sending, the receiving window will receive the message. How can we receive the message? It is also very easy to get it based on the message 0x004A. The Code is as follows:
     C ++:
     

Code
LONGWINAPI AppWndProc (HWNDHwnd,UINTMsg,WPARAMWParam,LPARAMLParam) // reload the window message
{
    COPYDATASTRUCT *PCopyDataStruct;
    CharBuf [];

    Switch (msg)
    {
    CaseWM_COPYDATA:
    {
        PCopyDataStruct=(COPYDATASTRUCT *) lParam;
        Memset (& buf,0,Sizeof (buf ));
        Memcpy (& buf, pCopyDataStruct-> lpData, pCopyDataStruct-> cbData );

        If(0=Strcmp (buf, "") // you can judge it here.
        {}
        }
        Break;
        
    }
    Return(LONG)DefWindowProc (hwnd, msg, wParam, lParam );
}

     It's very easy. There are not many codes and it should be easy to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.