调用C++写的fzyktclient.dll发生错误,提示“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”;

fangxiaofelix 2011-05-31 10:53:21
dll接口描述,见图

下面是我写的调用方法
 public class Class1
{
[DllImport("fzyktclient.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int ykt_opendevice(int devicetype, int com, int baud, ref string region, ref string warnmsg, ref string errmsg);
}
string str_region = "3403";

string ls_warnmsg = "";

string ls_errmsg = "";
static int li_result;
int gi_ndevicetype = 10;
int gi_ncom = 9;
int gi_nbaud = 3;
try
{
li_result = Class1.ykt_opendevice(gi_ndevicetype, gi_ncom, gi_nbaud, ref str_region, ref ls_warnmsg, ref ls_errmsg);
}
catch (ExecutionEngineException ex)
{
MessageBox.Show(ex.ToString());
}


如果看不到图就看下面的接口说明
函数声明 int ykt_opendevice (int nDeviceType,int nCom,int nBaud,char *pRegion,char *pWarnmsg,char *pErrmsg)
用途说明 用于打开读写卡设备。
参数说明 名称 类型长度 说明 备 注
nDeviceType Int 设备类型(10…) 目前类型为10
以后可能会调整
nCom Int 端口(0-9) 0 - com1 1 - com2 …
9 – usb
nBaud Int 波特率(1-4) 1 - 9600 2 - 19200
3 – 28800 4 – 57600
建议用3或者2
pRegion Char(4) 地区编号 3500代表省本级
pWarnmsg Char(1024) 警告信息
pErrmsg Char(1024) 错误信息
返回值
0 成功
<>0 错误,详见pErrmsg
...全文
422 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lpingz 2011-05-31
  • 打赏
  • 举报
回复
感觉像dll中跳出的异常!
ghost5216 2011-05-31
  • 打赏
  • 举报
回复
public static extern int ykt_opendevice(int devicetype, int com, int baud, StringBuilder region, StringBuilder warnmsg, StringBuilder errmsg);

StringBuilder str_region = new StringBuilder(4);
str_region.Add("3403");
StringBuilder ls_warnmsg = new StringBuilder(1024) ;

StringBuilder ls_errmsg = new StringBuilder(1024);


li_result = Class1.ykt_opendevice(gi_ndevicetype, gi_ncom, gi_nbaud, str_region, ls_warnmsg, ls_errmsg);

如StringBuilder 不行 可以试试byte[]
fangxiaofelix 2011-05-31
  • 打赏
  • 举报
回复
各位大侠。求支持啊。。。。
fangxiaofelix 2011-05-31
  • 打赏
  • 举报
回复
已经解决了,不过走了弯路,我把char *pRegion看成是指针类型了,所以传参数用了ref ,其实这是数组,C#调用不用加ref,当我解决的时候,才发现二楼的方法是正解,呵呵,结贴了
fangxiaofelix 2011-05-31
  • 打赏
  • 举报
回复
两种方法都试了,还是不行啊。。[Quote=引用 2 楼 ghost5216 的回复:]
public static extern int ykt_opendevice(int devicetype, int com, int baud, StringBuilder region, StringBuilder warnmsg, StringBuilder errmsg);

StringBuilder str_region = new StringBuilder(4);
st……
[/Quote]

111,125

社区成员

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

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

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