No result code for SendMessage with WM_COPYDATA

G

Guest

Hello,
I try a inter process communication with windows messages and WM_COPYDATA.
It should work for managed and unmanaged applications. The receiver of
WM_COPYDATA messages is a managed applications. For the sender the result
code of SendMessage is important.
In the managed receiver I use Microsoft.WindowsCE.Forms.MessageWindow. I
override the WndProc, to check for WM_COPYDATA. Then I set the msg.Result to
the desired value and call base.WndProc. But the sender always get 0 as
result of his SendMessage.
What's wrong? Does base.WndProc overwrite my msg.Result.
Here is a code snippet from my receiver.

protected override void WndProc(ref Message msg)
{
switch(msg.Msg)
{
case Const.WM_COPYDATA:
COPYDATASTRUCT st = (COPYDATASTRUCT)
Marshal.PtrToStructure(msg.LParam, typeof(COPYDATASTRUCT));
if (this.iCallback != null)
{
string data = Marshal.PtrToStringUni(st.lpData);
msg.Result = AnotherFunction(data);
}
break;
}
base.WndProc (ref msg);
}

Thanks in advance
Steffen
 
G

Guest

Hi Chris,
yes, I think so. He gets the hWnd with FindWindow(NULL, ReceiverWindowName)
and the receiver sets the window title with SetWindowText. He also needs the
hWnd, becaues he uses SendMessage with hWnd parameter set and not with
broadcast message.
Now I also have a unmanaged sender application, with same result (0) for
SendMessage.

By Steffen
 
G

Guest

Hi Chris,

Chris Tacke said:
What does GetLastError return after the fail?

the SendMessage function doesn't reply any error code, only 0. If I call
GetLastError immediatly after SendMessage I get for the first call code 57
and then 6, which is ERROR_INVALID_HANDLE. But I can't believe, that I'm
using wrong handle. I've checked all window handle with Remote Spy and get
the same values, I'm using. And the message reach their destination.
Then I made a test to call SendMessage from managed to unmanaged
application. In unmanaged application WindowProc returns a value. In managed
application I'll get this value back. I made also some test with a WM_USER
message, but he same result.

I think, that somebody changes the msg.Result in
Microsoft.WindowsCE.Forms.MessageWindow.WndProc(ref Msg), when I call
base.WndProc.

By
Steffen
 
G

Guest

Hello again,
I've done a test with full framework with same result. So I think, I'm doing
something wrong. Who can figure out my error.
Here is the code from two simple WinForm applications,

Form1 overwritten WndProc
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_USER)
{
m.Result = (IntPtr) 10;
}
base.WndProc (ref m);
}

Form2 gets the hWnd from Form1 with FindWindow and sends a WM_USER message.
the result is always 0.
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hwnd = FindWindow(IntPtr.Zero,"Form1");
if (hwnd != IntPtr.Zero)
{
int result = SendMessage(hwnd,WM_USER,0,0);
this.textBox1.Text = result.ToString();
}
}

Thanks
Steffen
 
G

Guest

Hello
the problem is solved.
If I do not call base.WndProc for messages I handle in derived class
WndProc, everything works.
Steffen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top