[Mono-bugs] [Bug 38219][Nor] Changed - Issues with returning a pointer to a structure from P/Invoke

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Tue, 25 Feb 2003 07:30: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 lupus@ximian.com.

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

--- shadow/38219	Mon Feb 17 23:07:36 2003
+++ shadow/38219.tmp.24112	Tue Feb 25 07:30:08 2003
@@ -1,23 +1,23 @@
 Bug#: 38219
 Product: Mono/Runtime
 Version: unspecified
-OS: 
+OS: unknown
 OS Details: 
 Status: NEW   
 Resolution: 
-Severity: 
+Severity: Unknown
 Priority: Normal
 Component: misc
 AssignedTo: mono-bugs@ximian.com                            
 ReportedBy: alp@atoker.com               
 QAContact: mono-bugs@ximian.com
 TargetMilestone: ---
 URL: 
 Cc: 
-Summary: Marshalling a struct of char* fails
+Summary: Issues with returning a pointer to a structure from P/Invoke
 
 This attempt to marshal a struct containing char* pointers runs but returns
 garbage instead of the expected text fields from /etc/passwd. The struct is
 defined in pwd.h as:
 
 /* The passwd structure.  */
@@ -69,6 +69,32 @@
 		p = getpwuid (getuid ());
 		Console.WriteLine (p.pw_name);
 		Console.WriteLine (p.pw_uid);
 		Console.WriteLine (p.pw_gecos);
 	}
 }
+
+------- Additional Comments From lupus@ximian.com  2003-02-25 07:30 -------
+The code cannot work, because getpwuid() returns a pointer to a
+structure, while the method signature makes it return the structure.
+The simple way to implement this is:
+	[DllImport("libc.so.6")]
+	static extern IntPtr getpwuid (uint uid);
+
+and:
+	passwd p;
+	IntPtr v;
+
+	v = getpwuid (getuid ());
+	p = (passwd)Marshal.PtrToStructure (v, typeof (passwd));
+
+The variant that uses:
+	[DllImport("libc.so.6")]
+	static extern passwd* getpwuid (uint uid);
+
+	passwd p;
+	p = *getpwuid (getuid ());
+
+doesn't work in mono and this is the reason I'm leaving the bug open.
+Note I'm not sure this variant is supposed to work, anyway.
+We'd need to write tests and see what the MS runtime does in this case.
+