PostMessage + WndProc Unusual Behaviour

G

Guest

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.
InitializeComponent()

'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 108)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 60)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = 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 "PostMessageA"
(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.WriteLine(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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim lngRC As Long

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

End Sub
End Class
 
A

Alex Clark

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 "PostMessageA"
(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
 
G

Guest

Thanks Alex I'll give it a go...

Alex Clark said:
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 "PostMessageA"
(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
 

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