[Mono-bugs] [Bug 59909][Nor] Changed - [PATCH] passing classes to C library functions doesn't allow modification

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sat, 28 Aug 2004 00:11:49 -0400 (EDT)


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 bmaurer@users.sf.net.

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

--- shadow/59909	2004-08-28 00:06:24.000000000 -0400
+++ shadow/59909.tmp.24962	2004-08-28 00:11:49.000000000 -0400
@@ -11,13 +11,13 @@
 AssignedTo: bmaurer@users.sf.net                            
 ReportedBy: richard@garandnet.net               
 QAContact: mono-bugs@ximian.com
 TargetMilestone: ---
 URL: 
 Cc: 
-Summary: passing classes to C library functions doesn't allow modification
+Summary: [PATCH] passing classes to C library functions doesn't allow modification
 
 Description of Problem: 
 When passing a simple class that wraps ints to a C library function, the 
 value of the class is not modified. This has been verified to work with 
 the Microsoft C# compiler and a DLL created in C++ Builder; executing the 
 working example with the mono runtime also works. 
@@ -65,6 +65,87 @@
 (MonoObject)
 
 ------- Additional Comments From bmaurer@users.sf.net  2004-08-28 00:06 -------
 Created an attachment (id=9852)
 patch
 
+
+------- Additional Comments From bmaurer@users.sf.net  2004-08-28 00:11 -------
+The attached patch avoids copying the class if all of its members are
+blittable. The Microsoft docs say that they do this.
+
+The test case I used was:
+
+using System;
+using System.Runtime.InteropServices;
+
+[StructLayoutAttribute (LayoutKind.Sequential)]
+class X {
+	int x;
+
+	[DllImport ("libtest")]
+	static extern void testfn (X x);
+
+	static void Main () {
+		X x = new X ();
+		testfn (x);
+		Console.WriteLine (x.x);
+	}
+}
+
+----------------------
+
+void testfn (int* x) {
+	*x = 69;
+}
+
+One problem this creates is that is "regresses" pinvoke16.exe. That
+test depends on the exact oposite behavior of this test case. In other
+words, it depends on a class always being passed as [In] by default.
+
+However, MSFT seems to pass a structure as [In, Out] if all its types
+are blittable. Example:
+
+using System.Runtime.InteropServices;
+using System;
+
+[StructLayout (LayoutKind.Sequential)]
+public class SystemTime {
+	public ushort wYear, wMonth, wDayOfWeek, wDay, wHour, wMinute,
+wSecond, wMilliseconds;
+	
+	[DllImport ("Kernel32.dll")]
+	static extern void GetSystemTime (SystemTime st);
+	
+	static void Main () {
+		SystemTime st = new SystemTime();
+		GetSystemTime (st);
+		Console.WriteLine ("party like its {0}", st.wYear);
+	}
+}
+
+This will print out `party like its 2004'.
+
+However, if I have:
+
+using System.Runtime.InteropServices;
+using System;
+
+[StructLayout (LayoutKind.Sequential)]
+public class SystemTime {
+	public ushort wYear, wMonth, wDayOfWeek, wDay, wHour, wMinute,
+wSecond, wMilliseconds;
+	string fakeout;
+	[DllImport ("Kernel32.dll")]
+	static extern void GetSystemTime (SystemTime st);
+	
+	static void Main () {
+		SystemTime st = new SystemTime();
+		GetSystemTime (st);
+		Console.WriteLine ("party like its {0}", st.wYear);
+	}
+}
+
+it will print `party like its 0'
+
+The MS runtime only does [In] if there are non-blittable types (such
+as a string).