473,657 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MarshaAs COPYDATASTRUCT and SendMessage

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 correct field sizes but they do not
contain the strings that I added.

The application that I'm sending to is compiled using MBCS.

Thanks in advance!
Erik
Here is my DLLImport statement:
[DllImport("user 32.dll", CharSet=CharSet .Ansi)]
public static extern int SendMessage(Int Ptr hWnd, int msg, IntPtr
wParam, IntPtr lParam);

This is the CopyDataStruct structure:
[StructLayout(La youtKind.Sequen tial)]
public struct CopyDataStruct
{
public int dwData;

public int cbData;

public IntPtr data;
}

This is the structure that is stored in the CopyDataStruct. data
member:
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi]
public struct MyInfo
{
[MarshalAs(Unman agedType.LPStr, SizeConst=100)]
public string mystring1;

[MarshalAs(Unman agedType.LPStr, SizeConst=30)]
public string mystring2;

[MarshalAs(Unman agedType.LPStr, SizeConst=30)]
public string mystring3;

[MarshalAs(Unman agedType.LPStr, SizeConst=64)]
public string mystring4;

public int somenumber;
}
This is the orginal C++ struct for MyInfo:
typedef struct {
TCHAR mystring1[100];
TCHAR mystring2[30];
TCHAR mystring3[30];
TCHAR mystring4[64];
DWORD somenumber;
} MYINFO_STRUCT;
Here is how I'm creating the structures and sending the message:

Win32.MyInfo li;
li.mystring1 = "string1";
li.mystring2 = "string2";
li.mystring3 = "string3";
li.mystring4 = "string4";
li.somenumber = 34;

// Allocate memory for the MYINFO struct and
// copy the contents of our struct to this memory
IntPtr liMemory = Marshal.AllocHG lobal(Marshal.S izeOf(typeof(Wi n32.LogonInfo)) );

Marshal.Structu reToPtr(li, liMemory, false);

Win32.CopyDataS truct cds;
cds.dwData = 123;
cds.cbData = Marshal.SizeOf( typeof(Win32.My Info));
cds.data = liMemory;

// Allocate memory for the COPYDATA struct and
// copy the contents of our struct to this memory
IntPtr cdsMemory = Marshal.AllocHG lobal(Marshal.S izeOf(typeof(Wi n32.CopyDataStr uct)));

Marshal.Structu reToPtr(cds, cdsMemory, false);

// Send the actual Win32 message
Win32.SendMessa ge(apptosendto. MainWindowHandl e, Win32.WM_COPYDA TA,
IntPtr.Zero, cdsMemory);

// Free allocated memory
Marshal.FreeHGl obal(liMemory);
Marshal.FreeHGl obal(cdsMemory) ;
Nov 15 '05 #1
2 11790
Erik,
This is the structure that is stored in the CopyDataStruct. data
member:
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi]
public struct MyInfo
{
[MarshalAs(Unman agedType.LPStr, SizeConst=100)]
public string mystring1;

[MarshalAs(Unman agedType.LPStr, SizeConst=30)]
public string mystring2;

[MarshalAs(Unman agedType.LPStr, SizeConst=30)]
public string mystring3;

[MarshalAs(Unman agedType.LPStr, SizeConst=64)]
public string mystring4;

public int somenumber;
}

You should use UnmanagedType.B yValTStr instead of .LPStr to match the
C++ struct.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message news:<OJ******* ******@TK2MSFTN GP11.phx.gbl>.. .
Erik,
This is the structure that is stored in the CopyDataStruct. data
member:
[StructLayout(La youtKind.Sequen tial, CharSet=CharSet .Ansi]
public struct MyInfo
{
[MarshalAs(Unman agedType.LPStr, SizeConst=100)]
public string mystring1;

[MarshalAs(Unman agedType.LPStr, SizeConst=30)]
public string mystring2;

[MarshalAs(Unman agedType.LPStr, SizeConst=30)]
public string mystring3;

[MarshalAs(Unman agedType.LPStr, SizeConst=64)]
public string mystring4;

public int somenumber;
}

You should use UnmanagedType.B yValTStr instead of .LPStr to match the
C++ struct.

Mattias

Thanks Mattias!

That was the problem...it is working great now!
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10157
by: JSK | last post by:
Hi, As any one worked in VB.NET and made use of Sendmessage API? The Issue I am running into is how to pass pointers of Data Structures (UDT) to the SendMessage. I starting looking at "IntPrt" as a data type but it appears to fall short of using pointer types. Any help or insight would be greatly appreciated. Thank You
22
9238
by: SQACSharp | last post by:
I'm trying to get the control name of an editbox in another window. The following code set the value "MyPassword" in the password EditBox but it fail to return the control name of the EditBox. I'm sure the problem is the way i'm using the sendmessage API, the return string and the lParam return 0....is anybody have a clue? any sendmessage api expert here? public static extern Int32 FindWindow(String lpClassName,String
1
3541
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
6283
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 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"
1
13404
by: Necromis | last post by:
Ok, I have gotten my head around things better regarding SendMessage and FindWindow functions. However, I am running into an issue with my code still. The program I am working with is EXTRA! by Attachmate. It is a mainframe terminal. Here is the issue and strange part of it. It is accepting sendmessage when I use the WM_KEYDOWN/UP commands. However, when I use WM_SETTEXT to send my string it is not imputing the string to the session...
14
12632
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 dont have this in normal). So i decided to use the EnumWindows with SendMessage. What i try here is to pass a WM_USER+1111 with LParam and WParam, pointing to strings. But when i try to get the strings at the other end with Marshal.PtrToStringAnsi,
5
5231
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 marshaling the struct to be able to use it in sendmessage... Here is an example LM_GETITEM: http://msdn.microsoft.com/en-us/library/bb760720(VS.85).aspx
1
12817
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 (Application.StartupPath). This is the code I use to send the data: //Used for WM_COPYDATA for string messages public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; ...
2
5509
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) { switch (m.Msg)
0
8315
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
8829
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
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
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
7341
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...
0
4164
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...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1627
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.