为什么不能在Release版本中将char *字符串从C ++返回到C#?
|
我正在尝试从C#调用以下简单的C函数:
SIMPLEDLL_API const char* ReturnString()
{
return \"Returning a static string!\";
}
使用以下P / Invoke声明(带有或不带有return属性,这没有什么区别):
[DllImport(\"SimpleDll\")]
[return: MarshalAs(UnmanagedType.LPStr)]
public static extern string ReturnString();
如果DLL是Debug版本,但在Release版本(AccessViolationException)中崩溃,则它可以工作。
我正在调用其他十几个简单的函数,这是唯一失败的函数(其他是:)
[DllImport(\"SimpleDll\")] public static extern int NextInt();
[DllImport(\"SimpleDll\")] public static extern void SetNextInt(int x);
[DllImport(\"SimpleDll\")] public static extern int AddInts(int a, int b);
[DllImport(\"SimpleDll\")] public static extern int AddFourInts(int a, int b, int c, int d);
[DllImport(\"SimpleDll\")] public static extern double AddDoubles(double x, double y);
[DllImport(\"SimpleDll\")] public static extern IntPtr AddDoublesIndirect(ref double x, ref double y);
[DllImport(\"SimpleDll\")] [return: MarshalAs(UnmanagedType.U1)]
public static extern char CharStringArgument([MarshalAs(UnmanagedType.LPStr)]string s);
[DllImport(\"SimpleDll\")] [return: MarshalAs(UnmanagedType.U2)]
public static extern char WCharStringArgument([MarshalAs(UnmanagedType.LPWStr)]string s);
[DllImport(\"SimpleDll\")] [return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string ReturnWString();
[DllImport(\"SimpleDll\")] [return: MarshalAs(UnmanagedType.BStr)]
public static extern string ReturnBSTR();
[DllImport(\"SimpleDll\")] public static extern System.Drawing.Point MakePoint(int x, int y);
[DllImport(\"SimpleDll\")] public static extern IntPtr MakePointIndirect(int x, int y);
[DllImport(\"SimpleDll\")] public static extern int GetPointY(System.Drawing.Point p);
[DllImport(\"SimpleDll\")] public static extern int GetPointYIndirect(ref System.Drawing.Point pp);
[DllImport(\"SimpleDll\")] public static extern int SumIntegers(ref int firstElem, int size);
没有找到相关结果
已邀请:
5 个回复
擅香
然后在您的调用代码中使用元帅课
磁辫覆氓
并像这样使用它:
臂哦
锹缄
室邢
不知道为什么在调试和发行中会有所不同。