PostMessage/PeekMessage and interop


SeeSharp Bint


I'm using some of the definitions I found for PostMessage and
PeekMessage as follows

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(
HandleRef hWnd,// handle to destination window
UInt32 Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter

public struct Message
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;

[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PeekMessage(
out Message lpMsg,
HandleRef hWnd,// handle to destination window
uint wMsgFilterMin,
uint wMsgFilterMax,
uint wRemoveMsg

WM_USER = 0x0400,
public const int WM_EnteringGym = (int)WinServices.WindowsMessages.WM_USER +

I post a message as follows:

PostMessage(new HandleRef(null, _MainWindowHandle),
UserMessages.WM_EnteringGym, 1);

I peek for the message as follows:

Message msg;
PeekMessage(out msg, new HandleRef(null, this.Handle),
UserMessages.WM_EnteringGym, UserMessages.WM_EnteringGym, 1);

When it arrives, and I check msg, WParam is 0x118 and LParam is 0x00
Where am i going wrong please? Why isnt WParam 0x01 ?



Nicholas Paldino [.NET/C# MVP]

I have to ask why you are using this mechanism? There are many other
notification mechanisms in the framework other than this which are much

If I had to guess, I would say that you are doing work on another thread
and trying to notify the main window when some unit of work is done. In
this case, you should call the Invoke method on the main window, passing a
delegate to be executed on the thread that created the UI.

The only reason I can see to use this kind of code would be if you have
to send a message to another window in a different process which was
developed using unmanaged code. However, I doubt that is the case because
of your declaration of PeekMessage, which indicates you have managed code
looking for the message.

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