C++的头文件定义如下:
//读输入寄存器
DWORD __stdcall ZMB_ReadInputReg( HANDLE hand,
BYTE serID,
int nStart,
int nCount,
WORD * bRet/*arr of reg*/,
WORD TranID);
C#中我是下面这样写的:
//读输入寄存器,读AI
[DllImport("ZModbusSdk.dll")]
static extern UInt32 ZMB_ReadInputReg(UInt32 hand,
byte serID,
int nStart,
int nCount,
ref short bRet,
ushort TranID);
调用时我是这么用的:short tem = 0;
ZMB_ReadInputReg(hand, 3, 0x40, 1, ref tem, 0) 程序运行不了多久会出现错误,当我把调用的这句代码注释掉后,就不会出现错误了。
//读输入寄存器
DWORD __stdcall ZMB_ReadInputReg( HANDLE hand,
BYTE serID,
int nStart,
int nCount,
WORD * bRet/*arr of reg*/,
WORD TranID);
C#中我是下面这样写的:
//读输入寄存器,读AI
[DllImport("ZModbusSdk.dll")]
static extern UInt32 ZMB_ReadInputReg(UInt32 hand,
byte serID,
int nStart,
int nCount,
ref short bRet,
ushort TranID);
调用时我是这么用的:short tem = 0;
ZMB_ReadInputReg(hand, 3, 0x40, 1, ref tem, 0) 程序运行不了多久会出现错误,当我把调用的这句代码注释掉后,就不会出现错误了。
[DllImport("ZModbusSdk.dll")]
static extern UInt32 ZMB_ReadInputReg(UInt32 hand, byte serID, int nStart, int nCount, ref short bRet, short TranID);
static extern UInt32 ZMB_ReadInputReg(IntPtr hand,
byte serID,
int nStart,
int nCount,
ref ushort bRet,
ushort TranID);
[DllImport("ZModbusSdk.dll")]
static extern UInt32 ZMB_ReadInputReg(UInt32 hand,
byte serID,
int nStart,
int nCount,
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 这里初始化大小)]
ref short bRet,
ushort TranID);
上面的错了,,需要加上[],另外你也可以采用动态分配该指针的大小,然后封送一个Intptr类型
不过,编码复杂了。//读输入寄存器,读AI
[DllImport("ZModbusSdk.dll")]
static extern UInt32 ZMB_ReadInputReg(UInt32 hand,
byte serID,
int nStart,
int nCount,
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 这里初始化大小)]
ref short[] bRet,
ushort TranID);
APP.vshost.exe-应用程序错误
应用程序发生异常 未知的软件异常(0xc0000409),位置为0x7a0ca47e
APP.vshost.exe-应用程序错误
应用程序发生异常 未知的软件异常(0xc0000409),位置为0x7a0ca47e
Public Declare Function GetFirmwareVersion Lib "Mr3001ADll.dll" (ByVal hCom As Long, ByRef major As Integer, ByRef minor As Integer, ByVal NetAddr As Integer) As IntegerC#中引用
[DllImport("Mr3001ADll.dll")]
public static extern int GetFirmwareVersion(
long hCom,
ref long major,
ref long minor,
byte NetAddr);C#中调用:
stutas = RfidApiLib.GetFirmwareVersion(hCom, ref major, ref minor, NetAddr)报错:未处理的“System.AccessViolationException”类型的异常出现在 sdkq.exe 中。其他信息: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。路过的朋友帮忙解决一下。不要让贴沉了
这条语句时,因为他是读外面模块里寄存器的值,需要几毫秒的时间,当该条语句还没有执行完得时候,该线程的时间片用完了,CPU执行其他线程去了,当回来时一些寄存器的值变了,所以导致错误!C#中如何能保证该条语句执行过程中不被其他线程打断!
ZMB_ReadInputReg(m_hEthMaster,3,0x40,1,&temp,0);
没有出过错!
大侠们来帮帮忙啊!
static extern UInt32 ZMB_ReadInputReg(IntPtr hand,
byte serID,
int nStart,
int nCount,
ushort[] bRet,
ushort TranID);