Structure Layout

  • Thread starter Thread starter wanderer
  • Start date Start date
W

wanderer

I've imported a function from a DLL that takes a custom
structure. Something like:

typedef struct _myStruct{
INT16 A;
UINT32 B;
} myStruct;

Now, I want to use the function in C#, but I don't have a
definition of the structure. Thus, I defined the
equivalent structure in my C# code. Now here's the
problem:
I know in C# I can force my managed code to lay out the
variables sequentially ( [ StructLayout(
LayoutKind.Sequential )] ), but how do I force my C++
side to do so too if I am using eVC++ 4.0 ?

It doesn't seem to recognize the '#using' command, so I
can't use:
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;
[StructLayoutAttribute(LayoutKind::Sequential)]

Thanks.
 
In CF you cannot have sequential layout. All structures use 4 byte packing.
A workaround would be to do something like this:

struct MyStruct
{
public Int16 [] data;
public Int16 A { get { return data[0]; } set { data[0] = value; } }
public UInt32 B { get { return (((UInt32)data[2]) << 16) +
(UInt32)data[1]; } set { data[1] = (Int16)(value & 0xffff); data[2] =
(Int16)(value >> 16);} }
}

Use it with your function:

f(MyStruct.data);
 
In CF you cannot have sequential layout. All structures use 4 byte
packing.
A workaround would be to do something like this:

Is that really true? The OpenNetCF code makes a lot of use of
LayoutKind.Sequential. It would explain why I get parameter errors when
calling RAPI functions.

John.

Alex Feinman said:
In CF you cannot have sequential layout. All structures use 4 byte packing.
A workaround would be to do something like this:

struct MyStruct
{
public Int16 [] data;
public Int16 A { get { return data[0]; } set { data[0] = value; } }
public UInt32 B { get { return (((UInt32)data[2]) << 16) +
(UInt32)data[1]; } set { data[1] = (Int16)(value & 0xffff); data[2] =
(Int16)(value >> 16);} }
}

Use it with your function:

f(MyStruct.data);

wanderer said:
I've imported a function from a DLL that takes a custom
structure. Something like:

typedef struct _myStruct{
INT16 A;
UINT32 B;
} myStruct;

Now, I want to use the function in C#, but I don't have a
definition of the structure. Thus, I defined the
equivalent structure in my C# code. Now here's the
problem:
I know in C# I can force my managed code to lay out the
variables sequentially ( [ StructLayout(
LayoutKind.Sequential )] ), but how do I force my C++
side to do so too if I am using eVC++ 4.0 ?

It doesn't seem to recognize the '#using' command, so I
can't use:
#using <mscorlib.dll>
#using <System.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;
using namespace System::Runtime::InteropServices;
[StructLayoutAttribute(LayoutKind::Sequential)]

Thanks.
 
Back
Top