2017年5月25日 星期四

Passing string between C# and C++ dll

From C# to C++ dll

C++
void foo(const char *str)
{
    // do something with str
}
C#
[DllImport("...", CallingConvention = CallingConvention.Cdecl)
static extern void foo(string str);

....

foo("bar");
In the other direction you would typically expect the caller to allocate the buffer, into which the callee can write:
C++
void foo(char *str, int len)
{
    // write no more than len characters into str
}
C#
[DllImport("...", CallingConvention = CallingConvention.Cdecl)
static extern void foo(StringBuilder str, int len);

....

StringBuilder sb = new StringBuilder(10);
foo(sb, sb.Capacity);



From C++ dll to C#
C++ 裡面:
TCHAR g_awcMessage[] = L"Hello中文"; char g_aszMessage[] = "Hello中文";
extern "C" __declspec(dllexport) TCHAR* __stdcall GetHelloL() {   return g_awcMessage;  }
extern "C" __declspec(dllexport) CHAR* __stdcall GetHello() {   return g_aszMessage; }
extern "C" __declspec(dllexport) int __stdcall GetInt() {   return 100; }
C# 裡面:
[DllImport("gpDll.dll")] public static extern IntPtr GetHello();
[DllImport("gpDll.dll")] public static extern IntPtr GetHelloL();
[DllImport("gpDll.dll")] public static extern int GetInt();
private void button1_Click(object sender, EventArgs e) {             // Not support in CF             //Marshal.PtrToStringAnsi             //Marshal.PtrToStringAuto
            // Multibytes 會變成亂碼             string str =  Marshal.PtrToStringUni(GetHello());
            // Wide Character 顯示正常             string str = Marshal.PtrToStringAnsi(GetHello());
            // Wide Character 顯示正常             string strL = Marshal.PtrToStringUni(GetHelloL());
            int n = GetInt(); }

沒有留言:

張貼留言