首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

问个C#调用C++DLL的有关问题

2013-08-23 
问个C#调用C++DLL的问题我用C++写了个DLL,在C#调用.C++原型如下:extern C _declspec(dllexport)int CCod

问个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)]  这么试试?

热点排行