c#调用c写的dll出错,求大神指导!!![DllImport(__BANKER.dll, EntryPoint getdata, SetLastError t
c#调用c写的dll出错,求大神指导!!!
[DllImport("__BANKER.dll", EntryPoint = "getdata", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern double getdata(StringBuilder sb, int nLen);
这是c写的dll代码声明
extern "C" __declspec(dllexport) double zhuang(char *array,int N);
结果:
“尝试读取或写入受保护的内存,这通常表明其他内存已损坏。。。。”
网上搜索很多是类型的问题,之前C#用的是char[],出问题,现在改成string ,stringbuilder还是这样。。。。
[最优解释]public static extern double getdata(byte[] sb, int nLen);
byte[] sb = System.Text.Encoding.Default.GetBytes(字符串);
nLen = sb.Count();
[其他解释]EntryPoint = "getdata",可以使用depend工具看对应名称是什么就写什么,至于你这边的方法命名可以随你便。有一点要注意,方法的参数类型和顺序有一定的规定,需要你自己测试。
[其他解释] [DllImport("__BANKER.dll", EntryPoint = "getdata", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern double getdata( [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)] string sb, int nLen);
UnmanagedType.ByValTStr是把托管代码转化为非托管代码的数组,SizeConst是数组长度。你可以这样试试
[其他解释]问题依旧。。。
[其他解释]EntryPoint = "getdata"
=>
EntryPoint = "zhuang"
[其他解释]这都被你发现了。。。但是其实入口函数跟调用函数没什么关系呀。
[其他解释]zhuang那个函数名写错了没改
[其他解释]指定的非托管类型只对只读字段有效?
[其他解释]dll是我自己写的,就是用的char *
------其他解决方案--------------------
char*貌似对应byte[]
[其他解释]网上也有人提供这种方法,但是依然如此,算了,结贴