473,473 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Marshalling Structure to Byte

1 New Member
When marshalling structure from C to .NET, I get the
following error:
System.ArgumentException: Type could not be marshaled because the
length of an embedded array instance does not match the declared
length in the layout.
//////////////
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] 
  2.         ref struct MyStruct
  3.         {
  4.     [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )]
  5.             array<Byte>^data;
  6.         };
  7. /////////////////////////////////
  8.     array<Byte> ^Serialize(MyStruct ^Struct)
  9.         {
  10.     int len=Marshal::SizeOf(Struct);
  11.     IntPtr ptr=Marshal::AllocHGlobal(len);
  12.     array<Byte>^ rawdatas = gcnew array<Byte>(len);
  13.     Marshal::StructureToPtr(Struct,ptr,false);            
  14.     Marshal::Copy( ptr, rawdatas, 0, len ); 
  15.     Marshal::FreeHGlobal( ptr );     
  16.     return rawdatas; 
  17.         }
  18. /////////////////////////////
  19.  MyStruct ^my=gcnew MyStruct;
  20.  my->data=Encoding::Unicode->GetBytes("Example");
  21.     Serialize(my);
Mar 8 '08 #1
1 2793
Frinavale
9,735 Recognized Expert Moderator Expert
When marshalling structure from C to .NET, I get the
following error:
System.ArgumentException: Type could not be marshaled because the
length of an embedded array instance does not match the declared
length in the layout.
//////////////
Expand|Select|Wrap|Line Numbers
  1. [StructLayout(LayoutKind::Sequential, Pack=1, CharSet=CharSet::Ansi)] 
  2.         ref struct MyStruct
  3.         {
  4.     [MarshalAs(UnmanagedType::ByValArray, SizeConst=1024 )]
  5.             array<Byte>^data;
  6.         };
  7. /////////////////////////////////
  8.     array<Byte> ^Serialize(MyStruct ^Struct)
  9.         {
  10.     int len=Marshal::SizeOf(Struct);
  11.     IntPtr ptr=Marshal::AllocHGlobal(len);
  12.     array<Byte>^ rawdatas = gcnew array<Byte>(len);
  13.     Marshal::StructureToPtr(Struct,ptr,false);            
  14.     Marshal::Copy( ptr, rawdatas, 0, len ); 
  15.     Marshal::FreeHGlobal( ptr );     
  16.     return rawdatas; 
  17.         }
  18. /////////////////////////////
  19.  MyStruct ^my=gcnew MyStruct;
  20.  my->data=Encoding::Unicode->GetBytes("Example");
  21.     Serialize(my);
When passing arrays between .NET and C code, you have to make sure that the data type and the array lengths are the same.
Mar 10 '08 #2

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

Similar topics

7
by: Mick | last post by:
Does anyone know how to pass an array of structures to a DLL? Here's what I'm doing... the VB.Net error is at the end. *** C++ Structure Declaration: typedef struct _SOME_STRUCT { char...
1
by: Betty | last post by:
When marshalling a char array in a structure from C to .NET, I get the following error: System.ArgumentException: Type could not be marshaled because the length of an embedded array instance does...
2
by: allen | last post by:
I have an async socket callback for a UDP socket. When the callback is fired after receiving a packet, I want to map the packet to a structure I have. note, all unsafe code blocks here. //...
1
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
1
by: Me | last post by:
I'm trying to get a structure into a byte array. I can't seem to figure out how to get a non-fixed length null-terminated string into the array (without rolling my own logic). For example, a...
3
by: Markus Stiller | last post by:
Could someone give me an hint? I have trouble with mashaling of a struct. I can set values in struct (byVAL) but cant read from the struct. Its an long pointer struct in a c dll. thats my...
2
by: Howard Kaikow | last post by:
I've got the following in a VB 6 project: Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long...
0
by: PickwickBob3 | last post by:
Can you suggest how to marshall the bRawData in the following structure?? /* * Raw format of the input from Human Input Devices */ typedef struct tagRAWHID { DWORD dwSizeHid; // byte size of...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
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...
1
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...
0
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...
0
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.