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#中的字符串。
谢谢!

解决方案 »

  1.   

    很有可能是字符集设置不对。你在C++的指定字符是什么呢?试试ansi或者
    其他的字符http://msdn.microsoft.com/zh-cn/library/7b93s42f.aspx
      

  2.   

    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 );
      

  3.   

    byte[] buml = new byte[]{};
    byte[] bname = new byte[]{};这种定义方式有错误,改为:
     byte[] buml = new byte[a.Length];
     byte[] bname = new byte[b.Length];编译能通过,并且也解决了之后的参数传递问题。十分感谢,同时也感谢一楼、二楼和三楼的回帖