[Mono-devel-list] Calling Native C api - Marshalling issue.

Massimiliano Mantione massi at ximian.com
Thu Jul 28 11:17:46 EDT 2005


On Thu, 2005-07-28 at 10:15 -0400, Yogendra Thakur wrote:
>  I am getting problem while executing the c library function from C#.
>   //Define equivalent class to MyData structure
>   [StructLayout(LayoutKind.Sequential)]
>   public class CSMyData
>   {

either should be a struct, not a class...

>    //Import the dll and define C function.
>    [DllImport ("mylibrary.dll", EntryPoint="DoSomeOperation")]
>    static extern Int32 OperateData( ref  CSMyData pData);
>    //Other way of defining
>    //static extern Int32 OperateData( [In,Out]  CSMyData pData);

...or you should omit the "ref" (or the "[In,Out]").

With these modification it works.

The issue is that if CSMyData is a struct, then the "ref" (or the
"[In,Out]") is needed to get the address of the struct.
But if CSMyData is a class, marshalling converts it correctly.
I am no marshalling expert, but I think that if you put the "ref"
you pass the address of the object reference :-)

Ciao,
  Massi





More information about the Mono-devel-list mailing list