[VB] MAC-adres opvragen in Visual Basic

Pagina: 1
Acties:
  • 117 views sinds 30-01-2008
  • Reageer

  • nIghtorius
  • Registratie: Juli 2002
  • Laatst online: 26-08 12:20
Heb het internet al afgegoogled zonder resultaat (eigenlijk met, maar niet wat ik zoek)..

wat wil ik:

de MAC adres opvragen van de NIC-kaart onder Visual Basic 6.0. zowel onder W98/2000 en XP.

wat ik gevonden heb:

Oplossingen waarbij het alleen onder 2000/XP werkt.

Ben nu een beetje wanhopig aan het worden. is het uberhaupt wel mogelijk om een MAC adres in Visual Basic te achterhalen dat in Win98/2000/XP werkt?

Ryzen 9 5900X @ 5.1Ghz | MPG B550 GAMING CARBON | 96GB DDR4-3200 | RTX 4070TI | 2TB + 1TB m.2 SSD | 3x 1TB HDD | 1x 2TB SATA SSD | 32" G3223Q (4K/144Hz)


Verwijderd

Middels SendARP van de IP Helper API kan je inderdaad geen adres opvragen van 9x of NT.

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 Private Const NO_ERROR = 0

Private 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 Sub Form_Load()

   Text1.Text = "192.168.1.101"
   Text2.Text = ""
   Command1.Caption = "Get Remote Mac Address"
   
End Sub


Private Sub Command1_Click()

   Dim sRemoteMacAddress As String
   
   If Len(Text1.Text) > 0 Then
   
      If GetRemoteMACAddress(Text1.Text, sRemoteMacAddress) Then
         Text2.Text = sRemoteMacAddress
      Else
         Text2.Text = "(SendARP call failed)"
      End If
      
   End If

End Sub


Private Function GetRemoteMACAddress(ByVal sRemoteIP As String, _
                                     sRemoteMacAddress As String) As Boolean

   Dim dwRemoteIP As Long
   Dim pMacAddr As Long
   Dim bpMacAddr() As Byte
   Dim PhyAddrLen As Long
   Dim cnt As Long
   Dim tmp As String
    
  'convert the string IP into
  'an unsigned long value containing
  'a suitable binary representation
  'of the Internet address given
   dwRemoteIP = inet_addr(sRemoteIP)
   
   If dwRemoteIP <> 0 Then
   
     'set PhyAddrLen to 6
      PhyAddrLen = 6
   
     'retrieve the remote MAC address
      If SendARP(dwRemoteIP, 0&, pMacAddr, PhyAddrLen) = NO_ERROR Then
      
         If pMacAddr <> 0 And PhyAddrLen <> 0 Then
      
           'returned value is a long pointer
           'to the mac address, so copy data
           'to a byte array
            ReDim bpMacAddr(0 To PhyAddrLen - 1)
            CopyMemory bpMacAddr(0), pMacAddr, ByVal PhyAddrLen
          
           'loop through array to build string
            For cnt = 0 To PhyAddrLen - 1
               
               If bpMacAddr(cnt) = 0 Then
                  tmp = tmp & "00-"
               Else
                  tmp = tmp & Hex$(bpMacAddr(cnt)) & "-"
               End If
         
            Next
           
           'remove the trailing dash
           'added above and return True
            If Len(tmp) > 0 Then
               sRemoteMacAddress = Left$(tmp, Len(tmp) - 1)
               GetRemoteMACAddress = True
            End If

            Exit Function
         
         Else
            GetRemoteMACAddress = False
         End If
            
      Else
         GetRemoteMACAddress = False
      End If  'SendARP
      
   Else
      GetRemoteMACAddress = False
   End If  'dwRemoteIP
      
End Function
 


Maar via NetBIOS lukt het wel voor NT en 9x

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Public Const NCBASTAT As Long = &H33
Public Const NCBNAMSZ As Long = 16
Public Const HEAP_ZERO_MEMORY As Long = &H8
Public Const HEAP_GENERATE_EXCEPTIONS As Long = &H4
Public Const NCBRESET As Long = &H32

Public Type NET_CONTROL_BLOCK  'NCB
   ncb_command    As Byte
   ncb_retcode    As Byte
   ncb_lsn        As Byte
   ncb_num        As Byte
   ncb_buffer     As Long
   ncb_length     As Integer
   ncb_callname   As String * NCBNAMSZ
   ncb_name       As String * NCBNAMSZ
   ncb_rto        As Byte
   ncb_sto        As Byte
   ncb_post       As Long
   ncb_lana_num   As Byte
   ncb_cmd_cplt   As Byte
   ncb_reserve(9) As Byte 'Reserved, must be 0
   ncb_event      As Long
End Type

Public Type ADAPTER_STATUS
   adapter_address(5) As Byte
   rev_major         As Byte
   reserved0         As Byte
   adapter_type      As Byte
   rev_minor         As Byte
   duration          As Integer
   frmr_recv         As Integer
   frmr_xmit         As Integer
   iframe_recv_err   As Integer
   xmit_aborts       As Integer
   xmit_success      As Long
   recv_success      As Long
   iframe_xmit_err   As Integer
   recv_buff_unavail As Integer
   t1_timeouts       As Integer
   ti_timeouts       As Integer
   Reserved1         As Long
   free_ncbs         As Integer
   max_cfg_ncbs      As Integer
   max_ncbs          As Integer
   xmit_buf_unavail  As Integer
   max_dgram_size    As Integer
   pending_sess      As Integer
   max_cfg_sess      As Integer
   max_sess          As Integer
   max_sess_pkt_size As Integer
   name_count        As Integer
End Type
   
Public Type NAME_BUFFER
   name        As String * NCBNAMSZ
   name_num    As Integer
   name_flags  As Integer
End Type

Public Type ASTAT
   adapt          As ADAPTER_STATUS
   NameBuff(30)   As NAME_BUFFER
End Type

Public Declare Function Netbios Lib "netapi32" _
   (pncb As NET_CONTROL_BLOCK) As Byte
     
Public Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (hpvDest As Any, ByVal _
   hpvSource As Long, ByVal _
   cbCopy As Long)
     
Public Declare Function GetProcessHeap Lib "kernel32" () As Long

Public Declare Function HeapAlloc Lib "kernel32" _
  (ByVal hHeap As Long, _
   ByVal dwFlags As Long, _
   ByVal dwBytes As Long) As Long
     
Public Declare Function HeapFree Lib "kernel32" _
  (ByVal hHeap As Long, _
   ByVal dwFlags As Long, _
   lpMem As Any) As Long


Public Function GetMACAddress() As String

  'retrieve the MAC Address for the network controller
  'installed, returning a formatted string
   
   Dim tmp As String
   Dim pASTAT As Long
   Dim NCB As NET_CONTROL_BLOCK
   Dim AST As ASTAT

  'The IBM NetBIOS 3.0 specifications defines four basic
  'NetBIOS environments under the NCBRESET command. Win32
  'follows the OS/2 Dynamic Link Routine (DLR) environment.
  'This means that the first NCB issued by an application
  'must be a NCBRESET, with the exception of NCBENUM.
  'The Windows NT implementation differs from the IBM
  'NetBIOS 3.0 specifications in the NCB_CALLNAME field.
   NCB.ncb_command = NCBRESET
   Call Netbios(NCB)
   
  'To get the Media Access Control (MAC) address for an
  'ethernet adapter programmatically, use the Netbios()
  'NCBASTAT command and provide a "*" as the name in the
  'NCB.ncb_CallName field (in a 16-chr string).
   NCB.ncb_callname = "*               "
   NCB.ncb_command = NCBASTAT
   
  'For machines with multiple network adapters you need to
  'enumerate the LANA numbers and perform the NCBASTAT
  'command on each. Even when you have a single network
  'adapter, it is a good idea to enumerate valid LANA numbers
  'first and perform the NCBASTAT on one of the valid LANA
  'numbers. It is considered bad programming to hardcode the
  'LANA number to 0 (see the comments section below).
   NCB.ncb_lana_num = 0
   NCB.ncb_length = Len(AST)
   
   pASTAT = HeapAlloc(GetProcessHeap(), _
                      HEAP_GENERATE_EXCEPTIONS Or _
                      HEAP_ZERO_MEMORY, _
                      NCB.ncb_length)
            
   If pASTAT = 0 Then
      Debug.Print "memory allocation failed!"
      Exit Function
   End If
   
   NCB.ncb_buffer = pASTAT
   Call Netbios(NCB)
   
   CopyMemory AST, NCB.ncb_buffer, Len(AST)
   
   tmp = Right$("00" & Hex(AST.adapt.adapter_address(0)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(1)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(2)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(3)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(4)), 2) & " " & _
         Right$("00" & Hex(AST.adapt.adapter_address(5)), 2)
                    
   HeapFree GetProcessHeap(), 0, pASTAT
   
   GetMACAddress = tmp

End Function

  • nIghtorius
  • Registratie: Juli 2002
  • Laatst online: 26-08 12:20
thx voor de nfo.. ik kon alleen maar de 1e methode vinden..

edit ---

Werkt als een trein.. vet bedankt joh! :*)

[ Voor 33% gewijzigd door nIghtorius op 05-01-2003 17:46 ]

Ryzen 9 5900X @ 5.1Ghz | MPG B550 GAMING CARBON | 96GB DDR4-3200 | RTX 4070TI | 2TB + 1TB m.2 SSD | 3x 1TB HDD | 1x 2TB SATA SSD | 32" G3223Q (4K/144Hz)


Verwijderd

YW ;)

een eigen KB erop nahouden helpt af en toe ;)

  • Sponge
  • Registratie: Januari 2002
  • Laatst online: 15-08 11:47

Sponge

Serious Game Developer

Nog even een URL toevoegen voor de search:

http://www.mvps.org/vbnet/code/network/index.html

Kun je voor vele netwerk dingen code vinden. (denk dat bovenstaande code best wel daar vandaan kan komen ;))