473,325 Members | 2,712 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,325 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 2220
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.