473,386 Members | 1,694 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,386 software developers and data experts.

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.
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

Nov 21 '05 #1
2 2604
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
"Lenster" <Le*****@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.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.
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

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 "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
"Lenster" <Le*****@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.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.
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


Nov 21 '05 #3

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...
1
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...
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...
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: 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...
3
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...
2
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) ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.