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

How to send a structure from VB .NET to C++ with SendMessage?

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:

Expand|Select|Wrap|Line Numbers
  1. <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
  2.     Public Structure MsgSTR
  3.         Public code As String
  4.         Public index1 As Integer
  5.         Public index2 As Integer
  6.         Public arr() As Integer
  7.     End Structure
  8.  
  9. <StructLayout(LayoutKind.Sequential)> _
  10.     Public Structure CopyData
  11.         Public dwData As IntPtr
  12.         Public cbData As Integer
  13.         Public lpData As MsgSTR
  14.     End Structure
  15.  
  16. Private Declare Auto Function SendMessage Lib "user32" _
  17.      (ByVal hWnd As IntPtr, _
  18.       ByVal Msg As Integer, _
  19.       ByVal wParam As IntPtr, _
  20.       ByVal lParam As CopyData) As Boolean
  21.  
  22.     Private Declare Auto Function FindWindow Lib "user32" _
  23.      (ByVal lpClassName As String, _
  24.       ByVal lpWindowName As String) As IntPtr
  25.  
  26.     Private Const WM_COPYDATA As Integer = &H4A
  27.  
  28. Private Function mesajSMSSTR(ByVal msg As MsgSTR) As Boolean
  29.         Dim ClientWindow As IntPtr
  30.         Dim a() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
  31.  
  32.         For Each p In a
  33.             If UCase(p.ProcessName) = "SOMEPROCESS" Then
  34.                 ClientWindow = p.MainWindowHandle
  35.                 Exit For
  36.             End If
  37.         Next
  38.  
  39.         If Not ClientWindow.Equals(IntPtr.Zero) Then
  40.  
  41.             Dim message As MsgSTR = msg
  42.             Dim data As CopyData
  43.  
  44.             data.dwData = ClientWindow
  45.             data.lpData = message
  46.             data.cbData = Marshal.SizeOf(message)
  47.  
  48.             If frmTest.SendMessage(ClientWindow, frmTest.WM_COPYDATA, Me.Handle, data) then
  49.                 Return True
  50.             Else
  51.                 Return False
  52.             End If
  53.         Else
  54.             Return False
  55.         End If
  56. End Function
  57.  
  58. Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
  59.  
  60.             Dim msg As MsgSTR
  61.             msg.code = Trim(txtCode.Text)
  62.             msg.index1 = CInt(Trim(txtI1.Text))
  63.             msg.index2 = CInt(Trim(txtI2.Text))
  64.             ReDim msg.arr(40)
  65.             For i = 0 To Trim(txtArr.Text).Split(" ").Length - 1
  66.                 msg.arr(i) = CInt(Trim(txtArr.Text).Split(" ")(i))
  67.             Next
  68.  
  69.             If mesajSMSSTR(msg) Then
  70.                 MsgBox("Message sent")
  71.             Else
  72.                 MsgBox("Message not sent. Process not running.")
  73.             End If
  74.  
  75. End Sub
  76.  
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.
Nov 17 '11 #1
0 1532

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ...
2
by: Erik | last post by:
I am having trouble getting this to work so hopefully someone can provide some insite. I have a structure that I store in a CopyDataStruct which is then send to another application via...
6
by: Steve | last post by:
I am trying to use the SendMessage API function using EM_POSFROMCHAR to get the x,y location of the current character. I have declared the function and placed the call into my code. How do I...
1
by: Daniel Halan | last post by:
Hello, I want to send a SendMessage(hwnd,...) to an another application containing eighter a "String" or a PIDL... I know that the pointer lives in its own process so the other app will have...
3
by: growse | last post by:
Right, I've got a 2 c# programs here. Lets call them A and B. My aim is to send a simple string from B to A. A is always running. I've overridden the WndProc method to give me messages that are...
0
by: neonspark | last post by:
I'm buidling some simple macro functionality for my app so the users can record a sequence of keyboard inputs and replay them reliably via some menu. Originally, I used: protected override bool...
2
by: Mesan | last post by:
Hello everyone, Thanks to many useful posts in this newsgroup and others, I've been able to come very close to something I've been wanting to do for a very long time. I've figured out how to...
1
by: d-42 | last post by:
Hi, I'm pretty sure I've just got a Marshalling problem, but I'm completely stumped. If there is a better newsgroup to post this in, please point me towards it. First I'm trying to use...
3
by: michelqa | last post by:
Hi, I already post a similar question last week without success. Ok I want to get the current text selection in a RICHEDIT control.. This can be easily done in C++ with EM_EXGETSEL message. I...
0
by: dvanmil | last post by:
I am having problems with Window Messages. I want to send the handle of a VB.NET winform to a VB6 application. The code I have used seems to work ok, since the message arrives at the VB6 app....
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.