[Mono-devel-list] Calling Native C api - Marshalling issue.
Zoltan Varga
vargaz at gmail.com
Thu Jul 28 11:05:50 EDT 2005
Hi,
classes are by default passed by-ref to unmanaged code, i.e. a pointer to the
data is passed to the C side. If you define your managed method as 'ref', then
the unmanaged code will receive a pointer-to-a-pointer to the data. Try
removing the 'ref', or defining CSMyData as a struct, which by default
marshalled
by-value.
Zoltan
On 7/28/05, Yogendra Thakur <YogendraT at ami.com> wrote:
>
> I am getting problem while executing the c library function from C#.
>
> Following is the equivalent code .
>
> C Code :
> -----------
> //This is the structure in C
> typedef struct MyDataType
> {
> u_int data1;
> u_int data2;
> } MyData;
>
> //This is the C function which is called by passing pointer to above
> structure
> function u_int DoSomeOperation(MyData* pData)
> {
> return pData->data1 +pData->data2;
> }
>
>
> C# Code :
> ----------------
> Following code shows how I am executing that call.
>
> using System;
> using System.Runtime.InteropServices;
> namespace Test
> {
> //Define equivalent class to MyData structure
> [StructLayout(LayoutKind.Sequential)]
> public class CSMyData
> {
> public UInt32 data1;
> public UInt32 data2;
> }
>
>
>
> public class Test
> {
>
> //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);
>
> //main function for console application
> static void Main()
> {
> UInt32 result = 0;
> CSMyData inData = new CSMyData();
>
> inData.data1 = 0xFFFF;
> inData.data2 = 0xFF;
>
> result = OperateData(ref inData);
>
> Console.WriteLine(" Addition = " result.ToString();
> }
>
> }
>
> }
>
>
> Issue:
> --------
> When I am printing value in the C code side ,ie in DoSomeOperation
> function , the values are corrupted.
>
> Please let me know what part is wrong.
>
> regards,
> Yogi
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
More information about the Mono-devel-list
mailing list