Hello. I am trying to send a structure from VB .NET 2008 framework 3.5 to an application made in VC++ 6.0, using SendMessage and WM_COPYDATA. Here is the code:
When i execute the code above, only the string is sent. If i comment the lines related to Arr() (both in the structure and in the _click event), only the first 16 characters of that string are being sent.
For testing purposes, the receiving application uses memcopy to get any data that is sent to it and displays it in a message box.
If i put 3 strings in my structure instead, only the first is sent. I've also tried sending simple integer values, and the message gets sent, but the memory of the vc++ application remains empty and nothing is displayed in the message box.
So basically i cannot send structures nor integers. I've tried marshalling with and without SizeConst and it only seems to solve the issue with only 16 characters being shown.
I've also tried allocating memory for an integer variable, returning an IntPtr and reading the value from this address in the call to my function, but the result is the same - the value is not sent.
Anyone knows what i'm doing wrong or why these two applications cannot communicate properly? Thank you.
Code:
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure MsgSTR
Public code As String
Public index1 As Integer
Public index2 As Integer
Public arr() As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure CopyData
Public dwData As IntPtr
Public cbData As Integer
Public lpData As MsgSTR
End Structure
Private Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As CopyData) As Boolean
Private Declare Auto Function FindWindow Lib "user32" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
Private Const WM_COPYDATA As Integer = &H4A
Private Function mesajSMSSTR(ByVal msg As MsgSTR) As Boolean
Dim ClientWindow As IntPtr
Dim a() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
For Each p In a
If UCase(p.ProcessName) = "SOMEPROCESS" Then
ClientWindow = p.MainWindowHandle
Exit For
End If
Next
If Not ClientWindow.Equals(IntPtr.Zero) Then
Dim message As MsgSTR = msg
Dim data As CopyData
data.dwData = ClientWindow
data.lpData = message
data.cbData = Marshal.SizeOf(message)
If frmTest.SendMessage(ClientWindow, frmTest.WM_COPYDATA, Me.Handle, data) then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
Dim msg As MsgSTR
msg.code = Trim(txtCode.Text)
msg.index1 = CInt(Trim(txtI1.Text))
msg.index2 = CInt(Trim(txtI2.Text))
ReDim msg.arr(40)
For i = 0 To Trim(txtArr.Text).Split(" ").Length - 1
msg.arr(i) = CInt(Trim(txtArr.Text).Split(" ")(i))
Next
If mesajSMSSTR(msg) Then
MsgBox("Message sent")
Else
MsgBox("Message not sent. Process not running.")
End If
End Sub
For testing purposes, the receiving application uses memcopy to get any data that is sent to it and displays it in a message box.
If i put 3 strings in my structure instead, only the first is sent. I've also tried sending simple integer values, and the message gets sent, but the memory of the vc++ application remains empty and nothing is displayed in the message box.
So basically i cannot send structures nor integers. I've tried marshalling with and without SizeConst and it only seems to solve the issue with only 16 characters being shown.
I've also tried allocating memory for an integer variable, returning an IntPtr and reading the value from this address in the call to my function, but the result is the same - the value is not sent.
Anyone knows what i'm doing wrong or why these two applications cannot communicate properly? Thank you.