使用IP Helper函数列出网卡信息MAC,IP。
Adapter Name: {14CAF3AE-5B24-40C3-973C-63C0B55AD224} Description: Realtek RTL8101E Family PCI-E FE NIC - Packet Scheduler MiniportIP Address: 192.168.18.110 Sub Mask: 255.255.255.0 Gateway: 192.168.18.254 DHCP: 192.168.18.254 MAC: 00-03-25-31-CE-C0
; NetworkInfo.asm -- How to get the network address information.
; Created: 6/24/2008 Perry
.386
.model flat, stdcall
option casemap :none
;some of the necessary header&lib files
include windows.inc
include user32.inc
include kernel32.inc
include iphlpapi.inc
includelib user32.lib
includelib kernel32.lib
includelib iphlpapi.lib
WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD ;The initial entry point in this application.
WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD ;This function that processes messages sent to window.
ReadAddrInfo PROTO ;Reads all network adapters information for local computer.
ShowAddrDesc PROTO :DWORD ;Show the address information of network adapter on screen.
GetIPAddrString PROTO :DWORD,:DWORD,:DWORD ;Convert the IP_ADDRESS_STRING struct to IP address string.
GetMACString PROTO :DWORD,:DWORD,:DWORD,:DWORD ;Convert the bytes array to physical address string
IP_ADDRESS_STRING STRUCT
String db 16 dup (?)
IP_ADDRESS_STRING ENDS
IP_MASK_STRING textequ <IP_ADDRESS_STRING>
IP_ADDR_STRING STRUCT
Next dd ?
IpAddress IP_ADDRESS_STRING <16 dup (?)>
IpMask IP_MASK_STRING <>
Context dd ?
IP_ADDR_STRING ENDS
IP_ADAPTER_INFO STRUCT DWORD
Next dd ?
ComboIndex dd ?
AdapterName db 260 dup (?)
Description db 132 dup (?)
AddressLength dd ?
Address db 8 dup (?)
Index dd ?
uType dd ?
DhcpEnabled dd ?
CurrIpAddress dd ?
IpAddressList IP_ADDR_STRING <?>
GatewayList IP_ADDR_STRING <?>
DhcpServer IP_ADDR_STRING <?>
HaveWins dd ?
PWinsServer IP_ADDR_STRING <?>
SWinsServer IP_ADDR_STRING <?>
LeaseObtained dd ?
LeaseExpires dd ?
IP_ADAPTER_INFO ENDS
.const
IDC_EDIT equ 2930
IDC_COMBOBOX equ 2931
.data
szWndTitle db "Network Adapter Information", 0
szEditClass db "Edit", 0
szComboClass db "ComboBox", 0
szWndClass db "ClassSimpleWindow", 0
CommandLine dd 0
hInstance dd 0
hWnd dd 0
hEdit dd 0
hComboBox dd 0
hAddrInfo dd 0
szAddrDesc db 512 dup(?)
szDescFormat db "Adapter Name:", 9, "%s", 13, 10
db "Description:", 9, "%s", 13, 10
db "IP Address:", 9, "%s", 13, 10
db "Sub Mask:", 9, "%s", 13, 10
db "Gateway:", 9, 9, "%s", 13, 10
db "DHCP:", 9, 9, "%s", 13, 10
db "MAC:", 9, 9, "%s", 13, 10, 0
.code
Swdl_AppStart:
INVOKE GetModuleHandle, NULL
mov hInstance, eax
INVOKE GetCommandLine
mov CommandLine, eax
INVOKE WinMain, hInstance, NULL, CommandLine, SW_SHOWDEFAULT
INVOKE ExitProcess, eax
WinMain proc hInst:DWORD, hPrevInst:DWORD, CmdLine:DWORD, CmdShow:DWORD
LOCAL wcx :WNDCLASSEX
LOCAL msg :MSG
mov wcx.cbSize, sizeof WNDCLASSEX
mov wcx.style, CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
mov wcx.lpfnWndProc, offset WndProc
mov wcx.cbClsExtra, NULL
mov wcx.cbWndExtra, NULL
push hInst
pop wcx.hInstance
mov wcx.hbrBackground, COLOR_BTNFACE + 1
mov wcx.lpszMenuName, NULL
mov wcx.lpszClassName, offset szWndClass
INVOKE LoadIcon, hInst, IDI_APPLICATION
mov wcx.hIcon, eax
INVOKE LoadCursor, NULL, IDC_ARROW
mov wcx.hCursor, eax
mov wcx.hIconSm, 0
INVOKE RegisterClassEx, addr wcx
cmp eax, NULL
je Exception
INVOKE GetSystemMetrics, SM_CXSCREEN
mov ebx, eax
shr ebx, 1
sub ebx, 280
INVOKE GetSystemMetrics, SM_CYSCREEN
mov ecx, eax
shr ecx, 1
sub ecx, 100
INVOKE CreateWindowEx, 0, addr szWndClass, addr szWndTitle, \
WS_OVERLAPPED or WS_CAPTION or WS_SYSMENU or WS_THICKFRAME or WS_MINIMIZEBOX, \
ebx, ecx, 500, 200, NULL, NULL, hInst, NULL
cmp eax, NULL
je Exception
mov hWnd, eax
INVOKE ShowWindow, hWnd, SW_SHOWNORMAL
INVOKE UpdateWindow, hWnd
@@:
INVOKE GetMessage, addr msg, NULL, 0, 0
cmp eax, 0
je @F
INVOKE TranslateMessage, addr msg
INVOKE DispatchMessage, addr msg
jmp @B
@@:
cmp hAddrInfo, 0
je @F
INVOKE GetProcessHeap
INVOKE HeapFree, eax, 0, hAddrInfo
@@:
mov eax, msg.wParam
ret
Exception:
INVOKE MessageBox, NULL, addr ExceptMsg, addr szWndTitle, MB_OK
INVOKE GetLastError
ret
WinMain endp
WndProc proc hWin:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
LOCAL hDC :DWORD
LOCAL Ps :PAINTSTRUCT
LOCAL rt :RECT
LOCAL crt :RECT
.if uMsg == WM_CREATE
INVOKE CreateWindowEx, WS_EX_CLIENTEDGE, addr szComboClass, NULL, \
WS_VISIBLE or WS_BORDER or WS_CHILD or CBS_DROPDOWNLIST, CW_USEDEFAULT, \
CW_USEDEFAULT, CW_USEDEFAULT, 240, hWin, IDC_COMBOBOX, hInstance, NULL
mov hComboBox, eax
INVOKE CreateWindowEx, WS_EX_CLIENTEDGE, addr szEditClass, NULL, \
WS_VISIBLE or WS_BORDER or WS_CHILD or ES_MULTILINE or ES_AUTOVSCROLL or ES_AUTOHSCROLL or ES_READONLY, \
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWin, IDC_EDIT, hInstance, NULL
mov hEdit, eax
INVOKE ReadAddrInfo
.elseif uMsg == WM_SIZE
INVOKE IsIconic, hWin
cmp eax, 0
jne @F
INVOKE IsWindow, hComboBox
cmp eax, 0
je @F
INVOKE IsWindow, hEdit
cmp eax, 0
je @F
INVOKE GetClientRect, hComboBox, addr crt
INVOKE GetClientRect, hWin, addr rt
mov eax, rt.right
sub eax, rt.left
INVOKE MoveWindow, hComboBox, rt.left, rt.top, eax, 240, 01H
mov eax, rt.right
sub eax, rt.left
mov ebx, rt.bottom
sub ebx, crt.bottom
INVOKE MoveWindow, hEdit, rt.left, crt.bottom, eax, ebx, 01H
@@:
.elseif uMsg == WM_COMMAND
mov eax, lParam
cmp eax, hComboBox
jne @F
mov eax, wParam
cmp ax, IDC_COMBOBOX
jne @F
shr eax, 16
cmp ax, CBN_SELCHANGE
jne @F
INVOKE SendMessage, hComboBox, CB_GETCURSEL, 0, 0
INVOKE ShowAddrDesc, eax
@@:
.elseif uMsg == WM_CLOSE
INVOKE DestroyWindow, hWin
.elseif uMsg == WM_DESTROY
INVOKE PostQuitMessage, 0
mov eax, 0
ret
.endif
INVOKE DefWindowProc, hWin, uMsg, wParam, lParam
ret
WndProc endp
ReadAddrInfo proc
LOCAL hHeap :DWORD
LOCAL xBytes :DWORD
LOCAL xReturn :DWORD
jmp @F
szErrInfo1 db "No adapter information exists for the local computer.", 0
szErrInfo2 db "This program is not supported by the operating system running on the local computer.", 0
szErrInfo3 db "The buffer size indicated by the request value is too small to get the adapter information.", 0
@@:
INVOKE GetProcessHeap
mov hHeap, eax
cmp hAddrInfo, 0
je @F
INVOKE HeapFree, hHeap, 0, hAddrInfo
xor eax, eax
mov hAddrInfo, eax
@@:
mov eax, sizeof IP_ADAPTER_INFO
mov xBytes, eax
INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, xBytes
mov hAddrInfo, eax
INVOKE GetAdaptersInfo, hAddrInfo, addr xBytes
cmp eax, ERROR_BUFFER_OVERFLOW
jne @F
INVOKE HeapReAlloc, hHeap, HEAP_ZERO_MEMORY, hAddrInfo, xBytes
mov hAddrInfo, eax
INVOKE GetAdaptersInfo, hAddrInfo, addr xBytes
@@:
mov xReturn, eax
cmp xReturn, ERROR_SUCCESS
jne @F
mov edi, hAddrInfo
assume edi:PTR IP_ADAPTER_INFO
.while edi
INVOKE SendMessage, hComboBox, CB_ADDSTRING, 0, addr [edi].Description
mov edi, [edi].Next
.endw
assume edi:nothing
INVOKE SendMessage, hComboBox, CB_GETCOUNT, 0, 0
cmp eax, 0
jle @F
INVOKE SendMessage, hComboBox, CB_SETCURSEL, 0, 0
cmp eax, CB_ERR
je @F
INVOKE ShowAddrDesc, eax
mov eax, 0
ret
@@:
.if xReturn == ERROR_NO_DATA
INVOKE MessageBox, hWnd, addr szErrInfo1, addr szWndTitle, MB_OK
.elseif xReturn == ERROR_NOT_SUPPORTED
INVOKE MessageBox, hWnd, addr szErrInfo2, addr szWndTitle, MB_OK
.elseif xReturn == ERROR_BUFFER_OVERFLOW
INVOKE MessageBox, hWnd, addr szErrInfo3, addr szWndTitle, MB_OK
.endif
mov eax, 1
ret
ReadAddrInfo endp
ShowAddrDesc proc Index:DWORD
LOCAL IDX :DWORD
LOCAL IP1[20] :BYTE
LOCAL IP2[20] :BYTE
LOCAL IP3[20] :BYTE
LOCAL IP4[20] :BYTE
LOCAL MAC[20] :BYTE
mov ebx, offset szAddrDesc
mov ecx, 512
@@:
mov BYTE PTR [ebx], 0
inc ebx
loop @B
cmp hAddrInfo, 0
je @F
mov IDX, 0
mov edi, hAddrInfo
assume edi:PTR IP_ADAPTER_INFO
.while edi
mov eax, IDX
.if eax == Index
INVOKE GetMACString, addr [edi].Address, [edi].AddressLength, addr MAC, 20
INVOKE GetIPAddrString, addr [edi].IpAddressList.IpAddress, addr IP1, 20
INVOKE GetIPAddrString, addr [edi].IpAddressList.IpMask, addr IP2, 20
INVOKE GetIPAddrString, addr [edi].GatewayList.IpAddress, addr IP3, 20
INVOKE GetIPAddrString, addr [edi].DhcpServer.IpAddress, addr IP4, 20
INVOKE wsprintf, addr szAddrDesc, addr szDescFormat, addr [edi].AdapterName, \
addr [edi].Description, addr IP1, addr IP2, addr IP3, addr IP4, addr MAC
.endif
inc IDX
mov edi, [edi].Next
.endw
assume edi:nothing
INVOKE lstrlen, addr szAddrDesc
cmp eax, 0
je @F
INVOKE SetWindowText, hEdit, addr szAddrDesc
@@:
mov eax, 0
ret
ShowAddrDesc endp
GetMACString proc buffer:DWORD, maxLength:DWORD, pszString:DWORD, maxOut:DWORD
LOCAL Hex :DWORD
mov ebx, pszString
mov ecx, maxOut
jmp @F
szHexFmt db "%02X-", 0
szHexFmt1 db "%02X", 0
@@:
mov BYTE PTR [ebx], 0
inc ebx
loop @B
mov ebx, buffer
.while maxLength
mov al, [ebx]
lea edx, szHexFmt1
cmp maxLength, 1
je @F
lea edx, szHexFmt
@@:
INVOKE wsprintf, addr Hex, edx, al
INVOKE lstrcat, pszString, addr Hex
dec maxLength
inc ebx
.endw
mov eax, 0
ret
GetMACString endp
GetIPAddrString proc buffer:DWORD, pszString:DWORD, maxOut:DWORD
mov ebx, pszString
mov ecx, maxOut
@@:
mov BYTE PTR [ebx], 0
inc ebx
loop @B
mov ebx, buffer
mov cx, 16
@@:
mov al, [ebx]
sub al, 30h
cmp al, 9
jle @F
inc ebx
loop @B
mov ebx, pszString
@@:
INVOKE lstrcpy, pszString, ebx
mov eax, 0
ret
GetIPAddrString endp
end Swdl_AppStart