473,387 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PostMessage/PeekMessage and interop

Hi

I'm using some of the definitions I found pinvoke.net 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
);

[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
[DllImport("user32.dll")]
[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 +
11;

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 ?
May 9 '07 #1
1 8919
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
cleaner.

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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"SeeSharp Bint" <Se******@nospam.comwrote in message
news:uW**************@TK2MSFTNGP06.phx.gbl...
Hi

I'm using some of the definitions I found pinvoke.net 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
);

[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr handle;
public uint msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}
[DllImport("user32.dll")]
[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
+ 11;

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 ?

May 9 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: James | last post by:
In my code I have a problem in abtaining a windows handle. I do not know the namespace to obtain the windows hanle. here is the code. using System; using System.Collections; using...
2
by: Jeff | last post by:
What is the .Net Framework equivalent of the Windows API function PeekMessage? Or any other method that will do the same thing. I simply need to check to see if there are messages waiting to be...
12
by: Wilfried Mestdagh | last post by:
Hi, Using P/Invoke I use PostMessage to send a message from one thread to another. I'm pretty sure this has already worked in dotnet. but I've upgraded version 2 few day ago. Now I have an...
17
by: Bonj | last post by:
Is PostQuitMessage(?) different to PostMessage(hWnd, WM_QUIT, ... ) ? I've got a window in a DLL (the same one experiencing the issue below, "return value of WM_QUIT") in which PostMessage(hWnd,...
6
by: SM | last post by:
Hello group, i'm converting a VB6 application for .NET Framework. The application depends on a DLL written in standard C. The C DLL code creates an invisible window with CreateWindow() with the...
2
by: Lenster | last post by:
When using PostMessage to post myself a message, the msg and wparam parameters somehow get swapped over. They are in the correct order when calling PostMessage but by the time wndproc handles the...
3
by: Max M. Power | last post by:
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. ...
3
by: knikkix | last post by:
Hi, I created a form in VB.Net with only one button. This is the code in button click event Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
10
by: Sergei | last post by:
Can anyone explain why PostMessage to a Windows Form is considered unsafe and raises InvalidOperationException? I can get rid of that setting CheckForIllegalCrossThreadCalls to false and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.