By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,528 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

SendMessage works, PostMessage doesn't work.

P: n/a
When I use the SendMessage API I can sucessfully send and receive a user
defined message.
When I use the PostMessage API I can NOT sucessfully send and receive the
same user defined message.

I've got a C# class library project with two classes:

Class 1 is derives from : System.Windows.Forms.Form and overrides the base
WndProc method for the purpose of receiving and handeling user defined
messages:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_MESSAGE1:
// do something here.
break;
case WM_MESSAGE2:
// do something here.
break;
default:
// do something here.
break;
}

Class 2 is created by Class 1 and runs as a background thread. At the
appropriate time Class 2 will either use:
SendMessage - when Class2 needs to wait for Class 1 to process the message
and possible return a response.
PostMessage - when Class2 does not need to wait for Class1 to processes
the message.

Class 2 uses this code to Send / Post the user defined messages:

[System.Runtime.InteropServices.DllImport("user32.d ll")]
private static extern Int32 SendMessage(IntPtr hWnd, Int32 msg, Int32
wParam, IntPtr lParam);

[System.Runtime.InteropServices.DllImport("user32.d ll")]
private static extern Int32 PostMessage(IntPtr hWnd, Int32 msg, Int32
wParam, IntPtr lParam);

private const Int32 WM_USER= (0x400);
private const Int32 WM_MESSAGE1 = (WM_USER + 101);
private const Int32 WM_MESSAGE2 = (WM_USER + 102);

// THIS CODE SUCCEEDS.
IntPtr lParam = System.IntPtr.Zero;
int res = SendMessage(Class1.Handle, WM_MESSAGE1, 0, lParam);

// THIS CODE FAILS.
IntPtr lParam = System.IntPtr.Zero;
PostMessage(Class1.Handle, WM_MESSAGE1, 0, lParam);

I'm logging the messages (m.Msg) that are arriving in Class1 - WndProc
function to a file for review. The log file shows that both the SendMessage
and PostMessage API calls cause the Class1 - WndProc method to fire, but in
the case of PostMessage the message that I'm looking for WM_MESSAGE1 never
arrives.

The log file also shows both the SendMessage and PostMessage cause exactly
the same additional messages to appear in the Class1 - WndProc method that I
did not explicitly issue. There are about 23 or so additional messages that
appear in the log prior to WM_MESSAGE1. But in the case of PostMessage
WM_MESSAGE1 never arrives. Just the other non-WM_MESSAGE1 messages appear in
the log.

Any idea why this might be?

Thanks in advance.
Nov 28 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
>Class 2 is created by Class 1 and runs as a background thread. At the
appropriate time Class 2 will either use:
SendMessage - when Class2 needs to wait for Class 1 to process the message
and possible return a response.
PostMessage - when Class2 does not need to wait for Class1 to processes
the message.

Any reason you're not using the Invoke and BeginInvoke methods of the
form instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 28 '05 #2

P: n/a
Thanks for the suggestion.

I'm really in need of a solution to the issue of the PostMessage command not
working as it should.

Does anyone have any idea why SendMessage would work, but PostMessage doesn't?

Thanks again.

"Mattias Sjögren" wrote:
Class 2 is created by Class 1 and runs as a background thread. At the
appropriate time Class 2 will either use:
SendMessage - when Class2 needs to wait for Class 1 to process the message
and possible return a response.
PostMessage - when Class2 does not need to wait for Class1 to processes
the message.

Any reason you're not using the Invoke and BeginInvoke methods of the
form instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 29 '05 #3

P: n/a
>I'm really in need of a solution to the issue of the PostMessage command not
working as it should.

Does anyone have any idea why SendMessage would work, but PostMessage doesn't?


Can you post a complete sample that will let us reproduce the behavior
you're seeing?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 29 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.