日期:2014-05-17  浏览次数:20709 次

问个C#调用C++DLL的问题
我用C++写了个DLL,在C#调用.C++原型如下:

extern "C" _declspec(dllexport)  int CCodeInitApp::Code(char * path,char * str)
{
  int plen =strlen(path);
  int slen=strlen(str);
  return 0;
}

我在C#的原型是这样

[DllImport("CodeInit.dll", CharSet = CharSet.Auto)]
public static extern int Code(string path, string str);

我在调用Code的时候传进参数。例如:

Code("d:\\config.ini","config")

然后问题来了,无论我传进去的参数是什么
在C++里面 strlen()返回的都是1;
估计是参数的问题了,因为我试过在C++定义过char * code="code",用strlen得出的结果是4
有没有人做过这方面的?麻烦告知一下,参数应该怎么传。
C++ C#

------解决方案--------------------
try
[DllImport("CodeInit.dll", CharSet = CharSet.Auto)]
public static extern int Code([MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] string str);

------解决方案--------------------
   [DllImport("CodeInit.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]  这么试试?