473,385 Members | 1,676 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.

Union Structure Marshaling

THANK YOU 1,000 fold if anyone can help me in this Marshalling:

C structure
===========
struct mystruct {
char tag;
char field1;
char field2;
char field3;
long field4;
union struct2 tagData;
}

union struct2 {
struct struct3 union1;
struct struct4 union2;
}

struct struct3 {
long id;
char flags;
char size;
char data[MAX_SIZE];
}
struct struct4 {
char byte1;
char byte2;
char byte3;
}

C# Attempt
==========
[StructLayout (LayoutKind.Explicit, Pack=1, CharSet=CharSet.Ansi)]
public struct oldStruct
{
[FieldOffset (0)] public byte tag;
[FieldOffset (1)] public byte field1;
[FieldOffset (2)] public byte field2;
[FieldOffset (3)] public byte field3;
[FieldOffset (4)] public uint field4;

/* union members */
// VRcvMsg
[FieldOffset (8) ] public uint id;
[FieldOffset (12)] public byte flags;
[FieldOffset (13)] public byte dlc;
[FieldOffset (14)] public byte [] data;
// union2
[FieldOffset (8) ] public byte byte1;
[FieldOffset (9) ] public byte byte2;
[FieldOffset (10)] public byte byte3;
}
Although this compiles, It cannot instantiate oldStruct because the
data is not aligned properly. I've thought of just putting MAX_SIZE
data parameters and brute force but.....

THANKS to anyone out there
Nov 16 '05 #1
3 3648
Hi, Jeffrey.
Try the following declaration for the oldStruct (change 100 to MAX_SIZE
defined in your C code):

[StructLayout (LayoutKind.Explicit, Pack=1, CharSet=CharSet.Ansi)]
public struct oldStruct
{
[FieldOffset (0)] public byte tag;
[FieldOffset (1)] public byte field1;
[FieldOffset (2)] public byte field2;
[FieldOffset (3)] public byte field3;
[FieldOffset (4)] public uint field4;

/* union members */
// VRcvMsg
[FieldOffset (8) ] public uint id;
[FieldOffset (12)] public byte flags;
[FieldOffset (13)] public byte dlc;
[FieldOffset (14), MarshalAs(UnmanagedType.ByValArray, SizeConst=100)]
public byte [] data;
// union2
[FieldOffset (8) ] public byte byte1;
[FieldOffset (9) ] public byte byte2;
[FieldOffset (10)] public byte byte3;
}

Regards
Ming Chen

"Jeffrey B. Holtz" <jh****@accuratetechnologies.com> wrote in message
news:93*************************@posting.google.co m...
THANK YOU 1,000 fold if anyone can help me in this Marshalling:

C structure
===========
struct mystruct {
char tag;
char field1;
char field2;
char field3;
long field4;
union struct2 tagData;
}

union struct2 {
struct struct3 union1;
struct struct4 union2;
}

struct struct3 {
long id;
char flags;
char size;
char data[MAX_SIZE];
}
struct struct4 {
char byte1;
char byte2;
char byte3;
}

C# Attempt
==========
[StructLayout (LayoutKind.Explicit, Pack=1, CharSet=CharSet.Ansi)]
public struct oldStruct
{
[FieldOffset (0)] public byte tag;
[FieldOffset (1)] public byte field1;
[FieldOffset (2)] public byte field2;
[FieldOffset (3)] public byte field3;
[FieldOffset (4)] public uint field4;

/* union members */
// VRcvMsg
[FieldOffset (8) ] public uint id;
[FieldOffset (12)] public byte flags;
[FieldOffset (13)] public byte dlc;
[FieldOffset (14)] public byte [] data;
// union2
[FieldOffset (8) ] public byte byte1;
[FieldOffset (9) ] public byte byte2;
[FieldOffset (10)] public byte byte3;
}
Although this compiles, It cannot instantiate oldStruct because the
data is not aligned properly. I've thought of just putting MAX_SIZE
data parameters and brute force but.....

THANKS to anyone out there

Nov 16 '05 #2
Thanks for the response but that's what I tried to begin with.
The response that I get durring runtime is:

An unhandled exception of type 'System.TypeLoadException' occurred in
CANdotNET.exe

Additional information: Could not load type oldVEvent2 from assembly
CANdotNET, Version=1.0.1728.30204, Culture=neutral, PublicKeyToken=null
because it contains an object field at offset 14 that is incorrectly
aligned or overlapped by a non-object field.
The P/Invoke function is:
=========================
[DllImport("Unmanaged.dll")]
private static extern short csFunc(int nHandle, out oldStruct oStruct);

And in the Unmanaged dll I have:
UNAMANGED_API short csFunc(long nHandle, mystruct *pStruct);

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Hi, Jeffrey.
Didn't notice the error message last time. :)
Try this:

[StructLayout (LayoutKind.Explicit, Pack=1, CharSet=CharSet.Ansi)]
public struct oldStruct
{
[FieldOffset (0)] public byte tag;
[FieldOffset (1)] public byte field1;
[FieldOffset (2)] public byte field2;
[FieldOffset (3)] public byte field3;
[FieldOffset (4)] public uint field4;

/* union members */
// VRcvMsg
[FieldOffset (8) ] public uint id;
[FieldOffset (12)] public byte flags;
[FieldOffset (13)] public byte dlc;
[FieldOffset (14), MarshalAs(UnmanagedType.ByValArray, SizeConst=100)]
public IntPtr data;

// union2
[FieldOffset (8) ] public byte byte1;
[FieldOffset (9) ] public byte byte2;
[FieldOffset (10)] public byte byte3;
}

To allocate the memory for data, use Marshal.AllocHGlobal. And then use
Marshal.FreeHGlobal to free it.

Regards
Ming Chen

"Jeffrey Holtz" <jh****@accuratetechnologies.com> wrote in message
news:OA**************@TK2MSFTNGP10.phx.gbl...
Thanks for the response but that's what I tried to begin with.
The response that I get durring runtime is:

An unhandled exception of type 'System.TypeLoadException' occurred in
CANdotNET.exe

Additional information: Could not load type oldVEvent2 from assembly
CANdotNET, Version=1.0.1728.30204, Culture=neutral, PublicKeyToken=null
because it contains an object field at offset 14 that is incorrectly
aligned or overlapped by a non-object field.
The P/Invoke function is:
=========================
[DllImport("Unmanaged.dll")]
private static extern short csFunc(int nHandle, out oldStruct oStruct);

And in the Unmanaged dll I have:
UNAMANGED_API short csFunc(long nHandle, mystruct *pStruct);

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #4

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

Similar topics

6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
2
by: Pas de Spam | last post by:
Hi, I have some problem in marshaling array of structure. I have to exchange a structure of data with a C++ program. This structure data contains a field which is an array of a custom type. I...
3
by: Sin | last post by:
Hello everybody, I'm currently trying to understand how marshaling can use used for accessing Win32 API functions as well as custom C/C++ code we design which exposes functions the same way as...
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
11
by: Lance | last post by:
Hi all, I've got a some structures defined as ////// <StructLayout(LayoutKind.Sequential)Public Structure GM_LayerInfo_t Public mDescription As String Public mNativeRect As GM_Rectangle_t...
0
by: swts | last post by:
hi, the following marshaling code gives me an errror of "type packet cannot be marshaled as an unmanaged structure; no meaningful size or offset can be managed". public static byte...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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.