c#调用DLL尝试读取或写入受保护的内存。这通常指示其他内存已损坏

lxclei 2010-06-20 11:05:29
DELPHI里dll的源码


function Init(const CDT:integer):integer;stdcall;
//释放对象 =0成功;=1失败
function UnInit:integer;stdcall;
//主机初始化com口函数 =0成功;=1失败
function InitCom(const comid:string):integer;stdcall;
//设置读写器状态 =0成功;=1失败
function SetState(const state:integer):integer;stdcall;
//读取卡号 =1失败
function ReadCard(buffer: PChar; bufferlen: integer):integer;stdcall;
//写信息到卡片中 =0成功;=1失败 mark=0 默认密码FF FF FF FF FF FF =1 新密码 d2 bb c3 f9 cc ec
function WriteInfo(const mark:integer;const id:integer; const d1:integer;const d2:integer;const d3:integer;const d4:integer;
const d5:integer;const d6:integer;const d7:integer;const d8:integer;const d9:integer;
const d10:integer;const d11:integer;const d12:integer;const d13:integer;const d14:integer;
const d15:integer;const d16:integer):integer;stdcall;
//读信息到卡片中 =0成功;=1失败 mark=0 默认密码FF FF FF FF FF FF =1 新密码 d2 bb c3 f9 cc ec
function ReadInfo(const mark:integer;const id:integer;d1:PChar;d2:PChar;d3:PChar;d4:PChar;d5:PChar;d6:PChar;d7:PChar;
d8:PChar;d9:PChar;d10:PChar;d11:PChar;d12:PChar;d13:PChar;d14:PChar;d15:PChar;d16:PChar):integer;stdcall;
//*******************内部函数**************************
//设置发送数据标识
function SetMark(const handle:THandle;const mark:byte):integer;
//发送数据函数 =0成功;=1失败 =3 mark位设置失败
function SendData(const handle:THandle):integer;
//接收数据函数 =0成功;=1失败 =11 接收失败 =12 接收头错误 =5 超时错误
function InceptData(const handle:THandle):integer;
//计算效验值 //mark=1 读,=0 写
function ComputeEfficacy(mark:integer):integer;
我调用C#调用

[DllImport("Project2.dll")]
private static extern int Init(int CDT);
//释放对象 =0成功;=1失败
[DllImport("Project2.dll")]
private static extern int UnInit();

//主机初始化com口函数 =0成功;=1失败
[DllImport("Project2.dll")]
private static extern int InitCom(string comid);
//设置读写器状态 =0成功;=1失败
[DllImport("Project2.dll")]
private static extern int SetState(int state);
//读取卡号 =1失败
[DllImport("Project2.dll")]
private static extern int ReadCard(ref string buffer, byte bufferlen);
//写信息到卡片中 =0成功;=1失败 mark=0 默认密码FF FF FF FF FF FF =1 新密码 d2 bb c3 f9 cc ec
[DllImport("Project2.dll")]
private static extern int WriteInfo(int mark,int id,int d1, int d2, int d3,int d4,int d5,int d6,int d7, int d8,int d9,int d10,int d11,int d12,int d13,int d14,int d15,int d16);

//读信息到卡片中 =0成功;=1失败 mark=0 默认密码FF FF FF FF FF FF =1 新密码 d2 bb c3 f9 cc ec
[DllImport("Project2.dll")]
private static extern int ReadInfo(int mark,int id,ref string d1,ref string d2,ref string d3,ref string d4,ref string d5,ref string d6,ref string d7,ref string d8,ref string d9,ref string d10,ref string d11,ref string d12,ref string d13,ref string d14,ref string d15,ref string d16);
//*******************内部函数**************************
//设置发送数据标识
[DllImport("Project2.dll")]
private static extern int SetMark(ref IntPtr handle, byte mark);
//发送数据函数 =0成功;=1失败 =3 mark位设置失败
[DllImport("Project2.dll")]
private static extern int SendData(IntPtr handle);
//接收数据函数 =0成功;=1失败 =11 接收失败 =12 接收头错误 =5 超时错误
[DllImport("Project2.dll")]
private static extern int InceptData( IntPtr handle);
//计算效验值 //mark=1 读,=0 写
[DllImport("Project2.dll")]
private static extern int ComputeEfficacy(int mark);
...全文
632 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhu_jiang 2010-06-23
  • 打赏
  • 举报
回复
自己用dll建立新的project调用自己的dll看存在问题否!
delphi中有个内存管理模块叫borlandmm.dll,在你写的函数中,使用到了string,那么就可能 use 这个单元,但这只适合delphi调用(内存共享管理单元),建议重构delphi中导出函数,去掉string,改为pchar,或者其它。
当然你可以加入borlandmm单元然后再测试下。

使用delphi进行调试dll,找到报错地址后再进行分析。

顺遍请楼主把调用堆栈贴出来看看把
yao2004jessica 2010-06-23
  • 打赏
  • 举报
回复
我也遇到过这种问题,是不是你的数据量太大了导致有的内存还没有使用就被垃圾回收了呢?
Jellyfancy 2010-06-23
  • 打赏
  • 举报
回复

[DllImport("Project2.dll",CharSet=CharSet.ansi)]

试下行不
Jellyfancy 2010-06-22
  • 打赏
  • 举报
回复
是不是c#中调用函数语句有错?
或者你的dll没放对位置?
Jellyfancy 2010-06-22
  • 打赏
  • 举报
回复
[DllImport("Project2.dll")]
private static extern int InceptData( IntPtr handle);
为什么要用Private呢?
lxclei 2010-06-22
  • 打赏
  • 举报
回复
我调用
[DllImport("Project2.dll")]
private static extern int Init(int CDT);
//释放对象 =0成功;=1失败
[DllImport("Project2.dll")]
private static extern int UnInit();

//主机初始化com口函数 =0成功;=1失败
[DllImport("Project2.dll")]
private static extern int InitCom(string comid);

没有问题
就是刚才我发的那两个函数有问题
lxclei 2010-06-21
  • 打赏
  • 举报
回复
楼主补充
我是调用function InceptData(const handle:THandle):integer;
function ReadInfo(const mark:integer;const id:integer;d1:PChar;d2:PChar;d3:PChar;d4:PChar;d5:PChar;d6:PChar;d7:PChar;
d8:PChar;d9:PChar;d10:PChar;d11:PChar;d12:PChar;d13:PChar;d14:PChar;d15:PChar;d16:PChar):integer;stdcall;
这两个方法出错用C#定义如下
//接收数据函数 =0成功;=1失败 =11 接收失败 =12 接收头错误 =5 超时错误
[DllImport("Project2.dll")]
private static extern int InceptData( IntPtr handle);
function ReadInfo(const mark:integer;const id:integer;d1:PChar;d2:PChar;d3:PChar;d4:PChar;d5:PChar;d6:PChar;d7:PChar;
d8:PChar;d9:PChar;d10:PChar;d11:PChar;d12:PChar;d13:PChar;d14:PChar;d15:PChar;d16:PChar):integer;stdcall;

Jellyfancy 2010-06-21
  • 打赏
  • 举报
回复
1.如果dll中的函数参数含有var,则c#中要加上ref(引用);
否则,会有提示错误:“尝试读取或写入受保护的内容。这通常指示其他内存已损坏”。
平生我自如 2010-06-21
  • 打赏
  • 举报
回复
不会delphi呢

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧