6

Delphi を使用してネットワーク カードの MacAddress を取得するにはどうすればよいですか?

4

4 に答える 4

17

プロジェクトJEDIAPIヘッダーライブラリからMicrosoftIPヘルパーライブラリのJEDI変換を取得します。ファイルはIPHlpAPI.zipです。ファイルを解凍します。IpTypes.pasとIpHlpApi.pasが必要です。次に、次のようなものを使用できます。

procedure TForm1.Button1Click(Sender: TObject);
var
  NumInterfaces: Cardinal;
  AdapterInfo: array of TIpAdapterInfo;
  OutBufLen: ULONG;
  i: integer;
begin
  GetNumberOfInterfaces(NumInterfaces);
  SetLength(AdapterInfo, NumInterfaces);
  OutBufLen := NumInterfaces * SizeOf(TIpAdapterInfo);
  GetAdaptersInfo(@AdapterInfo[0], OutBufLen);

  Memo1.Lines.Clear;
  for i := 0 to NumInterfaces - 1 do begin
    Memo1.Lines.Add(Format('%.2x:%.2x:%.2x:%.2x:%.2x:%.2x',
      [AdapterInfo[i].Address[0], AdapterInfo[i].Address[1],
       AdapterInfo[i].Address[2], AdapterInfo[i].Address[3],
       AdapterInfo[i].Address[4], AdapterInfo[i].Address[5]]));
  end;
end;

(すべてのエラー処理は省略されています。もちろん追加する必要があります。)

于 2009-02-23T10:51:57.000 に答える
3

GetAdaptersAddresses 関数は、Windows XP で 2001 年以降、アダプター情報を取得するための推奨される方法です。

アダプターの情報は、パラメーターによってIP_ADAPTER_ADDRESSES 構造体AdapterAddressesに返されます。

この関数は、 IPv4およびIPv6アドレスGetAdaptersAddressesの情報を取得できます。

関数を呼び出す推奨される方法は、パラメーターGetAdaptersAddressesが指す 15KB の作業バッファーを事前に割り当てることです。通常のコンピューターでは、これにより、関数が を返すAdapterAddresses可能性が劇的に減少します。これには、関数を複数回呼び出す必要があります。GetAdaptersAddressesERROR_BUFFER_OVERFLOWGetAdaptersAddresses


procedure TForm1.Button1Click(Sender: TObject);
const
  AF_UNSPEC = 0;
  GAA_FLAG_INCLUDE_ALL_INTERFACES = $100;
  WORKING_BUFFER_SIZE = 15000;
  MAX_TRIES = 3;
var
  pAddresses,
  pCurrAddresses: PIpAdapterAddresses;
  dwRetVal,
  outBufLen: Cardinal;
  i: Integer;
  macAddress: string;
begin
  Memo1.Lines.Clear;

  outBufLen := WORKING_BUFFER_SIZE;
  pAddresses := nil;
  i := 0;
  repeat
    if Assigned(pAddresses) then
      FreeMem(pAddresses);

    GetMem(pAddresses, outBufLen);
    if not Assigned(pAddresses) then
      raise Exception.Create('Memory allocation failed for IP_ADAPTER_ADDRESSES struct');

    dwRetVal := GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_ALL_INTERFACES, nil, pAddresses, @outBufLen);
    Inc(i);
  until (dwRetVal <> ERROR_BUFFER_OVERFLOW) or (i = MAX_TRIES);

  try
    if NO_ERROR <> dwRetVal then begin
      if ERROR_NO_DATA = dwRetVal then begin
        MessageDlg('No addresses were found for the requested parameters', mtInformation, [mbOK], 0);
        Exit;
      end
      else
        raise Exception.Create(SysErrorMessage(dwRetVal));
    end;

    pCurrAddresses := pAddresses;
    while Assigned(pCurrAddresses) do begin
      if pCurrAddresses^.PhysicalAddressLength > 0 then begin
        Memo1.Lines.Add(pCurrAddresses^.FriendlyName);
        macAddress := '';
        for i := 0 to pCurrAddresses^.PhysicalAddressLength - 1 do begin
          if i > 0 then
            macAddress := macAddress + ':';
          macAddress := macAddress + Format('%.2X', [pCurrAddresses^.PhysicalAddress[i]]);
        end;
        Memo1.Lines.Add(macAddress);
        Memo1.Lines.Add('');
      end;
      pCurrAddresses := pCurrAddresses^.Next;
    end;

  finally
    if Assigned(pAddresses) then
      FreeMem(pAddresses);
  end;
end;
于 2015-11-17T09:51:24.003 に答える
-5

delphiについてほとんど何も知らずに、%system32%\ ipconfig.exe / allを実行して、出力を解析するのはどうでしょうか。

于 2009-02-23T10:33:36.450 に答える