如何用delphi獲得網卡的物理地址?
Delphi代碼獲取網卡物理地址
以下是我從MSDN中翻譯過來的三個Delphi單元,調用任意一個單元中聲明的API都可以獲取網卡的物理地址,但三個單元中的API函數的有效環境和功能各有不同,我把說明附于代碼的注釋中。
unit Lmwksta;
interface
uses
Windows,SysUtils,Dialogs;
type
_WKSTA_TRANSPORT_INFO_0=packed record
wkti0_quality_of_service:DWORD;
wkti0_number_of_vcs:DWORD;
wkti0_transport_name:PWideChar;{連接設備名稱,這個名稱是\DEVICE\NetBT_TcpIp……我也不知道如何理解,不過我們只想獲取MAC地址就不必管它了!}
wkti0_transport_address:PWideChar; //MAC地址
wkti0_wan_ish:BOOL; //是否是廣域網連接
end;
WKSTA_TRANSPORT_INFO_0=_WKSTA_TRANSPORT_INFO_0;
PWKSTA_TRANSPORT_INFO_0=^WKSTA_TRANSPORT_INFO_0;
const
NERR_Success=0;
MAX_PREFERRED_LENGTH=$FFFFFFFF;
//當本計算機有可用的網卡,且已經連接上網絡時,調用本函數才能成功,否則獲取不到任何信息
function NetWkstaTransportEnum(
ServerName:PWideChar;//主機名稱,傳遞nil時表示本機
Level:DWORD;//傳遞0
BufPtr:PPointer; {接受_WKSTA_TRANSPORT_INFO_0記錄數組的緩沖區,有此函數自行分配,但使用完后要用下面定義的NetApiBufferFree函數釋放內存}
PrefMaxLen:DWORD;//緩沖區最大長度,傳遞上面定義的MAX_PREFERRED_LENGTH常量即可
EntriesRead,TotalEntries,ResumeHandle:PDWORD):DWORD;stdcall;
{EntriesRead為返回的_WKSTA_TRANSPORT_INFO_0記錄數組的元素個數,至于TotalEntries和ResumeHandle,可以傳遞nil,需要更深入的了解,請參見MSDN}
function NetApiBufferFree(Buffer:Pointer):DWORD;stdcall;
implementation
function NetWkstaTransportEnum;external 'netapi32.dll' name 'NetWkstaTransportEnum';
function NetApiBufferFree;external 'netapi32.dll' name 'NetApiBufferFree';
end.
unit Rpcdce;
interface
uses
Windows,SysUtils;
type
TUUID=packed record
Data1:ULONG;
Data2:Word;
Data3:Word;
Data4:array [0..7] of Byte;//此數組的后6個元素就是網卡的物理地址信息
end;
TGUID=TUUID;
PUUID=^TUUID;
const
{以下為UuidCreateSequential函數的可能返回值}
RPC_S_UUID_LOCAL_ONLY:LongInt=1824; //函數生成的GUID只能保證在本計算機上是唯一的
RPC_S_UUID_NO_ADDRESS:LongInt=1739; //不能獲取以太網或令牌環網網卡設備
RPC_S_OK:LongInt=0; //函數調用成功,生成的GUID中包含了網卡的物理地址信息
function UuidCreateSequential(var uuid:TUUID):Cardinal;stdcall; //此函數只使用于單網卡的機器
function GetMACAddress:string;
implementation
function UuidCreateSequential;external 'Rpcrt4.dll' name 'UuidCreateSequential';
//引用此單元后,只需要調用GetMACAddress函數即可獲得網卡物理地址
function GetMACAddress:string;
var
uuid:TUUID;
I:Integer;
begin
Result:='';
if UuidCreateSequential(uuid)=RPC_S_OK then
for I:=2 to 7 do
begin
if I>2 then Result:=Result+'-';
Result:=Result+IntToHex(uuid.Data4[I],2);
end;
end;
end.
unit IPHlpAPI;
interface
uses
Windows,SysUtils,Classes;
const
MAX_ADAPTER_DESCRIPTION_LENGTH=128;
MAX_ADAPTER_NAME_LENGTH=256;
MAX_ADAPTER_ADDRESS_LENGTH=8;
DEFAULT_MINIMUM_ENTITIES=32;
MAX_HOSTNAME_LEN=128;
MAX_DOMAIN_NAME_LEN=128;
MAX_SCOPE_ID_LEN=256;
ERROR_NO_DATA:LongInt=232;
ERROR_NOT_SUPPORTED:LongInt=50;
ERROR_INVALID_PARAMETER:LongInt=87;
ERROR_BUFFER_OVERFLOW:LongInt=111;
type
time_t=Integer;
IP_ADDRESS_STRING=packed record
Addr:array [0..15] of Char;
end;
PIP_ADDRESS_STRING=^IP_ADDRESS_STRING;
IP_MASK_STRING=IP_ADDRESS_STRING;
PIP_MASK_STRING=^IP_MASK_STRING;
PIPAdapterInfo=^TIPAdapterInfo;
TIPAdapterInfo=packed record
Next:PIPAdapterInfo;//下一個節點的指針
ComboIndex:DWORD;
AdapterName:array [0..MAX_ADAPTER_NAME_LENGTH + 3] of Char; //適配器名稱
Description:array [0..MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of Char; //適配器描述信息
AddressLength:UINT;//Address域的最大長度,傳遞MAX_ADAPTER_ADDRESS_LENGTH常量即可
Address:array [0..MAX_ADAPTER_ADDRESS_LENGTH-1] of Byte;//適配器物理地址
Index:DWORD;
uType:UINT;
DhcpEnabled:UINT;
CurrentIpAddress:PIP_ADDRESS_STRING;
IpAddressList:IP_ADDRESS_STRING;
GatewayList:IP_ADDRESS_STRING;
DhcpServer:IP_ADDRESS_STRING;
HaveWins:BOOL;
PrimaryWinsServer:IP_ADDRESS_STRING;
SecondaryWinsServer:IP_ADDRESS_STRING;
LeaseObtained:time_t;
LeaseExpires:time_t;
end;
//此函數功能最強大,而且只要求網卡在系統中可用,并不要求一定連接上網絡
function GetAdaptersInfo(Buf:PIPAdapterInfo;var BufLen:ULONG):DWORD;stdcall;
implementation
function GetAdaptersInfo;external 'iphlpapi.dll' name 'GetAdaptersInfo';
end.
對于GetAdaptersInfo函數,Buf參數要求函數的調用者自行分配,如果分配的空間不足,函數返回ERROR_BUFFER_OVERFLOW,BufLen變參的值被設置為實際需要的緩沖區大小,因此可以再行分配,以滿足需
求,采用鏈表遍歷操作可以獲取所有已安裝網卡的物理地址。