[Mono-list] Support for marshalling of C# string to unmanaged wchar_t on Linux

Kala B kalabalun@yahoo.co.in
Thu, 16 Dec 2004 13:12:02 +0000 (GMT)

Does mono support marshalling of C# string to
unmanaged wchar_t on Linux? 

It does not seem to work. Consider the following
sample code, 
( contents of 3 files ) 
1. chash.cs
   This C# code makes a call to a C API, which takes a
2. testlib.c
   This is the C code which implements the C API.
3. makefile
   to build the library and C# exe.

Contents of chash.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Specialized;

[StructLayout(LayoutKind.Sequential, CharSet =
public class Id
          public int              len;
SizeConst = 256)]
          public string   name;
class Test
    public static extern int TestFn (Id id);

    public static void Main()
        string name = "testname";
        Id id = new Id();
        id.name = name;
        id.len = 8;        //name.Length();
        int rcode = TestFn(id);
        Console.WriteLine("TestFn returns {0}",rcode);

Contents of testlib.c
#include <stdio.h>
#include <wchar.h>

typedef struct _id
    int len;
    wchar_t name[256];

int TestFn(Id *id)
    printf("wcslen returns.. %d\n",strlen(id->name));
    return 1;

all: lib test
lib: testlib.c
        gcc -fPIC -c testlib.c
        ld -x --shared -o libtest.so testlib.o
        mcs -debug chash.cs
clean :
        rm ./libtest.so
        rm ./chash.exe

When chash.exe is run, it prints some junk characters.
Even wcslen() does not print the expected output.

Could you please help? 
If marshalling is not supported, could you please
suggest some alternate solution to solve the issue?

Thanks & Regards
Kala B.

Yahoo! India Matrimony: Find your life partner online
Go to: http://yahoo.shaadi.com/india-matrimony