Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Problem when Invoking SendMessage() between two Windows Apps.. Please Help..

1) User defined message is declared as Follows [ Defined in Both Sender and Receiver ]
#define TEST_MSG WM_USER + 1

2) The Message data Structure Declared [ In both The Application ]
class msgData
{
public:
int msgNo;
CString data;
};

3) Window1 in App1 Sending the Message

void CTesterDlg::OnButton1()
{
HWND hw = ::FindWindow(0, "TestDCLeak") ;
msgData * data = new msgData();
data->data = "Sample Message" ;
data->msgNo = 20 ;
::SendMessage(hw, TEST_MSG, (WPARAM) data , 0 );
}

4) The Following Message Map is Added in the AFX_MSG_MAP Section of the App2:
ON_MESSAGE (TEST_MSG, OnTest )

5) The Linkage b/t the Handler and the Message is Done as Follows [ In App 2] :
afx_msg LRESULT OnTest(WPARAM wParam, LPARAM lParam);

6) Window 2 in App2 Receiving the Message
LRESULT CTestDCLeakDlg::OnTest(WPARAM wParam, LPARAM lParam)
{
msgData * theData = (msgData * ) wParam ;
AfxMessageBox(theData->data);
return 0;
}

* I can Find the App2 Message is Handler is Invoked & the pointer thedata in App2 is Equal to pointer
data in App1.

* But, I can't Able the get the data packed in App1, inside the App2..

Can Anybody Please Explain why this is Happened? What Correction I should take in Code?
[1402 byte] By [sirama] at [2007-11-11 8:25:29]
# 1 Re: Problem when Invoking SendMessage() between two Windows Apps.. Please Help..
You are sending a pointer to memory that the reciever does not own. If you can send it as data instead of a pointer to data, that would be better. Or look into memory sharing (complex) between processes. Or use like udp or tcp to talk instead (more portable, fairly simple).
jonnin at 2007-11-11 21:01:26 >
# 2 Re: Problem when Invoking SendMessage() between two Windows Apps.. Please Help..
Thanks. I got a advice to use WM_COPYDATA option...
Otherwise I have to go for memory sharing or pipes or Sockets
sirama at 2007-11-11 21:02:22 >