Message passing of VC interprocess communication

Source: Internet
Author: User
Tags message queue


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

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.