Click here to Skip to main content
Licence 
First Posted 3 Dec 1999
Views 181,703
Bookmarked 51 times

Inter-Process Communication using WM_COPYDATA

By Eddy Celis | 3 Dec 1999
Demonstrates the use of WM_COPYDATA

1

2

3
2 votes, 40.0%
4
3 votes, 60.0%
5
4.23/5 - 24 votes
μ 4.23, σa 0.96 [?]
  • 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)
    	{
    		COPYDATASTRUCT cpd;
    		cpd.dwData = 0;
    		cpd.cbData = strDataToSend.GetLength();
    		cpd.lpData = (void*)strDataToSend.GetBuffer(cpd.cbData);
    		copyDataResult = pOtherWnd->SendMessage(WM_COPYDATA,
                                                    (WPARAM)AfxGetApp()->m_pMainWnd->GetSafeHwnd(),
                                                    (LPARAM)&cpd);
    		strDataToSend.ReleaseBuffer();
    		// copyDataResult has value returned by other app
    		
    	} 
    	else 
    	{
    		AfxMessageBox("Unable to find other app.");
    	}	
    }
    

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

    BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
    	//{{AFX_MSG_MAP(CMyWnd)
    	...
    	ON_WM_COPYDATA()
    	...
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    ...
    
    BOOL CMyWnd::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) 
    {
    	CString strRecievedText = (LPCSTR) (pCopyDataStruct->lpData);
    	
    	return CMyWnd::OnCopyData(pWnd, pCopyDataStruct);
    }
    

    License

    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

    Web Developer

    United States United States

    Member
    Recent experience is C++ and MFC. Specialized in 'middle-ware' and comms.
    A lot of experience with codecs and video-conferencing in general.
    Currently writing firmware for 802.11 WLAN.
    Living in Palm Bay, FL.

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    Generalproblem with WM_COPYDATA PinmemberAdeel Mirza2:50 7 Mar '11  
    GeneralRe: problem with WM_COPYDATA Pinmemberbhupendradhyani3:13 6 Apr '11  
    GeneralAn bug in the program.(the buffer should include the end 0 of the string) Pinmemberlinzhehui20:31 5 Dec '08  
    GeneralQuestions PinmemberTom Wright6:23 15 Dec '04  
    Questionis the window available? Pinmemberdarwinw9:25 18 Nov '03  
    AnswerRe: is the window available? PinmemberThatsAlok22:08 24 Sep '04  
    GeneralRe: is the window available? PinmemberVitalyTomilov5:25 1 Dec '04  
    GeneralRe: is the window available? PinmemberThatsAlok19:08 1 Dec '04  
    Then Best of Luck Buddy,i Will Pray to god That the Best One Win (i think thats You!!)
     
    -----------------------------
    "I Think this Will Help"
    -----------------------------
    Alok Gupta
    visit me at http://www.thisisalok.tk

    GeneralVideo Conferencing PinmemberAshutosh Pandey0:49 11 Dec '02  
    GeneralMulti Client Application PinmemberSpiros9:54 7 Nov '02  
    GeneralRe: Multi Client Application PinmemberEddy Celis16:54 7 Nov '02  
    GeneralRe: Multi Client Application PinmemberSpiros17:51 7 Nov '02  
    GeneralRe: Multi Client Application PinmemberMatthias Mann12:33 5 Dec '02  
    GeneralRe: Multi Client Application PinsussAnonymous8:58 4 Oct '04  
    GeneralRe: Multi Client Application PinmemberSpiros16:22 4 Oct '04  
    GeneralSmall bug PinmemberAndreas Muegge22:47 30 Oct '02  
    GeneralIt doesn't work with MDI! PinmemberJohn Wong12:27 24 Aug '02  
    GeneralRe: It doesn't work with MDI! PinsussBughunter16:08 24 Aug '02  
    GeneralRe: It doesn't work with MDI! PinmemberJohn Simmons / outlaw programmer1:31 11 Dec '02  
    GeneralSendMessage followed by file read PinmemberBigBolMan7:21 24 Jul '02  
    GeneralPotential Blocking Issues PinmemberDavid Stroupe6:10 27 Jun '02  
    GeneralRe: Potential Blocking Issues PinsussAnonymous19:51 23 Sep '02  
    Generalbetter way to do this --> M$-Sample Pinmemberyannc10:25 10 Jul '01  
    GeneralRe: better way to do this --> M$-Sample PinmemberEddy Celis11:22 10 Jul '01  
    GeneralRe: better way to do this --> M$-Sample Pinmemberyannc21:04 10 Jul '01  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web02 | 2.5.120210.1 | Last Updated 4 Dec 1999
    Article Copyright 1999 by Eddy Celis
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid