473,714 Members | 3,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PostMessage + WndProc Unusual Behaviour

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 message msg is now in
the wparam position and msg is set to 0 (NULL message).

Does anyone know why this behaviour is occurring ?

I've included a simple test form below to illustrate this...
Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(80, 108)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(176, 60)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(340, 390)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region
Public Const WM_APP = &H8000
Public Const WM_MYTESTMSG = WM_APP + 1

Private Declare Function PostMessage Lib "user32" Alias "PostMessag eA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long

Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages
If m.Msg = 0 Then Console.WriteLi ne(m.ToString)
Select Case (m.Msg)
Case WM_MYTESTMSG
MsgBox("My Test Message")

Case Else
MyBase.WndProc( m)

End Select

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim lngRC As Long

lngRC = PostMessage(Me. Handle.ToInt32, WM_MYTESTMSG, 0, 0)

End Sub
End Class

Nov 21 '05 #1
2 2616
Try redefining your PostMessage function.

From the looks of it you've taken it straight out of the API text viewer for
VB6, which you can't do for VB.NET without a little conversion first.

Try it like this instead:

Private Declare Function PostMessage Lib "user32" Alias "PostMessag eA"
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer,
ByVal lParam
As Integer) As Integer

Long's in VB6 are Integers in .NET. A Long in .NET is actually a 64bit
signed Integer, which has no direct equivalent in VB6. If you're hoping to
work with structures during your calls to PostMessage, you'll need to change
the appropriate parameters from Integer to IntPtr and start using the
Marshall class. Have a read through the help on Interop for more details.

Regards,
Alex Clark
"Lenster" <Le*****@discus sions.microsoft .com> wrote in message
news:BD******** *************** ***********@mic rosoft.com...
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 message msg is now
in
the wparam position and msg is set to 0 (NULL message).

Does anyone know why this behaviour is occurring ?

I've included a simple test form below to illustrate this...
Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(80, 108)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(176, 60)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(340, 390)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region
Public Const WM_APP = &H8000
Public Const WM_MYTESTMSG = WM_APP + 1

Private Declare Function PostMessage Lib "user32" Alias "PostMessag eA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam
As Long) As Long

Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages
If m.Msg = 0 Then Console.WriteLi ne(m.ToString)
Select Case (m.Msg)
Case WM_MYTESTMSG
MsgBox("My Test Message")

Case Else
MyBase.WndProc( m)

End Select

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim lngRC As Long

lngRC = PostMessage(Me. Handle.ToInt32, WM_MYTESTMSG, 0, 0)

End Sub
End Class

Nov 21 '05 #2
Thanks Alex I'll give it a go...

"Alex Clark" wrote:
Try redefining your PostMessage function.

From the looks of it you've taken it straight out of the API text viewer for
VB6, which you can't do for VB.NET without a little conversion first.

Try it like this instead:

Private Declare Function PostMessage Lib "user32" Alias "PostMessag eA"
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer,
ByVal lParam
As Integer) As Integer

Long's in VB6 are Integers in .NET. A Long in .NET is actually a 64bit
signed Integer, which has no direct equivalent in VB6. If you're hoping to
work with structures during your calls to PostMessage, you'll need to change
the appropriate parameters from Integer to IntPtr and start using the
Marshall class. Have a read through the help on Interop for more details.

Regards,
Alex Clark
"Lenster" <Le*****@discus sions.microsoft .com> wrote in message
news:BD******** *************** ***********@mic rosoft.com...
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 message msg is now
in
the wparam position and msg is set to 0 (NULL message).

Does anyone know why this behaviour is occurring ?

I've included a simple test form below to illustrate this...
Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(80, 108)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(176, 60)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(340, 390)
Me.Controls.Add (Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)

End Sub

#End Region
Public Const WM_APP = &H8000
Public Const WM_MYTESTMSG = WM_APP + 1

Private Declare Function PostMessage Lib "user32" Alias "PostMessag eA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal
lParam
As Long) As Long

Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for operating system messages
If m.Msg = 0 Then Console.WriteLi ne(m.ToString)
Select Case (m.Msg)
Case WM_MYTESTMSG
MsgBox("My Test Message")

Case Else
MyBase.WndProc( m)

End Select

End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim lngRC As Long

lngRC = PostMessage(Me. Handle.ToInt32, WM_MYTESTMSG, 0, 0)

End Sub
End Class


Nov 21 '05 #3

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

Similar topics

15
8444
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 System.Windows.Forms; using System.Runtime.InteropServices; namespace mynamespace
1
2684
by: Srinivasa Ra via .NET 247 | last post by:
(Type your message here) I am writing an application that does lot of read/write's withcomputer's serial port. The application as a whole is workingfine. Current Approach: I have a Timer that checks once every millisecond to see if there is any data avaialable to read at theserial port. If so then reads it. The serial port is openedwithout OVERLAPPING. TRYING TO GET TO: I don't think polling serial port every millisecond is a good approach....
12
7192
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 illegal cross thread operation if posting a message. Is this a bug introduced in latest version of dotnet ? It is very legal to use postmessage because the message will execute in context of the thread the one was posted to. Same with...
17
7096
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, WM_QUIT, ... ) works, but PostQuitMessage doesn't. I would assume this to be the case as PostQuitMessage can't possibly know the hWnd I'm wanting it to operate on. So why does the docs advise you to use that?
6
4768
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 WndProc function located into the VB.NET code. Until the code was VB6/C code, they was OK. Now the VB WndProc receives the startup message correctly
3
5094
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. 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:
3
2916
by: EAdolphson | last post by:
In my ComboBox, I am trying to post the CBN_EDITCHANGE message after the CBN_SELCHANGE is received, but I am having no luck. I can trap for the CBN_SELCHANGE message, but I cannot post the CBN_EDITCHANGE message. I think my PostMessage parameters are wrong, but I cannot find what they should be. Any help would be greatly appreciated. Thanks. Here is my code. I am using VS 2003 and Visual Basic. Private Declare Function PostMessage...
3
5626
by: easoftware | last post by:
In my ComboBox, I am trying to post the CBN_EDITCHANGE message after the CBN_SELCHANGE is received, but I am having no luck. I can trap for the CBN_SELCHANGE message, but I cannot post the CBN_EDITCHANGE message. I think my PostMessage parameters are wrong, but I cannot find what they should be. Any help would be greatly appreciated. Thanks. Here is my code.
2
5517
by: alag20 | last post by:
Hi Guys, I need to send a string from c# application to another c# Application. On receiving application side the code i have is below. protected override void WndProc(ref Message m) { switch (m.Msg)
0
9316
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9077
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5959
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3160
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.