Click here to Skip to main content
Click here to Skip to main content

Inter-Process Communication using WM_COPYDATA

By , 3 Dec 1999
 
  • 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

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    Generalproblem with WM_COPYDATAmemberAdeel Mirza7 Mar '11 - 1:50 
    GeneralRe: problem with WM_COPYDATAmemberbhupendradhyani6 Apr '11 - 2:13 
    GeneralAn bug in the program.(the buffer should include the end 0 of the string)memberlinzhehui5 Dec '08 - 19:31 
    GeneralRe: An bug in the program.(the buffer should include the end 0 of the string)memberDestiny7774 Mar '13 - 17:42 
    GeneralQuestionsmemberTom Wright15 Dec '04 - 5:23 
    Questionis the window available?memberdarwinw18 Nov '03 - 8:25 
    AnswerRe: is the window available?memberThatsAlok24 Sep '04 - 21:08 
    GeneralRe: is the window available?memberVitalyTomilov1 Dec '04 - 4:25 
    GeneralRe: is the window available?memberThatsAlok1 Dec '04 - 18:08 
    GeneralVideo ConferencingmemberAshutosh Pandey10 Dec '02 - 23:49 
    Hi eddie
    i m new kid on the VC++ block. I am presently wrking on a virtual classroom application. I have made use of Windows Media Encoder in order to broadcast audio video information.
    In this application i need to share applications and sync with all the connected users (a concept of VNC).
    Can u plzz guide me in how can i achieve desktop sharing
    as well as is there any better option for Video Conferencing like the H323 protocol used by netmeeting.
    Thank you in advance
    regards
    ashutoshRoll eyes | :rolleyes:
    GeneralMulti Client ApplicationmemberSpiros7 Nov '02 - 8:54 
    GeneralRe: Multi Client ApplicationmemberEddy Celis7 Nov '02 - 15:54 
    GeneralRe: Multi Client ApplicationmemberSpiros7 Nov '02 - 16:51 
    GeneralRe: Multi Client ApplicationmemberMatthias Mann5 Dec '02 - 11:33 
    GeneralRe: Multi Client ApplicationsussAnonymous4 Oct '04 - 7:58 
    GeneralRe: Multi Client ApplicationmemberSpiros4 Oct '04 - 15:22 
    GeneralSmall bugmemberAndreas Muegge30 Oct '02 - 21:47 
    GeneralIt doesn't work with MDI!memberJohn Wong24 Aug '02 - 11:27 
    GeneralRe: It doesn't work with MDI!sussBughunter24 Aug '02 - 15:08 
    GeneralRe: It doesn't work with MDI!memberJohn Simmons / outlaw programmer11 Dec '02 - 0:31 
    GeneralSendMessage followed by file readmemberBigBolMan24 Jul '02 - 6:21 
    GeneralPotential Blocking IssuesmemberDavid Stroupe27 Jun '02 - 5:10 
    GeneralRe: Potential Blocking IssuessussAnonymous23 Sep '02 - 18:51 
    Generalbetter way to do this --> M$-Samplememberyannc10 Jul '01 - 9:25 
    GeneralRe: better way to do this --> M$-SamplememberEddy Celis10 Jul '01 - 10:22 

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

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