[Programming language] Microsoft Visual Basic 4.0, 5.0, 6.0
[Running platform] WINDOWS
[Source code source] http://support.microsoft.com/support/kb/articles/q175/4/72.asp? FR = 0
[Function description] IPX and NETBIOS interfaces require network addresses. This document demonstrates how to obtain the NIC address Through VB through detailed steps.
Steps:
1) generate a standard EXE file in Visual Basic. Form1 is created by default.
2) Add a command button in Form1. The default name is Command1.
3) place the following code in the description section of Form1.
Option Explicit
Private Const NCBASTAT = & H33
Private Const NCBNAMSZ = 16
Private Const HEAP_ZERO_MEMORY = & H8
Private Const HEAP_GENERATE_EXCEPTIONS = & H4
Private Const NCBRESET = & H32
Private Type NCB
Ncb_command As Byte 'integer
Ncb_retcode As Byte 'integer
Ncb_lsn As Byte 'integer
Ncb_num As Byte 'integer
Ncb_buffer As Long 'string
Ncb_length As Integer
Ncb_callname As String * NCBNAMSZ
Ncb_name As String * NCBNAMSZ
Ncb_rto As Byte 'integer
Ncb_sto As Byte 'integer
Ncb_post As Long
Ncb_lana_num As Byte 'integer
Ncb_1__cplt As Byte 'integer
Ncb_reserve (9) As Byte 'reserved, must be 0
Ncb_event As Long
End Type
Private Type ADAPTER_STATUS
Adapter_address (5) As Byte 'As String * 6
Rev_major As Byte 'integer
Reserved0 As Byte 'integer
Adapter_type As Byte 'integer
Rev_minor As Byte 'integer
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_eclip_sess As Integer
Max_sess As Integer
Max_sess_pkt_size As Integer
Name_count As Integer
End Type
Private Type NAME_BUFFER
Name As String * NCBNAMSZ
Name_num As Integer
Name_flags As Integer
End Type
Private Type ASTAT
Adapt As ADAPTER_STATUS
NameBuff (30) As NAME_BUFFER
End Type
Private Declare Function Netbios Lib "netapi32.dll" (pncb As NCB) As Byte
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory "_
(HpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32 "_
(ByVal hHeap As Long, ByVal dwFlags As Long ,_
ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long ,_
ByVal dwFlags As Long, lpMem As Any) As Long
Put the following code into the commandmediaclick event:
Private Sub commandementclick ()
Dim myNcb As NCB
Dim bRet As Byte
MyNcb. ncb_command = NCBRESET
BRet = Netbios (myNcb)
MyNcb. ncb_command = NCBASTAT
MyNcb. ncb_lana_num = 0
MyNcb. ncb_callname = "*"
Dim myASTAT As ASTAT, tempASTAT As ASTAT
Dim pASTAT As Long
MyNcb. ncb_length = Len (myASTAT)
Debug. Print Err. LastDllError
PASTAT = HeapAlloc (GetProcessHeap (), HEAP_GENERATE_EXCEPTIONS _
Or HEAP_ZERO_MEMORY, myNcb. ncb_length)
If pASTAT = 0 Then
Debug. Print "memory allcoation failed! "
Exit Sub
End If
MyNcb. ncb_buffer = pASTAT
BRet = Netbios (myNcb)
Debug. Print Err. LastDllError
CopyMemory myASTAT, myNcb. ncb_buffer, Len (myASTAT)
MsgBox Hex (myASTAT. adapt. adapter_address (0 ))&""&_
Hex (myASTAT. adapt. adapter_address (1 ))_
& "" & Hex (myASTAT. adapt. adapter_address (2 ))&""_
& Hex (myASTAT. adapt. adapter_address (3 ))_
& "" & Hex (myASTAT. adapt. adapter_address (4 ))&""_
& Hex (myASTAT. adapt. adapter_address (5 ))
HeapFree GetProcessHeap (), 0, pASTAT
End Sub
4) Press F5 to run the program.
5) Click Command1. Note: The NIC address is displayed in the information box.