[Mono-list] more problems with C and C# strings

Bryan Buchanan bryanb@webbtide.com
21 Dec 2004 10:15:44 +1000


Hi,

This small prog in C works:

#include <gnome.h>
#include <stdarg.h>
                                                                                                    void QMCall(char * subrname, int argc, ...);
                                                                
int main(int argc, char **argv)
{
    char *x = g_malloc(1024);
    char *y = g_malloc(1024);
    g_sprintf(x, "%s", "abcd");
    g_sprintf(y, "%s", "xyz");
    g_message(x);
    g_message(y);
    QMCall("a name", 2, x, y);
    g_message(x);
    g_message(y);
}
                                                                                                    void QMCall(char * subrname, int argc, ...) {
    va_list ap;
    char *arg;
    int i;
                                                                       
    g_message(subrname);
                                                                       
    va_start(ap, argc);
    for (i = 0; i < argc; i++) {
        arg = va_arg(ap, char *);
        g_message(arg);
    }
    va_end(ap);
                                                                 
    va_start(ap, argc);
    for (i = 1; i <= argc; i++) {
        arg = va_arg(ap, char *);
        g_sprintf(arg, "this is string %d", i);
    }
    va_end(ap);
}

Gives this output:
** Message: abcd
** Message: xyz
** Message: a name
** Message: abcd
** Message: xyz
** Message: this is string 1
** Message: this is string 2


When I compile as a library and call it from C# with this prog:

using System;
using System.Text;
using System.Runtime.InteropServices;
                                                                                                    public class QMClient {
    [DllImport("mylib.so")]
    public static extern void QMCall(string subrname, int argc, params
StringBuilder [] str);
}
                                                                                                    class test {
    static void Main() {
                                                                       
        StringBuilder x = new StringBuilder("abcd", 1024);
        StringBuilder y = new StringBuilder("xyz", 1024);
        Console.WriteLine("{0} {1}", x.ToString(), y.ToString());
                                                                       
        QMClient.QMCall("a name", 2, x, y);
                                                                      
        Console.WriteLine("{0} {1}", x.ToString(), y.ToString());
    }
                                                                        
}

I get this result:

abcd xyz
** Message: a name
** Message: hGpKpK,
** Message: abcd
 
Unhandled Exception: System.NullReferenceException: Object reference not
set to an instance of an object
in <0xf84e4c> (wrapper managed-to-native) QMClient:QMCall
(string,int,System.Text.StringBuilder[])
in <0x00004> (wrapper managed-to-native) QMClient:QMCall
(string,int,System.Text.StringBuilder[])
in <0x000cc> test:Main ()

Any clues ?

Thanks,

Bryan