ҡդӶ ʵŧ 纺촨շ٨絴͵ ᷹ФѺ

͵Դҷҧ Inbox Ѻ

˹ѡ
-
VB 6/VB.Net
ASP/ASP.Net
Ѻ¤
¹ҹ Flash Movie
ռͺش  
 RSS Feeds
 ǹŴ RSS Reader ...   Download  RSS Reader

Forum - www.g2gnet.com
Webmaster - www.g2gnet.com
Visitors - Session views
 5 6 1 6 1 7 8

7 ѹҤ ..2549
134 Users On-Line.
Visitors - Page views
 8 9 7 3 9 2 1
1 Ҿѹ ..2551

Google   
g2gnet.com
Ҵѡ:  

ᨡҹ MAC Address ͢ LAN - VB6

Category »  VB 6/VB.Net
: Webmaster 22/6/2553   : 11:55
(ҹ : 19156) 
شʧͧ (ͧ) ҡʹǤԴѺ Network ѹʹͧͧ Programming ͹任ء 㹡èѴ红 ¨ҡͧҧἹ ѧ Switching Hub ˹ Port ˹ Ţ IP/MAC Address ͧͧ١ ᷹к ¡д Excel Ѻ ... 鴵ǹ Ҩͧҡ仺ҧ й͡˹ͨҡҺԵ 亵 ѧͧǾѹѺê˹ Memory Address Pointer ͧ ... §衴 F5 ѹǴҧǤѺ ͧ Debug Program Ҫ´ ... س֧ ͧҾ͡
ͧѺк͢ - Fluke Network Inspector 5.0

ǹŴҹ ... (੾Ҫԡ)

ǹŴ Source Code Ѻҹ Visual Basic 6
ǹŴ Source Code Ѻ MS Visual Basic 6.0 - Service Pack 6
 ǹŴ Visual Basic 6.0 SP5: Run-Time Redistribution Pack
 ǹŴ Microsoft Data Access Object (MDAC) Jet 4.0 Update
 ǹŴ Microsoft Visual Basic Service Pack 6

ŧŢҹ 10 Ţҹ 16 Ţҹ 2 Ţҹ 16 (ǤԴͧҧ)
ԸդԴ B5 (ҹ 16) ѡͧҹ 16 B Ѻ 5 ѹբҴѡ 4 Ե (ҹ 2)
B ҹ 16 = 1 0 1 1 ҹ 2 = 11 ҹ 10
5 ҹ 16 = 0 1 0 1 ҹ 2 = 5 ҹ 10

Ţҹ 2 1 0 1 1 0 1 0 1 (Ҵ 8 Ե 1 亵) 181 (ҹ 10)

ҡسѡù ...
Դ ͹鹵͹ Debug Program MS Visual Basic 6.0
ͧͧ (ա) ... Sub Program, Routine, Procedure Function
¹ MAC Address (Media Access Control Address) Ẻ
VB6 Ѻҹ MAC Address ͧ LAN Card Ըպҹ ()

Design Time


Run Time

ѡ

Option Explicit

Private Sub cmdGetMAC_Click()

    If txtIPAddress.Text = "" Then Exit Sub
    
    ' 觤 IP Address ѧѧ GetMAC
    txtMACAddress.Text = GetMAC(txtIPAddress.Text)
    
    '  Focus ѺŢ IP 
    txtIPAddress.SetFocus
    ' 觡á Control + END ˹觷شͧ TextBox
    SendKeys "^{END}"
End Sub

Private Sub Form_Load()
    ' 駿觡ҧ
    Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
    
    ' ҷͺŧ
    txtIPAddress.Text = "192.168.0.1"
    txtMACAddress.Text = ""

End Sub

Private Sub txtIPAddress_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then Call cmdGetMAC_Click
End Sub
Module - modGetMACLan.bas

Option Explicit

' ѧ蹷ŧŢ IPv4 ŢӹǹẺԴ᷹ͧ ( 0 Ѻ Ţǡ) 
' ҡ 192.168.1.20 С 335653056 ᷹ ҹԧѹеͧżŢҹ 2 ФѺ
Public Declare Function inet_addr Lib "wsock32.dll" ( _
    ByVal s As String) _
    As Long

' ǹ͸Ժ鴴ҹҧѺ
Public Declare Function SendARP Lib "iphlpapi.dll" ( _
    ByVal DestIP As Long, _
    ByVal SrcIP As Long, _
    pMacAddress As Long, _
    pMacAddressLen As Long _
    ) As Long

' ê˹觢ͧ Memory Address
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    Destination As Any, _
    Source As Any, _
    ByVal Length As Long _
    )

' Ẻ Global 㹿 ·ء öҹ
' 纤ҤӵͺѺ
Dim sRemoteMacAddress As String

' ##############################################################
' ѧ㹡õǨͺҤ MAC Address
Public Function GetMAC(strIPAddress As String) As String
' ##############################################################

   
    If Len(strIPAddress) > 0 Then
    
        ' 觤 IP Address Ѻѧ GetRemoteAddress
        ' óöҤ MAC Address  ѧ蹨 Return Ҩԧ (True) Ѻ
        If GetRemoteMACAddress(strIPAddress) Then
            
            GetMAC = sRemoteMacAddress
        ' բͼԴҴ
        Else
            GetMAC = "SendARP Fail"
        End If
    
    End If
    
    sRemoteMacAddress = ""

End Function

' ##############################################################
' 觤 IP Address ѧ ǹѧ蹨Ф׹ҡѺẺ Boolean ҷӧҹ
Function GetRemoteMACAddress( _
    sRemoteIP As String _
    ) As Boolean
' ##############################################################

   Dim dwRemoteIP As Long
   Dim pMacAddress As Long
   '  Pointer ѧ˹觤ҷ MAC Address  6 ش
   Dim bpMacAddress() As Byte
   ' ˹ MAC Address դ 6 ش
   Dim pMacAddrLen  As Long
   Dim Count As Long
    
  ' ¹ IP 繤ҵŢẺ Long ͹ ¿ѧ inet_addr ѧࡵҵǹѹ API
  ' ѧ鴹֧繵ͧ͡ҹ WinSock Control ФѺ
   dwRemoteIP = inet_addr(sRemoteIP)
   
    If dwRemoteIP <> 0 Then
   
        ' ˹شŢҹ 16 ͧ MAC Address  6  (MAC Address ѹբҴ 6 亵)
        pMacAddrLen = 6
   
        '  Remote MAC Address  API32 (ѧ SendARP)
        ' ҹ´
        ' ARP ⵤ Ѻ͢дѺ Data Link Layer (鹷 2)
        ' ˹ҷͧ ARP Ѻ͢ TCP/IP  ŧ IP Address  Hardware Address
        '  MAC Address Ţҹ 16 Ҵ 6 亵  6 ش 1 亵  00-13-D4-9E-5B-DB
        ' ѧࡵ SendARP ѹ Return ҡѺ繵ŢӹǹẺ Long
        ' ҡѹ觤ҡѺ ҡѺ 0 蹤 Դ Error ҹФѺ 騺͡仨ҡѧ
        ' ѭԴҡ˵ѡ  Ţ IP Դ   IP 辺к͢
        If SendARP(dwRemoteIP, 0&, pMacAddress, pMacAddrLen) = 0 Then
      
            ' ͹ AND ҡԹص㴵˹ False ӵͺ False ѹ
            If pMacAddress <> 0 And pMacAddrLen <> 0 Then
        
                ' ѹա觤 Pointer ͪ˹红Ţͧ MAC Address
                ' ѧ鹵ͧѴ红ŧ Array 6 ش  6 ˹
                ReDim bpMacAddress(0 To pMacAddrLen - 1)
                
                ' Ѵ͡˹á   (0) ͧê  Ѻա 6 ˹
                ' ˹ҹ鹹ФѺ ǹҷ  MAC Address Ҵ 1 亵 (ѹ 6 )
                '  00-13-D4-9E-5B-DB Ъش蹴ͧ - բҴ 1 亵  8 Ե
                CopyMemory bpMacAddress(0), pMacAddress, ByVal pMacAddrLen
                
                ' ǹͺҤе˹
                For Count = 0 To pMacAddrLen - 1
                
                    
                    '  ͧͺ Debug  Immediate Windows 繵˹ Address  
                    ' ? VarPtr(bpMacAddress(1))
                    ' ҹҵ˹Ţҹ 10  140001186 
                    ' ? Hex$(VarPtr(bpMacAddress(1)))
                    ' ҹҵ˹Ţҹ 16   8583 FA2 (ͧ  Ф駨͹ѹФѺ)
                    ' ҡҡ MAC Address Ţҹ 16 ˹ҵҧ Immediate Windows
                    ' ? Hex$(bpMacAddress(1)) ... ӵͺ  13 (Ţҹ 16)
                    ' ҡ鹡Ҥҷ Hex$(bpMacAddress(Count)) 㹵˹觷仹͡㹵 Array
                    '  Pointer In The Visual Basic ...  ѹ仵˹ Memory Address Ҥҷ㹵˹觹ҹФѺ
                    If Count = pMacAddrLen - 1 Then
                        ' 繪شش ֧ͧͧ - ҵͷ¤Ѻ
                        sRemoteMacAddress = sRemoteMacAddress & Right("00" & Hex$(bpMacAddress(Count)), 2)
                    Else
                        ' ҹ MAC Address ҡ 5 á ... 鴴ҹҧẺ
                        ' شáѹ 0 ͡§ǹФѺ ... ͧͧ Debug ٨ͧ
                        sRemoteMacAddress = sRemoteMacAddress & Right("00" & Hex$(bpMacAddress(Count)) & "-", 3)
                    End If
                
                Next
                
                ' ׹ҡѺ True ʴöӧҹ١ͧ
                If Len(sRemoteMacAddress) > 0 Then GetRemoteMACAddress = True
                
                ' ͡ҡѧ
                Exit Function
                
            Else
                ' ԴͼԴҴ pMacAddress <> 0 And pMacAddrLen <> 0
                ' ó AND ... ҡ Input 㴵˹ False ӵͺ False 
                GetRemoteMACAddress = False
            End If
            
        Else
            ' ԴͼԴҴ SendARP 觤ҡѺ 0
            GetRemoteMACAddress = False
        End If
      
    Else
        ' ԴͼԴҴ dwRemoteIP ӡ¹ IP դ 0
        GetRemoteMACAddress = False
    End If

End Function
Conclusion:
ͧͧԵ 亵 кŢ ͧӤѭҧҡ͡þѲ 觼š͹ѹ 觷¹ҷҧªҧ ѹҧᵡҧѺ¹ҷҧѭ Ţҹҧ ҡͧŢҹ 10 (ԡءѹ) ѹҨջѭҡѺŢҹ ѹѡǡѹ ... ¹ҡѹԴ Ţҹ 10 ᷹Ҩн֡Ѻ 0 - 9 ´ѹѺ 1 - 10 ᷹ ѹµԴ繤ªԹ Ţҹ ѺѺҡ 0 ҹ 8 0 - 7 ҹ 16 Ѻҡ 0 - 15 ҹ 2 Ѻҡ 0 - 1 ... Ẻ չ仡ѹ 55555+

͵ - g2gNet Dot Com
Ţ¹ҳԪ硷͹ԡ 0407314800231
CopyLeft © 2004 - 2099 g2gNet.Com All rights reserved.
Email: [email protected] . 08-6862-6560