Click here to Skip to main content
15,314,282 members
Articles / Programming Languages / C++
Posted 3 Dec 1999


59 bookmarked

Inter-Process Communication using WM_COPYDATA

Rate me:
Please Sign up or sign in to vote.
4.67/5 (7 votes)
3 Dec 1999
Demonstrates the use of WM_COPYDATA
  • Download demo project - 83 Kb
  • Download 32-bit source - 20 Kb
  • Download 16-bit source - 13 Kb
  • sample image

    This demo shows how to send and receive message between apps (32-bit and 16-bit) using WM_COPYDATA.

    There are two samples. One is 32-bit and one is 16-bit. The 32-bit sample will check and see whether it is already running and if so, will start another instance with a different window header.

    • The 'Send' button will send the text from the editbox to the other 32-bit app.
    • The 'Send16' button will send the text to the 16-bit app.

    The received data will show up in the static box.Only 2 instances of the 32-bit apps are supported in this demo. The 16-bit app will only send and receive data to/from the 32-bit apps.

    The exchange of data is performed by finding the other application (using FindWindow) and sending a WM_COPYDATA message to that window:

        CString strWindowTitle = _T("Window Name");
        CString strDataToSend  = _T("This is a message to send");
    	LRESULT copyDataResult;
    	CWnd *pOtherWnd = CWnd::FindWindow(NULL, strWindowTitle);
    	if (pOtherWnd)
    		cpd.dwData = 0;
    		cpd.cbData = strDataToSend.GetLength();
    		cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData);
    		copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,
    		// copyDataResult has value returned by other app
    		AfxMessageBox("Unable to find other app.");

    The other app should handle the WM_COPYDATA message in the following manner

    BOOL CMyWnd::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) 
    	CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData);
    	return CMyWnd::OnCopyData(pWnd, pCopyDataStruct);


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Eddy Celis
    Software Developer (Senior)
    United States United States
    From firmware in Assembly code to GUIs in C#; done it all.
    Currently mainly programming C, C++ at the Windows system level.
    Living in Palm Bay, FL.

    Comments and Discussions

    QuestionReceiver part doesnt compile Pin
    dlavrantonis6-Oct-13 0:16
    Memberdlavrantonis6-Oct-13 0:16 
    AnswerRe: Receiver part doesnt compile Pin
    Eddy Celis6-Oct-13 2:31
    professionalEddy Celis6-Oct-13 2:31 
    Generalvery nice artical,thanks Pin
    haiqing19881-Jul-13 2:42
    Memberhaiqing19881-Jul-13 2:42 
    Generalproblem with WM_COPYDATA Pin
    Adeel Mirza7-Mar-11 1:50
    MemberAdeel Mirza7-Mar-11 1:50 
    GeneralRe: problem with WM_COPYDATA Pin
    bhupendradhyani6-Apr-11 2:13
    Memberbhupendradhyani6-Apr-11 2:13 
    GeneralAn bug in the program.(the buffer should include the end 0 of the string) Pin
    linzhehui5-Dec-08 19:31
    Memberlinzhehui5-Dec-08 19:31 
    GeneralRe: An bug in the program.(the buffer should include the end 0 of the string) Pin
    Destiny7774-Mar-13 17:42
    MemberDestiny7774-Mar-13 17:42 
    GeneralQuestions Pin
    Tom Wright15-Dec-04 5:23
    MemberTom Wright15-Dec-04 5:23 
    Questionis the window available? Pin
    darwinw18-Nov-03 8:25
    Memberdarwinw18-Nov-03 8:25 
    AnswerRe: is the window available? Pin
    ThatsAlok24-Sep-04 21:08
    MemberThatsAlok24-Sep-04 21:08 
    GeneralRe: is the window available? Pin
    Vitaly Tomilov1-Dec-04 4:25
    MemberVitaly Tomilov1-Dec-04 4:25 
    GeneralRe: is the window available? Pin
    ThatsAlok1-Dec-04 18:08
    MemberThatsAlok1-Dec-04 18:08 
    GeneralVideo Conferencing Pin
    Ashutosh Pandey10-Dec-02 23:49
    MemberAshutosh Pandey10-Dec-02 23:49 
    GeneralMulti Client Application Pin
    Spiros7-Nov-02 8:54
    MemberSpiros7-Nov-02 8:54 
    GeneralRe: Multi Client Application Pin
    Eddy Celis7-Nov-02 15:54
    professionalEddy Celis7-Nov-02 15:54 
    GeneralRe: Multi Client Application Pin
    Spiros7-Nov-02 16:51
    MemberSpiros7-Nov-02 16:51 
    GeneralRe: Multi Client Application Pin
    Matthias Mann5-Dec-02 11:33
    MemberMatthias Mann5-Dec-02 11:33 
    GeneralRe: Multi Client Application Pin
    Anonymous4-Oct-04 7:58
    MemberAnonymous4-Oct-04 7:58 
    GeneralRe: Multi Client Application Pin
    Spiros4-Oct-04 15:22
    MemberSpiros4-Oct-04 15:22 
    GeneralSmall bug Pin
    Andreas Muegge30-Oct-02 21:47
    MemberAndreas Muegge30-Oct-02 21:47 
    GeneralIt doesn't work with MDI! Pin
    John Wong24-Aug-02 11:27
    MemberJohn Wong24-Aug-02 11:27 
    GeneralRe: It doesn't work with MDI! Pin
    Member 590824-Aug-02 15:08
    MemberMember 590824-Aug-02 15:08 
    GeneralRe: It doesn't work with MDI! Pin
    #realJSOP11-Dec-02 0:31
    mva#realJSOP11-Dec-02 0:31 
    GeneralSendMessage followed by file read Pin
    BigBolMan24-Jul-02 6:21
    MemberBigBolMan24-Jul-02 6:21 
    GeneralPotential Blocking Issues Pin
    David Stroupe27-Jun-02 5:10
    MemberDavid Stroupe27-Jun-02 5:10 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.