[Mono-bugs] [Bug 40645][Nor] New - Passing class as an out parameter to pinvoke doesn't work

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Tue, 1 Apr 2003 18:40:41 -0500 (EST)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by pbaena@uol.com.ar.

http://bugzilla.ximian.com/show_bug.cgi?id=40645

--- shadow/40645	Tue Apr  1 18:40:41 2003
+++ shadow/40645.tmp.32162	Tue Apr  1 18:40:41 2003
@@ -0,0 +1,129 @@
+Bug#: 40645
+Product: Mono/Runtime
+Version: unspecified
+OS: Debian Woody
+OS Details: 2.4.17 kernel
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: pbaena@uol.com.ar               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Passing class as an out parameter to pinvoke doesn't work
+
+This example runs on .NET but not on mono:
+
+/* Unmanaged C code */
+#include <stdio.h>
+typedef struct
+{
+        char* str;
+        int i;
+} test_arr_t;
+ 
+typedef struct
+{
+        test_arr_t* arr;
+        int nr_of_arrs;
+} test_t;
+
+int modify_it (test_t** testme)
+{
+        test_t* ptest = (test_t *) malloc (sizeof (test_t));
+       
+        ptest->arr = (test_arr_t *) malloc (2 * sizeof (test_arr_t));
+        ptest->arr[0].i = 1;
+        ptest->arr[0].str = (char *) malloc (sizeof(char)*24);
+        strcpy (ptest->arr[0].str, "Hello");
+        ptest->arr[1].i = 2;
+        ptest->arr[1].str = (char *) malloc (sizeof(char)*24);
+        strcpy (ptest->arr[1].str, "baby");
+        ptest->nr_of_arrs = 2;
+
+        *testme = ptest;
+        return 0;
+}
+/* End of C code */
+
+
+/* C# code */
+using System;
+using System.Runtime.InteropServices;
+
+namespace teststruct
+{
+        [ StructLayout( LayoutKind.Sequential )]
+        class test_struct_arr
+        {
+                public string str;
+                public int i;
+        }
+        
+        [ StructLayout( LayoutKind.Sequential )]
+        class test_struct
+        {
+                public IntPtr arr;
+                public int nr_of_arrs;
+        }
+
+        class Class1
+        {
+                [DllImport ("library")]
+                static extern int modify_it (out test_struct p);
+                                
+                [STAThread]
+                static void Main(string[] args)
+                {
+                        test_struct testme;
+                        modify_it (out testme);
+
+                        if (testme != null)
+                        {                               
+                                if (testme.nr_of_arrs != 2)
+                                {
+                                        Console.WriteLine ("{0} elements?
+Yeah, right...", testme.nr_of_arrs);
+                                        return;
+                                }
+
+                                test_struct_arr[] tarr = new
+test_struct_arr[testme.nr_of_arrs];
+                                IntPtr current = testme.arr;
+                                
+                                for( int i = 0; i < testme.nr_of_arrs; i++ )
+                                {
+                                        tarr[ i ] = new test_struct_arr();
+                                        Marshal.PtrToStructure( current,
+tarr[ i ]);
+                                
+                                        //Marshal.FreeCoTaskMem(
+(IntPtr)Marshal.ReadInt32( current ));
+                                        //Marshal.DestroyStructure(
+current, typeof(test_struct_arr) );
+
+                                        current = (IntPtr)((int)current + 
+                                                Marshal.SizeOf( tarr[ i ] ));
+
+                                        Console.WriteLine ("i[{0}] = {1}
+{2}", i, tarr[i].str, tarr[i].i);         
+                                }
+                        }
+                        else
+                        {
+                                Console.WriteLine ("testme wasn't set");
+                        }
+                }
+        }
+}
+
+/* End of code */
+
+I got around this by making the modify parameter an IntPtr, and then
+marshaling it to the test_struct class. I don't know if this is a feature
+or a bug, it isn't documented anywhere what's the correct way to pass out
+objects to pinvoke.