急,求解,C#调用VC++DLL,出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
VC++ 方法int API_PCD(HANDLE commHandle,
int DeviceAddress,
unsigned char mode,
unsigned char blk_add,
unsigned char num_blk,
unsigned char *snr,
unsigned char *buffer);
C#中调用
声明:
[DllImport("MMCD.dll", EntryPoint = "API_PCD")]
static extern int API_PCD(long hcom, int devAddr, byte mode, int blk_Addr, int Num_blk, ref asnr, ref aRecvBuffer);
定义变量
int ret;
int blk_Addr=10;
int devAddr=0;
byte mode = 0;
byte Num_blk=1;
byte asnr= new byte();
byte aRecvBuffer = new byte();
long hcomm=752;
调用方法
ret = API_PCD(hcomm, devAddr, mode, blk_Addr, Num_blk, ref asnr, ref aRecvBuffer);
调试时,运行到此方法,出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”的错误,在网上也找了,但是没有得到解决
后来把hcomm更改为IntPtr类型后,运行到此方法,程序就不响应了,请教各位高手,这样的情况该怎么解决,先谢谢了 --------------------编程问答-------------------- 那几个char*是数组啊还是单个字符啊 --------------------编程问答-------------------- 要先弄清楚需要多大的接收缓冲区,这个错误是内存操作越界 --------------------编程问答--------------------
我在定义的时候也用过了byte[],一样的报错
byte[] asnr= new byte[20];
byte[] aRecvBuffer = new byte[2048];
调用方法
ret = API_PCD(hcomm, devAddr, mode, blk_Addr, Num_blk, ref asnr, ref aRecvBuffer);
请问,如果这样写错了,那么需要怎么样写才正确,谢谢 --------------------编程问答-------------------- API_PCD函数 dll 里面是怎么定义 --------------------编程问答-------------------- unsigned char 在C#应该是 byte类型. 其他的会 越界 移除 吧 --------------------编程问答--------------------
VC++ DLL中的方法
int API_PCD(HANDLE commHandle,
int DeviceAddress,
unsigned char mode,
unsigned char blk_add,
unsigned char num_blk,
unsigned char *snr,
unsigned char *buffer);
--------------------编程问答--------------------
--------------------编程问答--------------------
//ref asnr
//ref aRecvBuffer
//用byte[]试试
试了,还是一样报错 --------------------编程问答-------------------- 另外这个在VB6中可以调用
VB6的代码如下
定义
Declare Function API_PCD Lib "MMCD.dll" _
(ByVal handle As Integer, ByVal deviceAddr As Integer, _
ByVal mode As Byte, ByVal blk_Addr As Byte, ByVal Num_blk As Byte, _
ByRef snr As Byte, _
ByRef Buffer As Byte) As Integer
Dim ret As Integer
Dim blk_Addr, devAddr As Integer
Dim mode, Num_blk As Byte
Dim asnr(20) As Byte
Dim aRecvBuffer(2048) As Byte
Dim hcomm As long
ret = API_PCD(hComm, devAddr, mode, blk_Addr, Num_blk, asnr(0), aRecvBuffer(0))
--------------------编程问答-------------------- 前段时间我也遇到过这样的问题,给你看看我调试运行成功的结果:
c# 结构体:
[StructLayout(LayoutKind.Sequential,Pack=1)]
public struct AccountMsg
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=21)]
public string Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public string SexNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 19)]
public string DeptCode; /*部门代码*/
[MarshalAs(UnmanagedType.U4)]
public UInt32 CardNo; /*卡号*/
[MarshalAs(UnmanagedType.U4)]
public UInt32 AccountNo; /*帐号*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string StudentCode; /*学号*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string IDCard; /*身份证号*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]
public string PID; /*身份代码*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public string IDNo; /*身份序号*/
[MarshalAs(UnmanagedType.I4)]
public Int32 Balnce; /*现余额*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string Password; /*消费密码*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string ExpireDate; /*账户截止日期*/
[MarshalAs(UnmanagedType.U2)]
public UInt16 SubSeq; /*补助戳*/
public char IsOpenInSys; /*是否在本系统内开通*/
[MarshalAs(UnmanagedType.I2)]
public Int16 TerminalNo; /*终端号码,提取补助时需要填写*/
[MarshalAs(UnmanagedType.I2)]
public Int16 RetCode; /*后台处理返回值*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string Flag; /*状态(2004-08-26增加)*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 84)]
public string Pad; /*预留字段*/
}
c++ 结构体:
typedef struct
{
char Name[21]; /*姓名*/
char SexNo[2]; /*性别*/
char DeptCode[19]; /*部门代码*/
unsigned int CardNo; /*卡号*/
unsigned int AccountNo; /*帐号*/
char StudentCode[21]; /*学号*/
char IDCard[21]; /*身份证号*/
char PID[3]; /*身份代码*/
char IDNo[13]; /*身份序号*/
int Balance; /*现余额*/
char Password[7]; /*消费密码*/
char ExpireDate[7]; /*账户截止日期*/
unsigned short SubSeq; /*补助戳*/
char IsOpenInSys; /*是否在本系统内开通*/
short TerminalNo; /*终端号码,提取补助时需要填写*/
short RetCode; /*后台处理返回值*/
char Flag[16]; /*状态(2004-08-26增加)*/
char Pad[84]; /*预留字段*/
} AccountMsg;
补充:.NET技术 , C#