hi
I just wrote a experiment code that build a DLL that export a method
(ToOut),whic I am trying to call from c#. Right now it return uncorrect
result .
Here is the unmanaged code :
typedef struct flagtest1
{
WORD KK;
}test, *ptest;
typedef struct Out
{
struct
{
DWORD BW: 1;
DWORD Gray: 1;
DWORD Color: 1;
} mode;
ptest pSource[2];
}myOut, * pmyOut;
void ToOut( pmyOut* myouttest)
{
(*myouttest)->mode.BW=1;
(*myouttest)->mode.Color=2;
(*myouttest)->mode.Gray=3;
(*myouttest)->pSource[0] = new test;
(*myouttest)->pSource[1] = new test;
(*myouttest)->pSource[0]->KK=4;
(*myouttest)->pSource[0]->KK=5;
}
Now , here is how I tried the same thing from c#;
[StructLayout(LayoutKind.Explicit,Size=3)]
public class _mode
{
[FieldOffset(0)]
public int BW;
[FieldOffset(1)]
public int Gray;
[FieldOffset(2)]
public int color;
}
[StructLayout(LayoutKind.Sequential)]
public class test
{
public System.UInt16 kk;
}
[StructLayout(LayoutKind.Sequential)]
public class myout
{
public _mode mode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public IntPtr[] ptest; //=
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(test))) ;
}
public class Program
{
[DllImport("try.dll")]
public static extern void ToOut( out IntPtr test);
public static void Main(string[] args)
{
IntPtr zzz= Marshal.AllocHGlobal(Marshal.SizeOf(typeof(myout)) );
ToOut(out zzz);
myout myzzz = (myout)Marshal.PtrToStructure(zzz, typeof(myout));
/// here the object myzz is uncorrect. the
///members: mode.bw ;mode.gray ;
mode.color and ptest display a random number;
System.Console.WriteLine("{0}",myzzz.mode.BW.ToStr ing());
Marshal.DestroyStructure(zzz, typeof(myout));
Marshal.FreeHGlobal(zzz);
}
}
}
Can anyone suggest a solution?
Thanks