C header conversion. Please help !

M

MidSilence

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/)
 
D

Dmytro Lapshyn [MVP]

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 said:
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/)
 
C

CheshireCat

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.
 
A

Andre Azevedo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top