日期:2014-05-17  浏览次数:21032 次

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是数组长度。你可以这样试试
------其他解决方案--------------------
引用:
public static extern double getdata(byte[] sb, int nLen);

byte[] sb = System.Text.Encoding.Default.GetBytes(字符串);
nLen = sb.Count();


问题依旧。。。
------其他解决方案--------------------
EntryPoint = "getdata"
=>
EntryPoint = "zhuang"
------其他解决方案--------------------
引用:
EntryPoint = "getdata"
=>
EntryPoint = "zhuang"


这都被你发现了。。。但是其实入口函数跟调用函数没什么关系呀。
------其他解决方案--------------------
引用:
引用:EntryPoint = "getdata"
=>
EntryPoint = "zhuang"

这都被你发现了。。。但是其实入口函数跟调用函数没什么关系呀。

zhuang那个函数名写错了没改
------其他解决方案--------------------
引用:
C# code??12   [DllImport("__BANKER.dll", EntryPoint = "getdata", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]        public static extern dou……

指定的非托管类型只对只读字段有效?
------其他解决方案--------------------
引用:
EntryPoint = "getdata",可以使用depend工具看对应名称是什么就写什么,至于你这边的方法命名可以随你便。有一点要注意,方法的参数类型和顺序有一定的规定,需要你自己测试。

dll是我自己写的,就是用的char *
------其他解决方案--------------------