O
Olav Langeland
I am reading data to/from a file which contains C++ structures like this
struct StructCPP
{
long lDgType;
FILETIME ftDgTime;
char cName[128];
char cVersion[30];
char cSpare[98];
long lCount;
}
I am trying to access the data in the file using C# structures like this:
struct StructCSharp1 // structure without array(s)
{
public uint DgType;
public ulong DgTime;
public int Count;
};
struct StructCSharp2 // structure with arrays
{
public uint DgType;
public ulong DgTime;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
public byte[] Name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=30)]
public byte[] Version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=98)]
public byte[] Spare;
public int Count;
};
Trying to access the data in the structures:
....
unsafe
{
fixed (byte* pb = &pBuf[0]) // pointing to somewhere in the file
{
StructCSharp1 dg1 = new StructCSharp1(); // OK
StructCSharp2 dg2 = new StructCSharp2(); // OK
StructCSharp1 dg11 = *(StructCSharp1*)(pb); // OK
StructCSharp2 dg22 = *(StructCSharp2*)(pb);} // Fails
}
The last statement (dg 22 = ...) gives the compiler error:
"Cannot take the address or size of a variable of a managed type"
('...\StructCSharp2')
Any suggestions?
struct StructCPP
{
long lDgType;
FILETIME ftDgTime;
char cName[128];
char cVersion[30];
char cSpare[98];
long lCount;
}
I am trying to access the data in the file using C# structures like this:
struct StructCSharp1 // structure without array(s)
{
public uint DgType;
public ulong DgTime;
public int Count;
};
struct StructCSharp2 // structure with arrays
{
public uint DgType;
public ulong DgTime;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
public byte[] Name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=30)]
public byte[] Version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=98)]
public byte[] Spare;
public int Count;
};
Trying to access the data in the structures:
....
unsafe
{
fixed (byte* pb = &pBuf[0]) // pointing to somewhere in the file
{
StructCSharp1 dg1 = new StructCSharp1(); // OK
StructCSharp2 dg2 = new StructCSharp2(); // OK
StructCSharp1 dg11 = *(StructCSharp1*)(pb); // OK
StructCSharp2 dg22 = *(StructCSharp2*)(pb);} // Fails
}
The last statement (dg 22 = ...) gives the compiler error:
"Cannot take the address or size of a variable of a managed type"
('...\StructCSharp2')
Any suggestions?