我一个程序在调试的时候没有一点问题,可是当将其编译成.exe时就很容易出错,提示为:“"0x779b9416"指令引用的"0x0015b68a"内存。该内存不能为"read"。要终止程序,请单击"确定"。要调试程序,请单击"取消"。”
这个问题实在是令人难以理解,请问各位大虾你们是否也遇到过类似的情况,这问题又该怎么解决?
这个问题实在是令人难以理解,请问各位大虾你们是否也遇到过类似的情况,这问题又该怎么解决?
Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Long, PhyAddrLen As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal bcount As Long)Private Function GetMACAddress(ByVal sRemoteIP As String) As String
On Error Resume Next
Dim dwRemoteIP As Long
Dim pMacAddr As Long
Dim bpMacAddr() As Byte
Dim PhyAddrLen As Long
Dim cnt As Long
Dim tmp As String
dwRemoteIP = inet_addr(sRemoteIP)
If dwRemoteIP <> 0 Then
PhyAddrLen = 6
If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = NO_ERROR Then
If pMacAddr <> 0 And PhyAddrLen <> 0 Then
ReDim bpMacAddr(0 To PhyAddrLen - 1)
CopyMemory bpMacAddr(0), pMacAddr, ByVal PhyAddrLen
tmp = Format(bpMacAddr(0), "00")
For cnt = 1 To PhyAddrLen - 1
tmp = tmp & "-" & Format(Hex$(bpMacAddr(cnt)), "00")
Next cnt
If Len(tmp) > 0 Then
GetMACAddress = tmp
End If
Exit Function
Else
GetMACAddress = "No Find"
End If
Else
GetMACAddress = "No Find"
End If
Else
GetMACAddress = "No Find"
End If
End FunctionPrivate Sub Command1_Click()
Dim stIp As Integer, enIp As Integer, bIp As String
Dim i As Integer
ChangeIp Text1, bIp, stIp
ChangeIp Text2, vbNullString, enIp
If stIp > enIp Then
MsgBox "您输入的IP地址范围不正确!", vbCritical, "错误"
Exit Sub
End If
For i = stIp To enIp
AddData bIp & i, GetMACAddress(bIp & i)
Next i
End SubPrivate Sub AddData(ByVal a1 As String, ByVal a2 As String)
Dim x As ListItem
Set x = ListView1.ListItems.Add(, , a1)
x.ListSubItems.Add , , a2
Set x = Nothing
End SubPrivate Sub ChangeIp(ByVal IPAddress As String, HeadIp As String, LastIp As Integer)
Dim i As Integer
Dim IpLen As Integer
IPAddress = Trim(IPAddress)
IpLen = Len(IPAddress)
i = IpLen - 1
Do While i >= IpLen - 3
If Mid(IPAddress, i, 1) = "." Then
LastIp = Val(Right(IPAddress, IpLen - i))
HeadIp = Left(IPAddress, i)
Exit Do
End If
i = i - 1
Loop
End Sub
对内存操作了
我虽没用过SendARP这个函数,但明显感觉,你的用法有问题:
声明pMacAddr As Long,看似为一个指针,但后面CopyMemory却没有Byval传递,这样将一个4字节的Long型数复制给个6字节的Byte数组,那其后面2字节将是一个未知东东!从你在调试时能正常取值,可推断,这个API要求的不是指针,而是一个6字节的缓冲区,你每次只传一个4字节Long空间给它,它却按6字节去填,在IDE环境下,内存空余较多,2个字节出界引起麻烦的概率很低,但编译后没有VB大环境保护,越界读写出错可能就会高得多。基于以上分析,你可这样改:
Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, pMacAddr As Any, PhyAddrLen As Long) As Long
'声明为Any
....
PhyAddrLen = 6
ReDim bpMacAddr(0 To PhyAddrLen - 1)
If SendARP(dwRemoteIP, 0&, bpMacAddr(0), PhyAddrLen) = NO_ERROR Then
If PhyAddrLen <> 0 Then
.....无需再CopyMemory了
你是从哪抄来的代码?这作者也太不负责了!除上面说的用法外,还可用以下方案:Private Declare Function SendARP Lib "iphlpapi.dll" (ByVal DestIP As Long, ByVal SrcIP As Long, ByVal pMacAddr As Long, PhyAddrLen As Long) As Long
'声明为Byval Long
.....
If SendARP(dwRemoteIP, 0&, VarPtr(bpMacAddr(0)), PhyAddrLen) = NO_ERROR Then
....