+ Reply to Thread
Results 1 to 2 of 2

Thread: How to pass data between two application using SendMessage?

  1. #1

    How to pass data between two application using SendMessage?

    How to pass data between two application using SendMessage?

  2. #2
    Senior Member
    Join Date
    Feb 2012
    Posts
    109
    Example:

    Code:
    //CopyUserData.h
    #define USER_DATA_STUDENT	1
    #define USER_DATA_BOOK		2
    
    struct CStudent
    {
    	int		m_nRoleID ;
    	char	m_szName[100] ;
    } ;
    
    struct CBook
    {
    	char	m_szBookName[100] ;
    	char	m_szAuthor[100] ;
    	char	m_szPublication[100] ;
    	char	m_nISBN[100] ;
    } ;
    
    
    void SendDataToWindow(HWND hSourceWnd, HWND hTargetWnd, COPYDATASTRUCT& objMessageData)
    {
    	::SendMessage(hTargetWnd, WM_COPYDATA, (WPARAM)(HWND) hSourceWnd, (LPARAM) (LPVOID) &objMessageData) ;
    }
    
    void SendStudentObject(HWND hSourceWnd, HWND hTargetWnd)
    {
    	CStudent objStudent ;
    
    	objStudent.m_nRoleID = 1 ;
    	strcpy(objStudent.m_szName, "Roger") ;
    
    	COPYDATASTRUCT objMessageData ;
    	objMessageData.dwData = USER_DATA_STUDENT ;
    	objMessageData.cbData = sizeof(CStudent) ;
    	objMessageData.lpData = &objStudent ;
    
    	SendDataToWindow(hSourceWnd, hTargetWnd, objMessageData) ;
    }
    
    void SendBookObject(HWND hSourceWnd, HWND hTargetWnd)
    {
    	CBook objBook ;
    	
    	strcpy(objBook.m_szBookName, "Thinking in C++") ;
    	strcpy(objBook.m_szAuthor, "Bruce Eckel") ;
    	strcpy(objBook.m_szPublication, "Prentice Hall") ;
    	strcpy(objBook.m_nISBN, "0139798099") ;
    
    	COPYDATASTRUCT objMessageData ;
    	objMessageData.dwData = USER_DATA_BOOK ;
    	objMessageData.cbData = sizeof(CBook) ;
    	objMessageData.lpData = &objBook ;
    
    	SendDataToWindow(hSourceWnd, hTargetWnd, objMessageData) ;
    }
    
    void DisplayMessage(char *pszMessage)
    {
    	OutputDebugString(pszMessage) ;
    	OutputDebugString("\r\n") ;
    }
    
    void OnReceiveCopyData(COPYDATASTRUCT *pMessageData)
    {
    	switch(pMessageData->dwData)
    	{
    	case USER_DATA_STUDENT:
    		{
    			CStudent *pStudent = (CStudent *) pMessageData->lpData ;
    
    			if (NULL != pStudent)
    			{
    				char szMessage[1024] ;
    				sprintf(szMessage, "Student, Role ID = %d, Name = %s", pStudent->m_nRoleID, pStudent->m_szName) ;
    				DisplayMessage(szMessage) ;
    			}
    		}
    		break ;
    
    	case USER_DATA_BOOK:
    		{
    			CBook *pBook = (CBook *) pMessageData->lpData ;
    
    			if (NULL != pBook)
    			{
    				char szMessage[1024] ;
    				sprintf(szMessage, "Book, Name = %s, Author = %s, Publication = %s, ISBN = %s", pBook->m_szBookName, pBook->m_szAuthor, pBook->m_szPublication, pBook->m_nISBN) ;
    				DisplayMessage(szMessage) ;
    			}
    		}
    		break ;
    
    	default:
    		break ;
    	}
    }

    Sender Application:

    Code:
    #include "CopyUserData.h"
    
    HWND hTargetWnd = ::FindWindow(NULL, "TestMFC") ;
    
    if (NULL != hTargetWnd)
    {
    	SendStudentObject(m_hWnd, hTargetWnd) ;
    	SendBookObject(m_hWnd, hTargetWnd) ;
    }

    Receiver Application (CDialog Based):

    Code:
    #include "CopyUserData.h"
    BEGIN_MESSAGE_MAP(CTestMFCDlg, CDialog)
    	//{{AFX_MSG_MAP(CTestMFCDlg)
    	ON_WM_COPYDATA()
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    BOOL CTestMFCDlg::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct)
    {
    	OnReceiveCopyData(pCopyDataStruct) ;
    
    	return TRUE ;
    }

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts