Truncated string passed from C++ to C# in PPC

Hello,

I've just started experimenting with passing strings and converting them from ascii to unicode.. and have encountered some difficulties..

1) I'm trying to pass a string from C++ to C#. But somehow, the 1st 8 bytes always go missing.Does anyone know why?

C++ code:

extern "C" __declspec(dll export) TCHAR* _stdCall f1

{

return L"write some words..";

}

C# code:

[DllImport("test.dll", CharSet = CharSet.Auto)]

public static extern string f1();

char[] cArray = new char[20];

string s = new string(cArray);

s = f1();

The result of s would be: e some words..

2) Does anyone know how to convert ASCII to Unicode in C? Unlike C++, I can't seem to use TCHAR, LPCWSTR, etc.. in C.

Thanks in advance!

[918 byte] By [Juz_Miz] at [2008-1-9]
# 1

Try this instead:

C# code:

[DllImport("test.dll", CharSet = CharSet.Auto)]

public static extern void f1(StringBuilder string, int maxSize);

StringBuilder sb = new StringBuilder(20);

f1(sb, sb.Capacity);

s = sb.ToString();

C++ code:

extern "C" __declspec(dll export) void _stdCall f1 (WCHAR * string, int maxSize) // Always use WCHAR

{

// Copy up to MaxSize character into the string buffer here. Never return pointers to static buffers.

}

IlyaTumanov at 2007-10-3 > top of Msdn Tech,Smart Device Development,Smart Devices General...