[Mono-bugs] [Bug 38219][Nor] New - Marshalling a struct of char* fails

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Mon, 17 Feb 2003 23:07:36 -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 alp@atoker.com.

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

--- shadow/38219	Mon Feb 17 23:07:36 2003
+++ shadow/38219.tmp.17755	Mon Feb 17 23:07:36 2003
@@ -0,0 +1,74 @@
+Bug#: 38219
+Product: Mono/Runtime
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+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
+
+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.  */
+struct passwd
+{
+  char *pw_name;                /* Username.  */
+  char *pw_passwd;              /* Password.  */
+  __uid_t pw_uid;               /* User ID.  */
+  __gid_t pw_gid;               /* Group ID.  */
+  char *pw_gecos;               /* Real name.  */
+  char *pw_dir;                 /* Home directory.  */
+  char *pw_shell;               /* Shell program.  */
+};
+
+The test case:
+
+using System;
+using System.Runtime.InteropServices;
+
+[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
+struct passwd
+{
+	[MarshalAs(UnmanagedType.LPStr)]
+	public string pw_name;    /* Username.  */
+	[MarshalAs(UnmanagedType.LPStr)]
+	public string pw_passwd;  /* Password.  */
+	public uint pw_uid;        /* User ID.  */
+	public uint pw_gid;        /* Group ID.  */
+	[MarshalAs(UnmanagedType.LPStr)]
+	public string pw_gecos;   /* Real name.  */
+	[MarshalAs(UnmanagedType.LPStr)]
+	public string pw_dir;     /* Home directory.  */
+	[MarshalAs(UnmanagedType.LPStr)]
+	public string pw_shell;   /* Shell program.  */
+}
+
+class passwdtest
+{
+	[DllImport("libc.so.6")]
+	static extern uint getuid ();
+
+	[DllImport("libc.so.6")]
+	static extern passwd getpwuid (uint uid);
+	
+	static void Main ()
+	{
+		passwd p;
+
+		p = getpwuid (getuid ());
+		Console.WriteLine (p.pw_name);
+		Console.WriteLine (p.pw_uid);
+		Console.WriteLine (p.pw_gecos);
+	}
+}