问题描述
|
我正在尝试从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);
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)