[Mono-bugs] [Bug 71015][Maj] Changed - Marshalling string to UnmanagedType.ByValArray is not implemented

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Thu, 6 Jan 2005 08:43:08 -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 kalabalun@yahoo.co.in.

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

--- shadow/71015	2005-01-06 01:34:03.000000000 -0500
+++ shadow/71015.tmp.16556	2005-01-06 08:43:08.000000000 -0500
@@ -66,6 +66,118 @@
 
 
 
 
 
 
+
+------- Additional Comments From kalabalun@yahoo.co.in  2005-01-06 08:43 -------
+Here is some sample code which when compiled and run, shows that 
+mono does not support Marshal.PtrToStructure for a structure whose 
+charset = unicode and has a string, for your convenience.
+
+Sample code:
+------------
+Contents of test.c
+------------------
+
+#include <stdio.h>
+#include <wchar.h>
+#include <assert.h>
+typedef struct _id
+{
+    int id;
+    wchar_t name[128];
+}Id;
+
+typedef struct _idlist
+{
+    int num;
+    Id *list;
+}IdList;
+
+int myfunc(IdList *idList)
+{
+    printf("In lib : %s \t idList->num is
+%d\n",__func__,idList->num);
+    assert(idList->list);
+    idList->list[0].id = 10;
+    wcscpy(idList->list[0].name,L"One");
+    idList->list[1].id = 20;
+    wcscpy(idList->list[1].name,L"Two");
+    idList->num = 2; 
+    return 1;
+}
+
+Contents of chash.cs
+--------------------
+using System;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Collections.Specialized;
+
+[StructLayout(LayoutKind.Sequential, CharSet =
+CharSet.Unicode)]
+public class Id
+{
+    public int id;
+    [MarshalAs(UnmanagedType.ByValTStr, SizeConst =
+128)]
+    public string name;
+};
+
+[StructLayout(LayoutKind.Sequential, CharSet =
+CharSet.Unicode)]
+public class IdList
+{
+    public int num;
+    public IntPtr list;
+};
+
+class Test
+{
+    [DllImport("libtest.so")]
+    public static extern int myfunc([In, Out] IdList
+idList);
+
+    public static void Main()
+    {
+        Id id = new Id();
+        IdList idList = new IdList();
+        //Allocate memory for 3 ids.
+        idList.num = 3;
+        idList.list = Marshal.AllocHGlobal(3 * 
+Marshal.SizeOf(id));
+
+        int rcode = myfunc(idList);
+        Console.WriteLine("\nmyfunc returns {0} and
+numids returned is {1} ",rcode,idList.num);
+        for (int i = 0; i< idList.num; i++)
+        {
+            IntPtr temp = new
+IntPtr(idList.list.ToInt32() + (i *
+Marshal.SizeOf(id)));
+            id = (Id)Marshal.PtrToStructure(temp,
+typeof(Id));
+            Console.WriteLine("idList[{0}] is {1}
+{2}",i,id.id,id.name);
+        }
+        Marshal.FreeHGlobal(idList.list);
+
+    }
+}
+
+
+Contents of makefile
+---------------------
+all: lib test 
+lib: testlib.c 
+	gcc -fPIC -g -c test.c
+	ld -x --shared -o libtest.so test.o
+test:
+	mcs -debug chash.cs
+clean :
+	rm ./libtest.so
+        rm ./chash.exe
+
+Thanks
+