C#调用C++写的DLL中返回字符串的问题!
非托管DLL中:
extern "C" SIMPLELIBRARY_API int fnSimpleLibrary(char *name);
SIMPLELIBRARY_API int fnSimpleLibrary(char *name)
{
strcpy(name, "aaaa");
return 0;
}
////////////////////////////////////////
C#代码:
[DllImport("SimpleLibrary.dll", EntryPoint="fnSimpleLibrary", CharSet=CharSet.Ansi)]
public static extern int fnSimpleLibrary(ref StringBuilder name);
调用:
StringBuilder name = new StringBuilder(64);
SimpleDll.fnSimpleLibrary(ref name);
结果程序当掉,说是:
未处理的“System.NullReferenceException”类型的异常出现在 TestDll.exe 中
其他信息:未将对象引用设置到对象的实例。
把StringBuilder改成string也不行,这个问题怎么解决啊!!!!