[Mono-bugs] [Bug 26317][Wis] New - NullReferenceException thrown when calling DllImport functions

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
15 Jun 2002 20:31:36 -0000


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 jonpryor@vt.edu.

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

--- shadow/26317	Sat Jun 15 16:31:36 2002
+++ shadow/26317.tmp.25903	Sat Jun 15 16:31:36 2002
@@ -0,0 +1,97 @@
+Bug#: 26317
+Product: Mono/Runtime
+Version: unspecified
+OS: Solaris 7
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: jonpryor@vt.edu               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: NullReferenceException thrown when calling DllImport functions
+
+Please fill in this template when reporting a bug, unless you know what you
+are doing.
+Description of Problem:
+
+Mono throws a System.NullReferenceException when I attempt to call a
+[DllImport]ed function.
+
+Steps to reproduce the problem:
+1. Create a DLL/shared library with the function to call:
+
+1.a: Use the following source code:
+
+        // file: native.c
+        // Native library
+        #include <string.h>
+
+        #ifdef _MSC_VER
+        __declspec(dllexport)
+        #endif
+        int getname(char* s, unsigned int n)
+        {
+            const char m[] = "This is my message.  Isn't it nice?";
+            strncpy(s, m, n);
+            return 0;
+        }
+
+1.b. Compile `native.c' into a DLL/shared library.
+
+On Windows: cl /LD native.c
+
+On Linux: gcc -c -fpic native.c && ld -shared -o libnative.so native.o -lc
+
+2. Create the managed program that will invoke the native function through
+PInvoke:
+
+2.a: Use the following program:
+
+        // file: managed.cs
+        using System;
+        using System.Runtime.InteropServices;
+        using System.Text;
+        
+        public class ghbn {
+          [DllImport("native")]
+          private static extern int getname(
+            StringBuilder sb, 
+            uint len);
+        
+          public static void Main () {
+            StringBuilder sb = new StringBuilder(255);
+            getname(sb, (uint) sb.Capacity);
+            Console.Write("name: ");
+            Console.WriteLine(sb.ToString());
+          }
+        }
+
+2.b: Compile it with either csc or mcs: csc managed.cs
+
+3.  Run the managed program:  mono managed.exe
+
+Actual Results:
+
+        name: 
+        ** (process:13403): WARNING **: unhandled exception
+        System.NullReferenceException: "A null value was found where
+an         object
+instance was required"
+        in <0x00065> .ghbn:Main ()
+
+        RESULT: -1
+
+Expected Results:
+        name: This is my message.  Isn't it nice?
+
+How often does this happen? 
+
+        All the time.
+
+Additional Information: