An MSDN sample might be worth looking at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconbufferssample.asp

It works for me on .NET, but I haven't been able to make it work with
mono...  I'm thinking that mono isn't ready for full-blown DllImport
yet.

For example, the following works with .NET

        // 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;
        }

With VC, compile as `cl /LD native.c'; should create `native.dll'.

On Linux, compile as `gcc -c -fpic native.c && ld -shared -o
libnative.so native.o -lc', should create `libnative.so'

On Linux, make sure that `LD_LIBRARY_PATH' contains the directory with
`libnative.so': `export LD_LIBRARY_PATH=`pwd`'

Now, the managed code:

        // 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());
          }
        }

Compile under .NET with `csc.exe /out:managed-net.exe managed.cs'

Compile under mono with `mcs managed.cs'

When I run either `managed.exe' (the mcs-compiled program) or
`managed-net.exe' (the csc-compiled program) under .NET, I get the
expected output:

        name: This is my message.  Isn't it nice?

When I run either `managed.exe' or `managed-net.exe' under mono, I get
errors:

        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

This implies to me that Mono does not properly handle PInvoke calls, at
least currently.  (CVS tree built around 9:00 AM this morning).

I should probably add this to bugzilla...

 - Jon

On Fri, 2002-06-14 at 13:40, Stewart Allen wrote:
    I've been struggling with DllImport all evening just to call 
    gethostname(char *buf, int len). Seems like it should be simple. There 
    appear to be a lot of examples of sending string arguments and getting 
    string return values, but I can't find anything that addresses filling a 
    preallocated string buffer.
    
    most tests (that I can get to compile) result in:
    
    ** (process:7): WARNING **: unhandled exception 
    System.NullReferenceException: "A null value was found where an object 
    instance was required"
    
    Any help is greatly appreciated.
    
    
    _______________________________________________
    Mono-list maillist  -  [EMAIL PROTECTED]
http://lists.ximian.com/mailman/listinfo/mono-list
    
    


_______________________________________________
Mono-list maillist  -  [EMAIL PROTECTED]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to