Click here to Skip to main content
15,868,054 members
Articles / Programming Languages / C++
Article

Inter-Process Communication using WM_COPYDATA

Rate me:
Please Sign up or sign in to vote.
4.60/5 (8 votes)
3 Dec 1999 282.8K   8K   59   46
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)
    	{
    		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


    Written By
    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

     
    GeneralRe: Potential Blocking Issues Pin
    Anonymous23-Sep-02 18:51
    Anonymous23-Sep-02 18:51 
    Generalbetter way to do this --> M$-Sample Pin
    yannc10-Jul-01 9:25
    yannc10-Jul-01 9:25 
    GeneralRe: better way to do this --> M$-Sample Pin
    Eddy Celis10-Jul-01 10:22
    professionalEddy Celis10-Jul-01 10:22 
    GeneralRe: better way to do this --> M$-Sample Pin
    yannc10-Jul-01 20:04
    yannc10-Jul-01 20:04 
    GeneralRe: better way to do this --> M$-Sample Pin
    ecinaj0722-Jan-04 20:29
    ecinaj0722-Jan-04 20:29 
    GeneralRe: better way to do this --> M$-Sample Pin
    nilaysoft11-Mar-08 23:39
    nilaysoft11-Mar-08 23:39 
    Generalproblem between 2 instances Pin
    3-Jul-01 20:46
    suss3-Jul-01 20:46 
    GeneralGood one Pin
    Kannan Kalyanaraman21-Mar-01 19:16
    Kannan Kalyanaraman21-Mar-01 19:16 
    GeneralRe: Good one Pin
    Eddy Celis22-Mar-01 3:19
    professionalEddy Celis22-Mar-01 3:19 
    GeneralRe: Good one Pin
    6-Apr-01 17:05
    suss6-Apr-01 17:05 
    GeneralRe: Good one Pin
    Member 59089-Apr-01 6:12
    Member 59089-Apr-01 6:12 
    GeneralRe: Good one Pin
    3-May-01 6:08
    suss3-May-01 6:08 
    GeneralRe: Good one - solution here :) Pin
    Tim Stubbs28-Aug-01 4:16
    Tim Stubbs28-Aug-01 4:16 
    GeneralRe: Good one - solution here :) Pin
    Anonymous4-Oct-02 11:13
    Anonymous4-Oct-02 11:13 
    GeneralRe: Good one - solution here :) Pin
    Tim Stubbs12-Nov-02 4:55
    Tim Stubbs12-Nov-02 4:55 
    GeneralRe: Good one - solution here :) Pin
    Anonymous15-Mar-04 14:20
    Anonymous15-Mar-04 14:20 
    GeneralRe: Good one Pin
    #realJSOP4-May-01 3:58
    mve#realJSOP4-May-01 3:58 
    GeneralRe: Good one Pin
    Chris Meech4-May-01 8:22
    Chris Meech4-May-01 8:22 
    GeneralRe: Good one Pin
    #realJSOP4-May-01 11:05
    mve#realJSOP4-May-01 11:05 
    GeneralNevermind Pin
    Gino Quintana7-Feb-00 12:53
    Gino Quintana7-Feb-00 12:53 
    General16 bit source code link is not working Pin
    Gino Quintana7-Feb-00 12:50
    Gino Quintana7-Feb-00 12:50 

    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.