Marshalling Double Array from C# to C DLL

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, we marshalled the followoing C# struct into C struct and the codes runs okey.
C# code
<div style="color:black; background-color:white
<pre>[StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Ansi)]

<span style="color:blue public <span style="color:blue struct UnmanagedData

{

<span style="color:blue public <span style="color:blue bool Value1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]

<span style="color:blue public <span style="color:blue double[] Value2;

}

[/code]

C code
<div style="color:black; background-color:white
<pre><span style="color:blue typedef <span style="color:blue struct Data {

<span style="color:blue bool Value1;

<span style="color:blue double Value2[11];

};

[/code]

However, when we check the values in the debugger, the Value2 array had an extra element appended at the beginning.
e.g. The original data of Value2 property before marshalling is
[0] 1.0 double<br/>
[1] 0.99999999999999999 double<br/>
[2] 0.88888888888888888 double<br/>
[3] 0.77777777777777777 double<br/>
[4] 0.66666666666666666 double<br/>
[5] 0.55555555555555555 double<br/>
[6] 0.44444444444444444 double<br/>
[7] 0.33333333333333333 double<br/>
[8] 0.22222222222222222 double<br/>
[9] 0.11111111111111111 double<br/>
[10] 0.98989898989898989 double
after marshalling and check it in the Quick Watch window in the C DLL unmanaged environment, it becomes
[0] 0.00000000000000000 double<br/>
[1] 1.0 double<br/>
[2] 0.99999999999999999 double<br/>
[3] 0.88888888888888888 double<br/>
[4] 0.77777777777777777 double<br/>
[5] 0.66666666666666666 double<br/>
[6] 0.55555555555555555 double<br/>
[7] 0.44444444444444444 double<br/>
[8] 0.33333333333333333 double<br/>
[9] 0.22222222222222222 double<br/>
[10] 0.11111111111111111 double
The first elements became zero and the others are pushed down one position.
If we acess Value2[11] in the local windows, it will show [11] 0.98989898989898989 double
Did we missed configured anything?
Thanks

View the full article
 
Back
Top