我一个程序在调试的时候没有一点问题,可是当将其编译成.exe时就很容易出错,提示为:“"0x779b9416"指令引用的"0x0015b68a"内存。该内存不能为"read"。要终止程序,请单击"确定"。要调试程序,请单击"取消"。”
这个问题实在是令人难以理解,请问各位大虾你们是否也遇到过类似的情况,这问题又该怎么解决?

解决方案 »

  1.   

    Option ExplicitPrivate Const NO_ERROR = 0Private Declare Function inet_addr Lib "wsock32.dll" (ByVal s As String) As Long
    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
      

  2.   

    我遇到过编译好的exe文件在硬盘上正常运行但是在软盘U盘上出错的问题,错误信息好像也是你这样的。
      

  3.   

    CopyMemory bpMacAddr(0), pMacAddr, ByVal PhyAddrLen就这句出的问题
    对内存操作了
      

  4.   

    我在运行代码时,出错: listitem  没有定义!是不是还有一些代码没有贴出来啊
      

  5.   

    都在这儿listitem是因为我添加了ListView控件
      

  6.   

    怎么在我的机子上面没出错啊,我用的windows 2000 server
      

  7.   

    我用的是Win2000professional我在调试的时候不会出错,可当将其编译成.exe再运行时就会报错。
      

  8.   

    你这问题有点复杂!
    我虽没用过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了
      

  9.   

    测试了一下,证实我的推测是正确的,你一直在用4字节的Long做缓冲接收6字节数据,也就是说你使用了2个字节的未知内存来传递数据,这种错误真是很难发现,因为,只在这2个字节存有其它数据时才会引发“非法操作”,所以错误发生带有很大的随机性!若不细分析,这将成为永久性的超级难题!
    你是从哪抄来的代码?这作者也太不负责了!除上面说的用法外,还可用以下方案: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
    ....