473,811 Members | 2,856 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 1553

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

Similar topics

8
6715
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ######################################################################### #! /usr/bin/env python import win32api, win32ui, win32con
2
11801
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 SendMessage. The message is arriving in the other application but when I retrieve my structure from the CopyDataStruct, the CopyDataStruct is correct, but the fields for the structure contain in CopyDataStruct.data member are garbage. The do show the...
6
4181
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 access the x,y coordinates. Some example code in C# would be greatly appreciated.
1
1689
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 problems reading it. How was it done to create the String / PIDL in a common memoryspace? What I want to do is: string szFolder = @"C:\temp\";
3
2673
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 sent to it. B is a program that loads, sends a message and then quits. Let me give you the code to B (bits are missed out, but I've got the important stuff there): private const uint WM_USER_SENDTEXT = 0x8001;
0
2750
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 ProcessCmdKey(ref Message msg, Keys keyData) To map "Keys" objects to their string constant, and stored them on a string Queue as they were happening. Then (after resetting the form), I simulate replaying of these actions by flushing the queued...
2
5507
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 create a new custom protocol handler in Windows to handle locations like "myProtocol:", which lets me have a shortcut pointing to "myProtocol:myPrimaryKey" and have my application automatically open and display the given account. That's great. I
1
2556
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 SendMessage() with WM_COPYDATA to send a string data between two instances of a program. As part of the debugging I've written two methods one where a given instance will obtain its own hWnd and send itself the message, one where it accepts
3
6156
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 really need to do the same thing in C#. How can I put the structure in memory to be able to call SendMessage and get the expected results in the structure.
0
1324
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. However, I have a problem that sometimes I get an error stating that it is not possilbe to read or write to the memory and that memory might be corrupt. I have no idea what is causing this problem as I think I release the memory at the end of my procedure....
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.