[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)
Hi,
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
wchar_t[].
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 =
CharSet.Unicode)]
public class Id
{
public int len;
[MarshalAs(UnmanagedType.ByValTStr,
SizeConst = 256)]
public string name;
};
class Test
{
[DllImport("libtest.so")]
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];
}Id;
int TestFn(Id *id)
{
printf("%s\t%S\t%d\n",__func__,id->name,id->len);
printf("wcslen returns.. %d\n",strlen(id->name));
return 1;
}
makefile
--------
all: lib test
lib: testlib.c
gcc -fPIC -c testlib.c
ld -x --shared -o libtest.so testlib.o
test:
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