473,401 Members | 2,146 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,401 software developers and data experts.

C header conversion. Please help !

Hi all,

I'm trying to convert a long c header file with a lot of structs to c# and
I'm getting the following error:

An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.
Additional information: Could not load type
.....because it contains an object field at offset 0 that is incorrectly
aligned or overlapped by a non-object field.

There's a lot of others structs but it's very similar with the following
ones. I also post my C# conversion and if I comment all the string fields in
the structures it works fine.

TIA,

--
Andre

-------------------------------------------------------
// Win32 Specific definitions for Windows/NT 3.5
#pragma pack(8)
typedef unsigned long Handle_t;
typedef unsigned char Boolean;
#define _Int int

typedef unsigned long InvokeID_t;
typedef char DeviceID_t[64];
typedef long RoutingCrossRefID_t;
typedef long RouteRegisterReqID_t;

typedef struct
{

InvokeID_t invokeID;
union
{
CSTARouteRequestEvent_t routeRequest;
CSTARouteRequestExtEvent_t routeRequestExt;
CSTAReRouteRequest_t reRouteRequest;
CSTAEscapeSvcReqEvent_t escapeSvcReqeust;
CSTASysStatReqEvent_t sysStatRequest;
} u;

} CSTARequestEvent;

typedef struct CSTARouteRequestEvent_t {
RouteRegisterReqID_t routeRegisterReqID;
RoutingCrossRefID_t routingCrossRefID;
DeviceID_t currentRoute;
DeviceID_t callingDevice;
ConnectionID_t routedCall;
SelectValue_t routedSelAlgorithm;
Boolean priority;
SetUpValues_t setupInformation;
} CSTARouteRequestEvent_t;

typedef struct ConnectionID_t {
long callID;
DeviceID_t deviceID;
ConnectionID_Device_t devIDType;
} ConnectionID_t;

typedef struct Connection_t {
ConnectionID_t party;
SubjectDeviceID_t staticDevice;
} Connection_t;

typedef struct ConnectionList_t {
_Int count;
Connection_t FAR *connection;
} ConnectionList_t;
--------------------------------------------------
C# Conversion

-----------------------------------------------------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8)]
public struct cstaRequestEvent
{
public uint invokeId;
public cstaRequestEventDescription requestEventDescription;
}

[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Ansi, Pack=8)]
public struct cstaRequestEventDescription
{
[FieldOffset(0)] public cstaRouteRequestEvent cstaRouteRequest;
[FieldOffset(0)] public cstaRouteRequestExtEvent cstaRouteRequestExt;
[FieldOffset(0)] public cstaReRouteRequestType cstaReRouteRequest;
[FieldOffset(0)] public cstaEscapeSvcReqEvent cstaEscapeSvcReq;
[FieldOffset(0)] public cstaSysStatReqEvent cstaSysStatReq;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8 )]
public struct cstaRouteRequestEvent
{
public int routeRegisterReqID;
public int routingCrossRefID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string currentRoute;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string callingDevice;
public ConnectionID routedCall;
public SelectValue routedSelAlgorithm;
[MarshalAs(UnmanagedType.Bool)]
public bool priority;
public SetUpValues setupInformation;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8 )]
public struct ConnectionID
{
public uint callID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string deviceID;
public ConnectionID_Device devIDType;
}

[StructLayout(LayoutKind.Sequential )]
public struct ConnectionList
{
public int count;
//public System.IntPtr PConnection; // Pointer to Connection -------->
Is this right ?
}


User submitted from AEWNET (http://www.aewnet.com/)
Nov 17 '05 #1
5 2222
Hi MidSilence,

Unions are a C feature which has very limited or even no support in C#.
While you can probably force several fields to have the same byte offset, it
can cause you nasty problems in the future.
Judging by the compiler's message, you cannot use the same field offset for
variables of reference types. I'd just declare a single field of type
'object' instead of the whole union and then upcast that object to an
appropriate type depending on the invokeID value.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"MidSilence" <MidSilence@aew_nospam.com> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
Hi all,

I'm trying to convert a long c header file with a lot of structs to c# and
I'm getting the following error:

An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.
Additional information: Could not load type
....because it contains an object field at offset 0 that is incorrectly
aligned or overlapped by a non-object field.

There's a lot of others structs but it's very similar with the following
ones. I also post my C# conversion and if I comment all the string fields
in
the structures it works fine.

TIA,

--
Andre

-------------------------------------------------------
// Win32 Specific definitions for Windows/NT 3.5
#pragma pack(8)
typedef unsigned long Handle_t;
typedef unsigned char Boolean;
#define _Int int

typedef unsigned long InvokeID_t;
typedef char DeviceID_t[64];
typedef long RoutingCrossRefID_t;
typedef long RouteRegisterReqID_t;

typedef struct
{

InvokeID_t invokeID;
union
{
CSTARouteRequestEvent_t routeRequest;
CSTARouteRequestExtEvent_t routeRequestExt;
CSTAReRouteRequest_t reRouteRequest;
CSTAEscapeSvcReqEvent_t escapeSvcReqeust;
CSTASysStatReqEvent_t sysStatRequest;
} u;

} CSTARequestEvent;

typedef struct CSTARouteRequestEvent_t {
RouteRegisterReqID_t routeRegisterReqID;
RoutingCrossRefID_t routingCrossRefID;
DeviceID_t currentRoute;
DeviceID_t callingDevice;
ConnectionID_t routedCall;
SelectValue_t routedSelAlgorithm;
Boolean priority;
SetUpValues_t setupInformation;
} CSTARouteRequestEvent_t;

typedef struct ConnectionID_t {
long callID;
DeviceID_t deviceID;
ConnectionID_Device_t devIDType;
} ConnectionID_t;

typedef struct Connection_t {
ConnectionID_t party;
SubjectDeviceID_t staticDevice;
} Connection_t;

typedef struct ConnectionList_t {
_Int count;
Connection_t FAR *connection;
} ConnectionList_t;
--------------------------------------------------
C# Conversion

-----------------------------------------------------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8)]
public struct cstaRequestEvent
{
public uint invokeId;
public cstaRequestEventDescription requestEventDescription;
}

[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Ansi, Pack=8)]
public struct cstaRequestEventDescription
{
[FieldOffset(0)] public cstaRouteRequestEvent cstaRouteRequest;
[FieldOffset(0)] public cstaRouteRequestExtEvent cstaRouteRequestExt;
[FieldOffset(0)] public cstaReRouteRequestType cstaReRouteRequest;
[FieldOffset(0)] public cstaEscapeSvcReqEvent cstaEscapeSvcReq;
[FieldOffset(0)] public cstaSysStatReqEvent cstaSysStatReq;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8 )]
public struct cstaRouteRequestEvent
{
public int routeRegisterReqID;
public int routingCrossRefID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string currentRoute;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string callingDevice;
public ConnectionID routedCall;
public SelectValue routedSelAlgorithm;
[MarshalAs(UnmanagedType.Bool)]
public bool priority;
public SetUpValues setupInformation;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8 )]
public struct ConnectionID
{
public uint callID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string deviceID;
public ConnectionID_Device devIDType;
}

[StructLayout(LayoutKind.Sequential )]
public struct ConnectionList
{
public int count;
//public System.IntPtr PConnection; // Pointer to Connection -------->
Is this right ?
}


User submitted from AEWNET (http://www.aewnet.com/)


Nov 17 '05 #2
Hi MidSilence,

Unions are a C feature which has very limited or even no support in C#.
While you can probably force several fields to have the same byte offset, it
can cause you nasty problems in the future.
Judging by the compiler's message, you cannot use the same field offset for
variables of reference types. I'd just declare a single field of type
'object' instead of the whole union and then upcast that object to an
appropriate type depending on the invokeID value.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"MidSilence" <MidSilence@aew_nospam.com> wrote in message
news:OT**************@TK2MSFTNGP12.phx.gbl...
Hi all,

I'm trying to convert a long c header file with a lot of structs to c# and
I'm getting the following error:

An unhandled exception of type 'System.TypeLoadException' occurred in
Unknown Module.
Additional information: Could not load type
....because it contains an object field at offset 0 that is incorrectly
aligned or overlapped by a non-object field.

There's a lot of others structs but it's very similar with the following
ones. I also post my C# conversion and if I comment all the string fields
in
the structures it works fine.

TIA,

--
Andre

-------------------------------------------------------
// Win32 Specific definitions for Windows/NT 3.5
#pragma pack(8)
typedef unsigned long Handle_t;
typedef unsigned char Boolean;
#define _Int int

typedef unsigned long InvokeID_t;
typedef char DeviceID_t[64];
typedef long RoutingCrossRefID_t;
typedef long RouteRegisterReqID_t;

typedef struct
{

InvokeID_t invokeID;
union
{
CSTARouteRequestEvent_t routeRequest;
CSTARouteRequestExtEvent_t routeRequestExt;
CSTAReRouteRequest_t reRouteRequest;
CSTAEscapeSvcReqEvent_t escapeSvcReqeust;
CSTASysStatReqEvent_t sysStatRequest;
} u;

} CSTARequestEvent;

typedef struct CSTARouteRequestEvent_t {
RouteRegisterReqID_t routeRegisterReqID;
RoutingCrossRefID_t routingCrossRefID;
DeviceID_t currentRoute;
DeviceID_t callingDevice;
ConnectionID_t routedCall;
SelectValue_t routedSelAlgorithm;
Boolean priority;
SetUpValues_t setupInformation;
} CSTARouteRequestEvent_t;

typedef struct ConnectionID_t {
long callID;
DeviceID_t deviceID;
ConnectionID_Device_t devIDType;
} ConnectionID_t;

typedef struct Connection_t {
ConnectionID_t party;
SubjectDeviceID_t staticDevice;
} Connection_t;

typedef struct ConnectionList_t {
_Int count;
Connection_t FAR *connection;
} ConnectionList_t;
--------------------------------------------------
C# Conversion

-----------------------------------------------------------
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8)]
public struct cstaRequestEvent
{
public uint invokeId;
public cstaRequestEventDescription requestEventDescription;
}

[StructLayout(LayoutKind.Explicit, CharSet=CharSet.Ansi, Pack=8)]
public struct cstaRequestEventDescription
{
[FieldOffset(0)] public cstaRouteRequestEvent cstaRouteRequest;
[FieldOffset(0)] public cstaRouteRequestExtEvent cstaRouteRequestExt;
[FieldOffset(0)] public cstaReRouteRequestType cstaReRouteRequest;
[FieldOffset(0)] public cstaEscapeSvcReqEvent cstaEscapeSvcReq;
[FieldOffset(0)] public cstaSysStatReqEvent cstaSysStatReq;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8 )]
public struct cstaRouteRequestEvent
{
public int routeRegisterReqID;
public int routingCrossRefID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string currentRoute;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string callingDevice;
public ConnectionID routedCall;
public SelectValue routedSelAlgorithm;
[MarshalAs(UnmanagedType.Bool)]
public bool priority;
public SetUpValues setupInformation;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=8 )]
public struct ConnectionID
{
public uint callID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
public string deviceID;
public ConnectionID_Device devIDType;
}

[StructLayout(LayoutKind.Sequential )]
public struct ConnectionList
{
public int count;
//public System.IntPtr PConnection; // Pointer to Connection -------->
Is this right ?
}


User submitted from AEWNET (http://www.aewnet.com/)


Nov 17 '05 #3
If you're passing records to someone else's unmanaged dlls, and you have no
joy trying to do it as you are at the moment, you always have the option of
creating an array of bytes of the correct size, wrapping them in a c# object
and fill/empty the byte array as you need it. You'll have to rewrite
definitions for the dll functions to pass byte[] rather than pointers to
structs. Pass the byte array to the dll. Ive had to do that where Ive needed
to pass structs that can vary in size because c# just can't handle them.
Nov 17 '05 #4
If you're passing records to someone else's unmanaged dlls, and you have no
joy trying to do it as you are at the moment, you always have the option of
creating an array of bytes of the correct size, wrapping them in a c# object
and fill/empty the byte array as you need it. You'll have to rewrite
definitions for the dll functions to pass byte[] rather than pointers to
structs. Pass the byte array to the dll. Ive had to do that where Ive needed
to pass structs that can vary in size because c# just can't handle them.
Nov 17 '05 #5
Hi Mr Lapshyn,

I've tried to use a single object type field but doesn't work because when I
pass the structure to dll function I'got an ExecuteEngineException.
I'm passing the structure with ref clause in function signature.

"Dmytro Lapshyn [MVP]" <x-****@no-spam-please.hotpop.com> wrote in message
news:eO**************@TK2MSFTNGP10.phx.gbl...
Hi MidSilence,

Unions are a C feature which has very limited or even no support in C#.
While you can probably force several fields to have the same byte offset,
it can cause you nasty problems in the future.
Judging by the compiler's message, you cannot use the same field offset
for variables of reference types. I'd just declare a single field of type
'object' instead of the whole union and then upcast that object to an
appropriate type depending on the invokeID value.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]

Nov 17 '05 #6

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

Similar topics

9
by: soni29 | last post by:
hi, i have written the following code, still in the learning stage: #include<iostream.h> class CBox { public: // Constructor definition CBox(double lv, double bv = 1.0, double hv = 1.0) :...
34
by: E. Robert Tisdale | last post by:
Please find attached the physical constants header file physical.h It defines conversion factors to mks units. It might be used like this: > cat main.cc #include<iostream>...
0
by: Andre Azevedo | last post by:
Hi all, I'm trying to convert a long c header file with a lot of structs to c# and I'm getting the following error: An unhandled exception of type 'System.TypeLoadException' occurred in...
0
by: MidSilence | last post by:
Hi all, I'm trying to convert a long c header file with a lot of structs to c# and I'm getting the following error: An unhandled exception of type 'System.TypeLoadException' occurred in...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: DCC700 | last post by:
VS 2005 Converted Header causing error when publishing After converting from Visual Studio 2003 to 2005, I have had several issues with a header that is used throughout the project. The...
10
by: Scoots | last post by:
I have the following code snippit that the compiler just won't take (vc ++ 6). I want to make a map as a member variable inside a class. So I've put it into the header file, and it won't take it...
5
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I have a sub in vb.net that adds extra headers to a gridview and it works very well. however, i tried to translate it to c# and i'm getting the header inserting itself over the first datarows and...
4
by: quophyie | last post by:
Hi guys I''m a new C++ programmer and I am having a few problems with some structs that I have defined in my header file and want to use in my CPP file. The struct called "deck" is defined in a...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
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...

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.