G
Guest
Dear All
I have this struct
[StructLayout(LayoutKind.Sequential)]
public struct biodata
{
public ushort weight;
public ushort lenght;
public ushort height;
public uint time;
}
Which I’m trying to pass as an array into this unmanaged function:
[DllImport("unmanaged.dll")]
public static extern void BFillArray (biodata [] aBio, ushort aMaxCount, out
ushort aCount);
By using this call:
biodata [] mRec = new biodata [1500];
ushort mCount = 0;
BFillArray (mRec,1500,out mCount);
Now in the unmanaged dll function BFillArray this happens:
typedef struct _ biodata {
unsigned short weight;
unsigned short lenght;
unsigned short height;
unsigned int time;
} biodata ;
UNMANAGED_API void BFillArray (biodata * aBio, const unsigned short
aMaxCount, unsigned short &aCount)
{
for (aCount = 0; aCount < aMaxCount; aCount ++)
{
aBio [aNo].time = aNo;
aBio [aNo].height = aNo;
aBio [aNo].lenght = aNo;
aBio [aNo].weight = aNo;
}
}
Note also that the “Struct Member Alignment†in the unmanaged project is set
to 1 byte, providing no padding between the values.
So far so good, but when looking into the array returned from the BFillArray
call in the managed code, the entire dataset is corrupted and seems more
like being misaligned. Now the obvious question I’m trying to solve is WHY?
Any suggestions?
Best regards
Eskild
I have this struct
[StructLayout(LayoutKind.Sequential)]
public struct biodata
{
public ushort weight;
public ushort lenght;
public ushort height;
public uint time;
}
Which I’m trying to pass as an array into this unmanaged function:
[DllImport("unmanaged.dll")]
public static extern void BFillArray (biodata [] aBio, ushort aMaxCount, out
ushort aCount);
By using this call:
biodata [] mRec = new biodata [1500];
ushort mCount = 0;
BFillArray (mRec,1500,out mCount);
Now in the unmanaged dll function BFillArray this happens:
typedef struct _ biodata {
unsigned short weight;
unsigned short lenght;
unsigned short height;
unsigned int time;
} biodata ;
UNMANAGED_API void BFillArray (biodata * aBio, const unsigned short
aMaxCount, unsigned short &aCount)
{
for (aCount = 0; aCount < aMaxCount; aCount ++)
{
aBio [aNo].time = aNo;
aBio [aNo].height = aNo;
aBio [aNo].lenght = aNo;
aBio [aNo].weight = aNo;
}
}
Note also that the “Struct Member Alignment†in the unmanaged project is set
to 1 byte, providing no padding between the values.
So far so good, but when looking into the array returned from the BFillArray
call in the managed code, the entire dataset is corrupted and seems more
like being misaligned. Now the obvious question I’m trying to solve is WHY?
Any suggestions?
Best regards
Eskild