Skip to main content
Email Password   helpLost your password?
  • 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 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);
    }
    
    You must Sign In to use this message board.
     
     
    Per page   
     FirstPrevNext
    GeneralAn bug in the program.(the buffer should include the end 0 of the string) Pin
    linzhehui
    20:31 5 Dec '08  
    GeneralQuestions Pin
    Tom Wright
    6:23 15 Dec '04  
    Generalis the window available? Pin
    darwinw
    9:25 18 Nov '03  
    GeneralRe: is the window available? Pin
    ThatsAlok
    22:08 24 Sep '04  
    GeneralRe: is the window available? Pin
    VitalyTomilov
    5:25 1 Dec '04  
    GeneralRe: is the window available? Pin
    ThatsAlok
    19:08 1 Dec '04  
    GeneralVideo Conferencing Pin
    Ashutosh Pandey
    0:49 11 Dec '02  
    GeneralMulti Client Application Pin
    Spiros
    9:54 7 Nov '02  
    GeneralRe: Multi Client Application Pin
    Eddy Celis
    16:54 7 Nov '02  
    GeneralRe: Multi Client Application Pin
    Spiros
    17:51 7 Nov '02  
    GeneralRe: Multi Client Application Pin
    Matthias Mann
    12:33 5 Dec '02  
    GeneralRe: Multi Client Application Pin
    Anonymous
    8:58 4 Oct '04  
    GeneralRe: Multi Client Application Pin
    Spiros
    16:22 4 Oct '04  
    GeneralSmall bug Pin
    Andreas Muegge
    22:47 30 Oct '02  
    GeneralIt doesn't work with MDI! Pin
    John Wong
    12:27 24 Aug '02  
    GeneralRe: It doesn't work with MDI! Pin
    Bughunter
    16:08 24 Aug '02  
    GeneralRe: It doesn't work with MDI! Pin
    John Simmons / outlaw programmer
    1:31 11 Dec '02  
    GeneralSendMessage followed by file read Pin
    BigBolMan
    7:21 24 Jul '02  
    GeneralPotential Blocking Issues Pin
    David Stroupe
    6:10 27 Jun '02  
    GeneralRe: Potential Blocking Issues Pin
    Anonymous
    19:51 23 Sep '02  
    Generalbetter way to do this --> M$-Sample Pin
    yannc
    10:25 10 Jul '01  
    GeneralRe: better way to do this --> M$-Sample Pin
    Eddy Celis
    11:22 10 Jul '01  
    GeneralRe: better way to do this --> M$-Sample Pin
    yannc
    21:04 10 Jul '01  
    GeneralRe: better way to do this --> M$-Sample Pin
    clayton04
    21:29 22 Jan '04  
    GeneralRe: better way to do this --> M$-Sample Pin
    Li Zhaoming
    0:39 12 Mar '08  


    Last Updated 3 Dec 1999 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009