Как получить mac адрес компьютера в delphi?

Пользователь

от ransom_homenick , в категории: Другие , 2 года назад

Как получить mac адрес компьютера в delphi?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

2 ответа

P

Пользователь

от Programmer , 2 года назад

@ransom_homenick Салют!

Для получения МАК адресе можете воспользоваться следующими функциями

 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
uses NB30;
 
...
 
function GetAdapterInfo(Lana: Char): string;
var
  Adapter: TAdapterStatus;
  NCB: TNCB;
begin
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBRESET);
  NCB.ncb_lana_num := AnsiChar(Lana);
  if Netbios(@NCB) <> Char(NRC_GOODRET) then
  begin
    Result := 'Адрес не известен';
    Exit;
  end;
 
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBASTAT);
  NCB.ncb_lana_num := AnsiChar(Lana);
  NCB.ncb_callname := '*';
 
  FillChar(Adapter, SizeOf(Adapter), 0);
  NCB.ncb_buffer := @Adapter;
  NCB.ncb_length := SizeOf(Adapter);
  if Netbios(@NCB) <> Char(NRC_GOODRET) then
  begin
    Result := 'Адрес не известен';
    Exit;
  end;
  Result :=
  IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' +
  IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' +
  IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' +
  IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' +
  IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' +
  IntToHex(Byte(Adapter.adapter_address[5]), 2);
end;
 
function GetMACAddress: string;
var
  AdapterList: TLanaEnum;
  NCB: TNCB;
begin
  FillChar(NCB, SizeOf(NCB), 0);
  NCB.ncb_command := Char(NCBENUM);
  NCB.ncb_buffer := @AdapterList;
  NCB.ncb_length := SizeOf(AdapterList);
  Netbios(@NCB);
  if Byte(AdapterList.length) > 0 then
    Result := GetAdapterInfo(Char(AdapterList.lana[0]))
  else
    Result := 'Адрес не известен';
end;

 

 Пример использования вышеизложенный функций

1
2
3
4
5
6
7
8
procedure TForm1.Button1Click(Sender: TObject);
var
  mac_adr: string;
begin
   mac_adr:= GetMacAddress;
   ShowMessage(mac_adr);
end;
  


Пользователь

от elda , 9 месяцев назад

@ransom_homenick 

Для получения MAC-адреса компьютера в Delphi можно использовать функцию GetAdaptersInfo из библиотеки iphlpapi.dll.


Вот пример кода:


uses Windows, IpsApi;


function GetMACAddress: string; type PIP_ADAPTER_INFO = ^IP_ADAPTER_INFO; IP_ADAPTER_INFO = packed record Next : PIP_ADAPTER_INFO; ComboIndex : DWORD; AdapterName : array [0..MAX_ADAPTER_NAME_LENGTH + 3] of AnsiChar; Description : array [0..MAX_ADAPTER_DESCRIPTION_LENGTH + 3] of AnsiChar; AddressLength : UINT; Address : array [0..MAX_ADAPTER_ADDRESS_LENGTH - 1] of Byte; Index : DWORD; Type_ : UINT; DhcpEnabled : UINT; CurrentIpAddress: PIP_ADDR_STRING; IpAddressList : IP_ADDR_STRING; GatewayList : IP_ADDR_STRING; DhcpServer : IP_ADDR_STRING; HaveWins : BOOL; PrimaryWinsServer: IP_ADDR_STRING; SecondaryWinsServer: IP_ADDR_STRING; LeaseObtained : LongInt; LeaseExpires : LongInt; end; var AdapterInfo : PIP_ADAPTER_INFO; Adapter : PIP_ADAPTER_INFO; Len : DWORD; begin Result := ''; Len := SizeOf(IP_ADAPTER_INFO); GetMem(AdapterInfo, Len); if GetAdaptersInfo(AdapterInfo, Len) = ERROR_BUFFER_OVERFLOW then begin ReallocMem(AdapterInfo, Len); if GetAdaptersInfo(AdapterInfo, Len) <> NO_ERROR then begin FreeMem(AdapterInfo); Exit; end; end; Adapter := AdapterInfo; while Adapter <> nil do begin if Adapter^.AddressLength > 0 then begin Result := Format('%.2x-%.2x-%.2x-%.2x-%.2x-%.2x', [Adapter^.Address[0], Adapter^.Address[1], Adapter^.Address[2], Adapter^.Address[3], Adapter^.Address[4], Adapter^.Address[5]]); Break; end; Adapter := Adapter^.Next; end; FreeMem(AdapterInfo); end;


Она возвращает строку, содержащую MAC-адрес компьютера.