日期:2014-05-18  浏览次数:21249 次

求助:C#调用c++的dll,字符串传递问题。
c++中的函数定义如下:
extern "C" __declspec(dllexport) DataBuffer* run(char* inputDataURL, char* runtimeFilePath)
{
 cout<<inputDataURL<<endl;
}
其中DataBuffer为结构体。
c#中函数声明如下:
public static extern IntPtr run(string uml, string name);
调用如下:
string a="bcde";
string b="fghijk";
IntPtr intp = run(a, b);
预期输出结果为“bcde”,运行后发现,只能输出字符串a的首字母‘b'。求助:我该如何定义c++中函数或者c#中函数的,才能实现c#向c++传递字符串。让其输出c#中的字符串。
谢谢!

------解决方案--------------------
charset ansi
------解决方案--------------------
探讨

charset ansi

------解决方案--------------------
C#的定义
public static extern IntPtr run([MarshalAs(UnmanagedType.LPArray)]byte[] uml, [MarshalAs(UnmanagedType.LPArray)]byte[] name);


调用
byte[] buml = new byte[]{};
byte[] bname = new byte[]{};

string a="bcde";
string b="fghijk";
buml = System.Text.Encoding.ASCII.GetBytes(a);
bname = System.Text.Encoding.ASCII.GetBytes(b);

IntPtr intp = run(buml , bname );