473,386 Members | 1,734 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.

MarshalAs COPYDATASTRUCT and SendMessage

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.

I have provided the code in hope that someone else might have some insights in this problem.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Runtime.InteropServices
  2. Imports System.Security
  3.     Public Class WindowMessages
  4.  
  5. #Region "Constants"
  6.         Const WM_COPYDATA = &H4A
  7.         Const LMEM_FIXED As Integer = &H0
  8.         Const LMEM_ZEROINIT As Integer = &H40
  9.         Const LPTR As Integer = (LMEM_FIXED Or LMEM_ZEROINIT)
  10. #End Region
  11.  
  12. #Region "Dll import"
  13.         <DllImport("user32.dll", EntryPoint:="SendMessageA", CallingConvention:=CallingConvention.StdCall, CharSet:=CharSet.Ansi)> _
  14.         Private Shared Function SendMessage(ByVal hwnd As UInteger, ByVal Msg_Const As UInteger, ByVal wParam As UInteger, ByVal lParam As UInteger) As UInteger
  15.         End Function
  16.  
  17. #End Region
  18.  
  19.         <MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst:=255)>
  20.         Private Shared text As String
  21.  
  22.         <StructLayout(LayoutKind.Sequential)>
  23.         Public Structure COPYDATASTRUCT
  24.             Public dwData As IntPtr   ' Use this to identify your message
  25.             Public cbData As Integer   ' Number of bytes to be transferred
  26.             Public lpData As IntPtr   ' Address of data
  27.         End Structure
  28.  
  29.         Public Shared Sub SendDataMsg(ByVal statusText As String, ByVal lngToHwnd As IntPtr, ByVal lngFromHwnd As IntPtr)
  30.             text = String.Copy(statusText)
  31.  
  32.             Dim oCDS As New COPYDATASTRUCT()
  33.  
  34.             oCDS.cbData = (text.Length + 1) 
  35.             oCDS.lpData = Marshal.StringToHGlobalUni(text)
  36.             oCDS.dwData = 3
  37.             Dim lParam As IntPtr = Marshal.AllocHGlobal(oCDS.cbData)
  38.             Marshal.StructureToPtr(oCDS, lParam, False)
  39.  
  40.             Call SendMessage(lngToHwnd, WM_COPYDATA, lngFromHwnd, lParam)
  41.  
  42.             Marshal.FreeHGlobal(oCDS.lpData)
  43.             Marshal.FreeHGlobal(lParam)
  44.  
  45.         End Sub
  46.  
  47.     End Class
  48.  
Oct 1 '10 #1
0 1296

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...
0
by: Augie | last post by:
Dear All, I came across this piece of code to send a message. I have this setup and running... getting all the WM_COPYDATA messages on the client side. However I can't see the contexts of the...
4
by: Sean | last post by:
I am trying to send a WM_COPYDATA message to another application in C#, ..NET 2.0. The other application receives the message, but only seems to see the first character of the string, does...
1
by: alexwhitman | last post by:
Hi, I'm trying to port a c++ class to c# and in the c++ class I have a struct defined as: struct SNARLSTRUCT { SNARL_COMMANDS cmd; long id; long timeout; long lngData2; char title;
1
by: Thomas Kehl | last post by:
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...
14
by: Kerem Gümrükcü | last post by:
Hi, i want to emulate a (synchronous) BroadCastSystemMessage with EnumWindows and SendMessage. I dont want to use the BroadcastSystemMessage because it needs the SE_TCB_NAME Privilege (you...
5
by: michelqa | last post by:
Hi, I need to call a lot of different native SendMessage to retreive informations from non managed application. Some win32 messages use struct pointer for lparam....how to create and...
1
by: xMetalDetectorx | last post by:
I have two C# Applications. The first application has to send 2 strings to the other application. The first string is just a simple string "abc", while the 2nd string contains a path...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.