怎样取得本机网卡的Mac地址呀?老板在催了,大虾救我呀!! ( 积分: 50 )

  • 主题发起人 主题发起人 jog81
  • 开始时间 开始时间
J

jog81

Unregistered / Unconfirmed
GUEST, unregistred user!
在网上找到一些代码,都是在网络连通状态下可以取。我现在需要的是网卡实际的MAC地址,不管它连不连通。
请各位大侠帮忙,谢谢!
 
在网上找到一些代码,都是在网络连通状态下可以取。我现在需要的是网卡实际的MAC地址,不管它连不连通。
请各位大侠帮忙,谢谢!
 
Function TForm_main.NBGetAdapterAddress(a:Integer) : String;
Var
NCB : TNCB;
// Netbios control block file:
//NetBios???
ADAPTER : TADAPTERSTATUS;
// Netbios adapter status
LANAENUM : TLANAENUM; // Netbios lana
intIdx : Integer;
// Temporary work value
cRC : Char;
// Netbios return code
//NetBios???
strTemp : String;
// Temporary string
Begin
Result := '';
Try
// Zero control blocl
ZeroMemory(@NCB, SizeOf(NCB));

// Issue enum command
NCB.ncb_command := Chr(NCBENUM);
cRC := NetBios(@NCB);

// Reissue enum command
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then exit;

// Reset adapter
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[a];
cRC := NetBios(@NCB);
If Ord(cRC) <>0 Then exit;

// Get adapter address
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[a];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB);

// Convert it to string
strTemp := '';
For intIdx := 0 To 5 Do strTemp := strTemp +
InttoHex(Integer
(ADAPTER.adapter_address[intIdx]),2);
Result := strTemp;
Finally
End;
End;

procedure TForm_main.Button_AboutMeClick(Sender: TObject);
var
Form_aboutme: TForm_aboutme;
begin
Form_aboutme:=TForm_aboutme.Create(Self);
Form_aboutme.ShowModal;
Form_aboutme.Free;
end;
 
没必要这么麻烦的。
你直接到DOS下,然后,输入ipconfig/all就可以了查看到你的网卡MAC了
 
楼上的齐华兄弟忽悠我吧? 我是想自己写程序去读取这些信息。
二楼的兄弟,你的代码只能在网卡在使用时可以读取,但网卡一旦断开就读不出来了。
谁有好的办法呀?![:(]
 
function GetMACAddress(LanaNum: Byte; MACAddress: PMACAddress): Byte;
var
AdapterStatus: PAdapterStatus;
StatNCB: PNCB;
begin
New(StatNCB);
ZeroMemory(StatNCB, SizeOf(TNCB));
StatNCB.ncb_length := SizeOf(TAdapterStatus) + 255 * SizeOf(TNameBuffer);
GetMem(AdapterStatus, StatNCB.ncb_length);
try
with StatNCB^ do
begin
ZeroMemory(MACAddress, SizeOf(TMACAddress));
ncb_buffer := PChar(AdapterStatus);
ncb_callname := '* ' + #0;
ncb_lana_num := Char(LanaNum);
ncb_command := Char(NCBASTAT);
NetBios(StatNCB);
Result := Byte(ncb_cmd_cplt);
if Result = NRC_GOODRET then
MoveMemory(MACAddress, AdapterStatus, SizeOf(TMACAddress));
end;
finally
FreeMem(AdapterStatus);
Dispose(StatNCB);
end;
end;
jog81 你试试!
 
应该行的!
 
楼上的大侠,网络连通时可以取,断开就不行了。[:(]
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,nb30;

type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
Var
NCB : TNCB;
ADAPTER : TADAPTERSTATUS;
LANAENUM : TLANAENUM;
intIdx : Integer;
re : Char;
buf : String;
begin
Try
{ ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBENUM);
re := NetBios(@NCB);

// Reissue enum command
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
re := NetBios(@NCB);
If Ord(re)<>0 Then
exit;
}
{ Copyright (C) Bowman }
{ MatthewBowman@21cn.com }
// Reset adapter
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[0];//important
re := NetBios(@NCB);
If Ord(re)<>0 Then
exit;

// Get adapter address
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[0];////bowman
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
re := NetBios(@NCB);
If Ord(re)<>0 Then
exit;

buf := '';
For intIdx := 0 To 5 Do
buf := buf + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2)+'-';
edit1.Text := copy(buf,0,length(buf)-1);
Finally
End;

end;

end.
 
结果一样,网络连通时可以取,断开就不行了。
谁能救我哟?![:(]
 
function GetMacAddresses(const Machine: string; const Addresses: TStrings): Integer;
var
NCB: TNCB;
Enum: TLanaEnum;
I, L, NameLen: Integer;
Adapter: ASTAT;
MachineName: string;
begin
Result := -1;
Addresses.Clear;
MachineName := UpperCase(Machine);
if MachineName = '' then
MachineName := '*';
NameLen := Length(MachineName);
L := NCBNAMSZ - NameLen;
if L > 0 then
begin
SetLength(MachineName, NCBNAMSZ);
FillChar(MachineName[NameLen + 1], L, ' ');
end;
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBENUM;
NCB.ncb_buffer := Pointer(@Enum);
NCB.ncb_length := SizeOf(Enum);
if NetBios(@NCB) = NRC_GOODRET then
begin
Result := Enum.Length;
for I := 0 to Ord(Enum.Length) - 1 do
begin
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBRESET;
NCB.ncb_lana_num := Enum.lana;
if NetBios(@NCB) = NRC_GOODRET then
begin
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBASTAT;
NCB.ncb_lana_num := Enum.lana;
Move(MachineName[1], NCB.ncb_callname, SizeOf(NCB.ncb_callname));
NCB.ncb_buffer := PChar(@Adapter);
NCB.ncb_length := SizeOf(Adapter);
if NetBios(@NCB) = NRC_GOODRET then
Addresses.Add(AdapterToString(Adapter.adapt));
end;
end;
end;
end;
************************
//获取网卡地址,使用netbios 3.0调用 uses nb30.pas shipped with delphi 4.0
function _NetCardAddress : String ;
var
NCB : TNCB ; // Netbios control block
ADAPTER : TADAPTERSTATUS ; // Netbios adapter status
LANAENUM : TLANAENUM ; // Netbios lana
intIdx : Integer ; // Temporary work value
CRC : Char ; // Netbios return code
strTemp : String ; // Temporary string
begin
Result := '' ;
try
ZeroMemory(@NCB,SizeOf(NCB)) ; // Zero control blocl
NCB.ncb_command := Chr(NCBENUM) ; // Issue enum command
cRC := NetBios(@NCB) ;
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit ;
end;



NCB.ncb_buffer := @LANAENUM ; // Reissue enum command
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB) ;
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit ;
end;
ZeroMemory(@NCB,SizeOf(NCB)) ; // Reset adapter
NCB.ncb_command := Chr(NCBRESET) ;
NCB.ncb_lana_num := LANAENUM.lana[0] ;
cRC := NetBios(@NCB) ;
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit ;
end;
ZeroMemory(@NCB,SizeOf(NCB)) ; // Get adapter address
NCB.ncb_command := Chr(NCBASTAT) ;
NCB.ncb_lana_num := LANAENUM.lana[0] ;
StrPCopy(NCB.ncb_callname,'*') ;
NCB.ncb_buffer := @ADAPTER ;
NCB.ncb_length := SizeOf(ADAPTER) ;
cRC := NetBios(@NCB) ;
if Ord(cRC)>0 then begin
result:=_NBReturnMSG(crc);
exit ;
end; // Convert Netcard address it to string
strTemp := '' ;
for intIdx := 0 to 5 do strTemp := strTemp + _Char2Hex(ADAPTER.adapter_address[intIdx]) ;
Result := strTemp ;
finally
end ;
end ;
 
function GetMacAddress(const AServerName : string) : string;
type//得到网卡地址
TNetTransportEnum = function(pszServer : PWideChar;
Level : DWORD;
var pbBuffer : pointer;
PrefMaxLen : LongInt;
var EntriesRead : DWORD;
var TotalEntries : DWORD;
var ResumeHandle : DWORD) : DWORD; stdcall;

TNetApiBufferFree = function(Buffer : pointer) : DWORD; stdcall;

PTransportInfo = ^TTransportInfo;
TTransportInfo = record
quality_of_service : DWORD;
number_of_vcs : DWORD;
transport_name : PWChar;
transport_address : PWChar;
wan_ish : boolean;
end;

var
E,ResumeHandle,
EntriesRead,
TotalEntries : DWORD;
FLibHandle : THandle;
sMachineName,
sMacAddr,
Retvar : string;
pBuffer : pointer;
pInfo : PTransportInfo;
FNetTransportEnum : TNetTransportEnum;
FNetApiBufferFree : TNetApiBufferFree;
pszServer : array[0..128] of WideChar;
i,ii,iIdx : integer;
begin
sMachineName := trim(AServerName);
Retvar := '00-00-00-00-00-00';

// Add leading // if missing
if (sMachineName <> '') and (length(sMachineName) >= 2) then
begin
if copy(sMachineName,1,2) <> '//' then
sMachineName := '//' + sMachineName
end;

// Setup and load from DLL
pBuffer := nil;
ResumeHandle := 0;
FLibHandle := LoadLibrary('NETAPI32.DLL');

// Execute the external function
if FLibHandle <> 0 then
begin //1
@FNetTransportEnum := GetProcAddress(FLibHandle,'NetWkstaTransportEnum');
@FNetApiBufferFree := GetProcAddress(FLibHandle,'NetApiBufferFree');
E := FNetTransportEnum(StringToWideChar(sMachineName,pszServer,129),0,
pBuffer,-1,EntriesRead,TotalEntries,Resumehandle);
if E = 0 then
begin //2
pInfo := pBuffer;
// Enumerate all protocols - look for TCPIP
for i := 1 to EntriesRead do
begin //3
if pos('TCPIP',UpperCase(pInfo^.transport_name)) <> 0 then
begin //4
// Got It - now format result 'xx-xx-xx-xx-xx-xx'
iIdx := 1;
sMacAddr := pInfo^.transport_address;
for ii := 1 to 12 do
begin //5
Retvar[iIdx] := sMacAddr[ii];
inc(iIdx);
if iIdx in [3,6,9,12,15] then inc(iIdx);
end; //5
end; //4
inc(pInfo);
end;//3
if pBuffer <> nil then FNetApiBufferFree(pBuffer);
end;//2
try
FreeLibrary(FLibHandle);
except
// 错误处理
end;
end;//1
result:=Retvar;
end;
 
用delphi 执行dos命令 并截取命令同时显示在memo中[设置memo不可见] 通过pos函数等函数截取mac地址
 
unit GetMacAddr;

interface
uses
Windows,SysUtils;

function GetMacAddress():string;

implementation

Const
MAX_HOSTNAME_LEN = 128; { from IPTYPES.H }
MAX_DOMAIN_NAME_LEN = 128;
MAX_SCOPE_ID_LEN = 256;
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;

Type
TIPAddressString = Array[0..4*4-1] of Char;

PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
Context : Integer;
End;

PFixedInfo = ^TFixedInfo;
TFixedInfo = Record { FIXED_INFO }
HostName : Array[0..MAX_HOSTNAME_LEN+3] of Char;
DomainName : Array[0..MAX_DOMAIN_NAME_LEN+3] of Char;
CurrentDNSServer : PIPAddrString;
DNSServerList : TIPAddrString;
NodeType : Integer;
ScopeId : Array[0..MAX_SCOPE_ID_LEN+3] of Char;
EnableRouting : Integer;
EnableProxy : Integer;
EnableDNS : Integer;
End;

PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
Next : PIPAdapterInfo;
ComboIndex : Integer;
AdapterName : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of Char;
Description : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of Char;
AddressLength : Integer;
Address : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index : Integer;
_Type : Integer;
DHCPEnabled : Integer;
CurrentIPAddress : PIPAddrString;
IPAddressList : TIPAddrString;
GatewayList : TIPAddrString;
DHCPServer : TIPAddrString;
HaveWINS : Bool;
PrimaryWINSServer : TIPAddrString;
SecondaryWINSServer : TIPAddrString;
LeaseObtained : Integer;
LeaseExpires : Integer;
End;

//Function GetNetworkParams(FI : PFixedInfo; Var BufLen : Integer) : Integer;
// StdCall; External 'iphlpapi.dll' Name 'GetNetworkParams';

Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';



function GetMacAddress():string;
Var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
// I : Integer;

Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
Result := Result+IntToHex(ByteArr^,2)+'-';
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
SetLength(Result,Length(Result)-1); { remove last dash }
End;

Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;

Function TimeTToDateTimeStr(TimeT : Integer) : String;
Const UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
Var
DT : TDateTime;
TZ : TTimeZoneInformation;
Res : DWord;

Begin
If (TimeT = 0) Then Result := ''
Else Begin
{ Unix TIME_T is secs since 1/1/1970 }
DT := UnixDateDelta+(TimeT / (24*60*60)); { in UTC }
{ calculate bias }
Res := GetTimeZoneInformation(TZ);
If (Res = TIME_ZONE_ID_INVALID) Then RaiseLastWin32Error;
If (Res = TIME_ZONE_ID_STANDARD) Then Begin
DT := DT-((TZ.Bias+TZ.StandardBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.StandardName);
End
Else Begin { daylight saving time }
DT := DT-((TZ.Bias+TZ.DaylightBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.DaylightName);
End;
End;
End;

begin
Size := 5120;
GetMem(AI,Size);
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;

Work := AI;
//I := 1;
Result := '';
Repeat
//Add('');
//Add('Adapter '+IntToStr(I));
//Add(' ComboIndex: '+IntToStr(Work^.ComboIndex));
//Add(' Adapter name: '+Work^.AdapterName);
//Add(' Description: '+Work^.Description);
//Add(' Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
Result := Result + MACToStr(@Work^.Address,Work^.AddressLength)+'*';

//Add(' Index: '+IntToStr(Work^.Index));
//Add(' Type: '+IntToStr(Work^._Type));
//Add(' DHCP: '+IntToStr(Work^.DHCPEnabled));
//Add(' Current IP: '+GetAddrString(Work^.CurrentIPAddress));
//Add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
//Add(' Gateways: '+GetAddrString(@Work^.GatewayList));
//Add(' DHCP servers: '+GetAddrString(@Work^.DHCPServer));
//Add(' Has WINS: '+IntToStr(Integer(Work^.HaveWINS)));
//Add(' Primary WINS: '+GetAddrString(@Work^.PrimaryWINSServer));
//Add(' Secondary WINS: '+GetAddrString(@Work^.SecondaryWINSServer));
//Add(' Lease obtained: '+TimeTToDateTimeStr(Work^.LeaseObtained));
//Add(' Lease expires: '+TimeTToDateTimeStr(Work^.LeaseExpires));
//Inc(I);
Work := Work^.Next;
Until (Work = nil);


FreeMem(AI);
end;

end.
 
这个单元调用了操作系统的iphlpapi.dll ,所以是确实可用的 。

其它我找到的获得网卡MAC的例子都不那么好使,包括我买的书(好像是DELPHI网络编程什么的名字)中的两个例子,都无法正常获得。

上面这个Unit可以获得网卡的详细信息,但是我注释掉了部分功能,如果你要得到详细的信息,可以恢复它们。
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, Buttons, ShellAPI;

Const
MAX_HOSTNAME_LEN = 128; { from IPTYPES.H }
MAX_DOMAIN_NAME_LEN = 128;
MAX_SCOPE_ID_LEN = 256;
MAX_ADAPTER_NAME_LENGTH = 256;
MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
MAX_ADAPTER_ADDRESS_LENGTH = 8;

Type
TIPAddressString = Array[0..4*4-1] of Char;

PIPAddrString = ^TIPAddrString;
TIPAddrString = Record
Next : PIPAddrString;
IPAddress : TIPAddressString;
IPMask : TIPAddressString;
Context : Integer;
End;

PFixedInfo = ^TFixedInfo;
TFixedInfo = Record { FIXED_INFO }
HostName : Array[0..MAX_HOSTNAME_LEN+3] of Char;
DomainName : Array[0..MAX_DOMAIN_NAME_LEN+3] of Char;
CurrentDNSServer : PIPAddrString;
DNSServerList : TIPAddrString;
NodeType : Integer;
ScopeId : Array[0..MAX_SCOPE_ID_LEN+3] of Char;
EnableRouting : Integer;
EnableProxy : Integer;
EnableDNS : Integer;
End;

PIPAdapterInfo = ^TIPAdapterInfo;
TIPAdapterInfo = Record { IP_ADAPTER_INFO }
Next : PIPAdapterInfo;
ComboIndex : Integer;
AdapterName : Array[0..MAX_ADAPTER_NAME_LENGTH+3] of Char;
Description : Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3] of Char;
AddressLength : Integer;
Address : Array[1..MAX_ADAPTER_ADDRESS_LENGTH] of Byte;
Index : Integer;
_Type : Integer;
DHCPEnabled : Integer;
CurrentIPAddress : PIPAddrString;
IPAddressList : TIPAddrString;
GatewayList : TIPAddrString;
DHCPServer : TIPAddrString;
HaveWINS : Bool;
PrimaryWINSServer : TIPAddrString;
SecondaryWINSServer : TIPAddrString;
LeaseObtained : Integer;
LeaseExpires : Integer;
End;

type
TForm1 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Panel2: TPanel;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Info: TMemo;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
edtIP: TEdit;
edtMask: TEdit;
edtGate: TEdit;
BitBtn1: TBitBtn;
Label4: TLabel;
Shape1: TShape;
Label5: TLabel;
edtDNS1: TEdit;
edtDNS2: TEdit;
Label6: TLabel;
edtLinkName: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Procedure GetAdapterInformation;
public
{ Public declarations }
end;

var
Form1: TForm1;

Function sendarp(ipaddr:ulong;
temp:dword;
ulmacaddr:pointer;
ulmacaddrleng:pointer) : DWord; StdCall;
implementation

{$R *.dfm}
Function sendarp; External 'Iphlpapi.dll' Name 'SendARP';
Function GetAdaptersInfo(AI : PIPAdapterInfo; Var BufLen : Integer) : Integer;
StdCall; External 'iphlpapi.dll' Name 'GetAdaptersInfo';


procedure TForm1.Button1Click(Sender: TObject);
var
myip:ulong;
mymac:array[0..5] of byte;
mymaclength:ulong;
r:integer;
begin
PageControl1.ActivePageIndex := 0;
{
myip:=inet_addr(PChar('10.0.0.1'));
mymaclength:=length(mymac);
r:=sendarp(myip, 0, @mymac, @mymaclength);
label1.caption:='errorcode:'+inttostr(r);
label2.caption:=format('%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x',[mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]]);
}
GetAdapterInformation;
end;

procedure TForm1.GetAdapterInformation;
Var
AI,Work : PIPAdapterInfo;
Size : Integer;
Res : Integer;
I : Integer;

Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
Result := Result+IntToHex(ByteArr^,2)+'-';
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
SetLength(Result,Length(Result)-1); { remove last dash }
End;

Function GetAddrString(Addr : PIPAddrString) : String;
Begin
Result := '';
While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End;
End;

Function TimeTToDateTimeStr(TimeT : Integer) : String;
Const UnixDateDelta = 25569; { days between 12/31/1899 and 1/1/1970 }
Var
DT : TDateTime;
TZ : TTimeZoneInformation;
Res : DWord;

Begin
If (TimeT = 0) Then Result := ''
Else Begin
{ Unix TIME_T is secs since 1/1/1970 }
DT := UnixDateDelta+(TimeT / (24*60*60)); { in UTC }
{ calculate bias }
Res := GetTimeZoneInformation(TZ);
If (Res = TIME_ZONE_ID_INVALID) Then RaiseLastWin32Error;
If (Res = TIME_ZONE_ID_STANDARD) Then Begin
DT := DT-((TZ.Bias+TZ.StandardBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.StandardName);
End
Else Begin { daylight saving time }
DT := DT-((TZ.Bias+TZ.DaylightBias) / (24*60));
Result := DateTimeToStr(DT)+' '+WideCharToString(TZ.DaylightName);
End;
End;
End;

begin
info.Lines.Clear;
Size := 5120;
GetMem(AI,Size);
Res := GetAdaptersInfo(AI,Size);
If (Res <> ERROR_SUCCESS) Then Begin
SetLastError(Res);
RaiseLastWin32Error;
End;
With Info,Lines do Begin
Work := AI;
I := 1;
Repeat
Add('');
Add('Adapter ' + IntToStr(I));
Add(' ComboIndex: '+IntToStr(Work^.ComboIndex));
Add(' Adapter name: '+Work^.AdapterName);
Add(' Description: '+Work^.Description);
Add(' Adapter address: '+MACToStr(@Work^.Address,Work^.AddressLength));
Add(' Index: '+IntToStr(Work^.Index));
Add(' Type: '+IntToStr(Work^._Type));
Add(' DHCP: '+IntToStr(Work^.DHCPEnabled));
Add(' Current IP: '+GetAddrString(Work^.CurrentIPAddress));
Add(' IP addresses: '+GetAddrString(@Work^.IPAddressList));
Add(' Gateways: '+GetAddrString(@Work^.GatewayList));
Add(' DHCP servers: '+GetAddrString(@Work^.DHCPServer));
Add(' Has WINS: '+IntToStr(Integer(Work^.HaveWINS)));
Add(' Primary WINS: '+GetAddrString(@Work^.PrimaryWINSServer));
Add(' Secondary WINS: '+GetAddrString(@Work^.SecondaryWINSServer));
Add(' Lease obtained: '+TimeTToDateTimeStr(Work^.LeaseObtained));
Add(' Lease expires: '+TimeTToDateTimeStr(Work^.LeaseExpires));
Inc(I);
Work := Work^.Next;
Until (Work = nil);
End;
FreeMem(AI);
end;

end.
 
用此方法可以简单准确的得到网卡的物理地址
procedure TForm1.Button2Click(Sender: TObject);
var
f1:TextFile ;
filename,tmpstr,allstr:string;
begin
filename :='c:/123.txt';
AssignFile(f1,filename );
Reset(f1);
allstr :='';
while not Eof(f1)do
begin
Readln(f1,tmpstr);
allstr :=Trim(tmpstr);
if AnsiContainsText(allstr,'Physical Address')then
begin
Edit1.Text :=RightStr(allstr,17);
exit;
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
const
temp='c:/123.txt';
begin
if fileexists(temp) then
deletefile(temp);
winexec(pchar('command.com /C ipconfig/all >'+temp),sw_hide);
end;
 
感谢lighttop,已经成功取得了![8D]
 
后退
顶部