跪求 调用DLL文件出现“ 尝试读取或写入受保护的内存。这通常指示其他内存已损坏” 错误解决办法。
是的这样的,小弟最近在调一套转矩测量测试,无奈对方木有给出通信协议只用动态库文件(DLL),当然也给出了一个VB6.0的程序例子 但是我用vb.net进行测试时总是会出现这个“ 尝试读取或写入受保护的内存。这通常指示其他内存已损坏”错误
vb6.0 源码如下
Dim out_currentA As Double '输出电流定义(控制电流)
Private Declare Sub get_current Lib "WLK3B.dll" (ByVal Port As Integer, ByVal addr_mcu As Integer, in_current As Double)
Private Sub Form_Load()
Call get_current(6, 9, in_currentA) ' 检测电流 “1”表示COM1口 “9”表示WLK-3B的通信地址为9
Label2.Caption = Str(in_currentA)
End Sub
vb.net 源码如下
Public Class Form1
Private Declare Sub get_current Lib "WLK3B.dll" (ByVal Port As Integer, ByVal addr_mcu As Integer, ByVal in_current As Double)
Dim out_currentA As Double '输出电流定义(控制电流)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call get_current(1, 2, out_currentA)
Label2.Text = out_currentA
End Sub
End Class
引用什么的都没有问题但是在运行到 Call get_current(a, b, out_currentA)时 出现错误 = =!我自己分析了一下原因 在vb6下 Integer为16位(2 个字节)的数值形式,而vb.net Integer为32位(4 个字节)的数值形式会不会在vb.net中定义的时候应该这样定义:
Private Declare Sub get_current Lib "WLK3B.dll" (ByVal Port As Int16, ByVal addr_mcu As Int16, ByVal in_current As Double)
但还是不行
Call get_current(1, 2, out_currentA) 中 1 2 好像还是为32位的数值形式出现的就算是之前 Port As Int16 addr_mcu As Int16 无法改为16位 求传道授业解惑!
[解决办法]
VB.NET定义为
Private Declare Sub get_current Lib "WLK3B.dll" (ByVal Port As Integer, ByVal addr_mcu As Integer, ByRef in_current As Double)