Postmessge or SendMessage () for interprocess communication
Method 1:postmessge or SendMessage () message mechanism
Message sent in Project 1:
#define Wm_mymessage Wm_user + 1//the window class name of the target process (which can be viewed through the Spy + + tool) and the window name CWnd *pwnd = Cwnd::findwindow ("#32770", "mfctest"); if (NUL L! = pWnd) {pwnd->postmessage (wm_mymessage, NULL, NULL);} OR: HWND hwnd =:: FindWindow ("#32770", "mfctest"), if (NULL! = hwnd) {::P ostmessage (hwnd, wm_mymessage, NULL, NULL);}
Message received in Item 2:
. H declares that:
afx_msg LRESULT onmymessage (WPARAM WP, LPARAM LP);
Defined in. cpp:
Begin_message_map (CMainFrame, CFrameWnd) ... On_message (Wm_mymessage, OnMyMessage)
LRESULT cmfctestdlg::onmymessage (WPARAM WP, LPARAM LP) {AfxMessageBox ("Hello World"); return 0;}
Description
Using either PostMessage or SendMessage, the difference is that the SendMessage blocks until the target window program finishes processing the message and returns, and PostMessage sends a message to a thread's message queue and returns immediately. PostMessage is usually used, and SendMessage is better if the target process is to be detected.
Method 2: The message receiving end uses PreTranslateMessage () to handle
Send-side code:
#define Wm_mymessage Wm_user + 1//the window class name of the target process (which can be viewed through the Spy + + tool) and the window name CWnd *pwnd = Cwnd::findwindow ("#32770", "mfctest"); if (NUL L! = pWnd) {pwnd->postmessage (wm_mymessage, NULL, NULL);} OR: HWND hwnd =:: FindWindow ("#32770", "mfctest"), if (NULL! = hwnd) {::P ostmessage (hwnd, wm_mymessage, NULL, NULL);}
Receive-Side code:
. h Declaration
BOOL Cmfctestdlg::P retranslatemessage (msg* pMsg);
. CPP Definition
#define Wm_mymessage Wm_user + 1 BOOL cmfctestdlg::P retranslatemessage (msg* pMsg) {if (Pmsg->message = Wm_user + 1 {AfxMessageBox ("Hello World");} Return CDialog::P retranslatemessage (PMSG);}
Description
The receive-side processing is placed in the PreTranslateMessage, so the sender must select PostMessage because the SendMessage message is not placed in the message queue, PreTranslateMessage can not receive this message.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Message passing of VC interprocess communication