SendMessage WM_COPYDATA on 64bit System

T

Thomas Kehl

Hello.

I use the fallowing Functions to send Message from one Application to
another. This is working correct on a 32bit System. But on a 64Bit System,
the Target-Application will no received anything. Can somebody help me, what
I am doing wrong?

Private Const _messageID As Integer = -11678085939
Public Const WM_COPYDATA As Integer = &H4A
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal
lParam As COPYDATASTRUCT) As Integer

<StructLayout(LayoutKind.Sequential)> _
Private Class COPYDATASTRUCT
Public dwData As Integer = 0 '32 bit int to passed. Not used.
Public cbData As Integer = 0 'length of string. Will be one greater
because of null termination.
Public lpData As String 'string to be passed.

Public Sub New()
End Sub

Public Sub New(ByVal Data As String)
lpData = Data + Convert.ToChar(0) 'add null termination
cbData = lpData.Length 'length includes null chr so will be one
greater
End Sub
End Class

' For Sending:
Private Shared Sub SendCommandLine(ByVal hWnd As IntPtr, ByVal CommandLine
As String)
SendMessage(hWnd, WM_COPYDATA, _messageID, New
COPYDATASTRUCT(CommandLine))
End Sub

' For Receiving in the Target-Application
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = AppMessager.WM_COPYDATA Then
' ... do something ... this will no be called on a 64bit-Machine - on
32bit its working right
Return
End If
MyBase.WndProc(m)
End Sub

Thank all for their help!

Thomas
 
M

Mattias Sjögren

<StructLayout(LayoutKind.Sequential)> _
Private Class COPYDATASTRUCT
Public dwData As Integer = 0 '32 bit int to passed. Not used.
Public cbData As Integer = 0 'length of string. Will be one greater
because of null termination.
Public lpData As String 'string to be passed.

dwData must be an IntPtr.

Public Sub New()
End Sub

Public Sub New(ByVal Data As String)
lpData = Data + Convert.ToChar(0) 'add null termination

Strings are automatically null terminated so you don't really have to
do this.


Mattias
 

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