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

Yogendra Thakur YogendraT at ami.com
Thu Jul 28 12:31:28 EDT 2005


Thanks Massi & Zoltan.

It works after removing the ref. As Zoltan said, by defualt classed are
passed as reference to unmanaged code.

-YoGi 

-----Original Message-----
From: Massimiliano Mantione
To: Yogendra Thakur
Cc: 'mono-devel-list at lists.ximian.com '
Sent: 7/28/05 11:17 AM
Subject: Re: [Mono-devel-list] Calling Native C api - Marshalling issue.


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