【问题标题】:How to get MAC address in windows7? [duplicate]windows7如何获取MAC地址? [复制]
【发布时间】:2010-07-17 03:55:04
【问题描述】:

可能的重复:
Getting Machine’s MAC Address — Good Solution?
How do I get the MAC address of a network card using Delphi?

我使用 MAC 地址作为硬件 ID 进行保护(当然我已经加密了这些数据) 我正在使用下面的代码来获取用户计算机上的 MAC 地址

function MacAddress: string;
var
Lib: Cardinal;
Func: function(GUID: PGUID): Longint; stdcall;
GUID1, GUID2: TGUID;
begin
Result := '';
Lib := LoadLibrary('rpcrt4.dll');
if Lib <> 0 then
begin
   @Func := GetProcAddress(Lib, 'UuidCreateSequential');
   if Assigned(Func) then
   begin
     if (Func(@GUID1) = 0) and
        (Func(@GUID2) = 0) and
        (GUID1.D4[2] = GUID2.D4[2]) and
        (GUID1.D4[3] = GUID2.D4[3]) and
        (GUID1.D4[4] = GUID2.D4[4]) and
        (GUID1.D4[5] = GUID2.D4[5]) and
        (GUID1.D4[6] = GUID2.D4[6]) and
        (GUID1.D4[7] = GUID2.D4[7]) then
     begin
       Result :=
         IntToHex(GUID1.D4[2], 2) + '-' +
         IntToHex(GUID1.D4[3], 2) + '-' +
         IntToHex(GUID1.D4[4], 2) + '-' +
         IntToHex(GUID1.D4[5], 2) + '-' +
         IntToHex(GUID1.D4[6], 2) + '-' +
         IntToHex(GUID1.D4[7], 2);
     end;
   end;
end;
end;

以上代码在 Windows XP 上完美运行 但它在windows7中给出不同的值,每次计算机重新命名后值都会改变:( 是否有机会获得恒定的 MAC 地址(除非用户更改了他的 MAC 地址) 还是有什么好的代码可以在所有操作系统上检索常量数据?

提前致谢

【问题讨论】:

  • 是什么让您认为this code 检索了您计算机的 MAC 地址?
  • 确认!该函数不返回 MAC 地址。它返回一个有时恰好包含 MAC 地址的值。

标签: windows delphi network-programming


【解决方案1】:

@steve0,要检索网络适配器的mac地址,您可以使用WMIWin32_NetworkAdapterConfiguration类并检查MACAddress属性。

检查此代码:

program WMI_MAC;

{$APPTYPE CONSOLE}


uses
  SysUtils
  ,ActiveX
  ,ComObj
  ,Variants;

 function VarToStrNil(Value:Variant):string;  //Dummy function to onvert an variant value to string
 begin
   if VarIsNull(Value) then
    Result:=''
   else
    Result:=VarToStr(Value);
 end;


Procedure GetMacAddress;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
  wmiHost, root, wmiClass: string;

  function GetWMIObject(const objectName: String): IDispatch;
  var
    chEaten: Integer;
    BindCtx: IBindCtx;//for access to a bind context
    Moniker: IMoniker;//Enables you to use a moniker object
  begin
    OleCheck(CreateBindCtx(0, bindCtx));
    OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));//Converts a string into a moniker that identifies the object named by the string
    OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));//Binds to the specified object
  end;

begin
  wmiHost       := '.';
  root          := 'root\CIMV2';
  wmiClass      := 'Win32_NetworkAdapterConfiguration';
  objWMIService := GetWMIObject(Format('winmgmts:\\%s\%s',[wmiHost,root]));
  colItems      := objWMIService.ExecQuery(Format('SELECT * FROM %s',[wmiClass]),'WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  //if VarToStrNil(colItem.MACAddress)<>'' then //uncomment if you only want list the interfaces with mac adress
  //if colItem.IPEnabled then  // uncomment if you only want list the active interfaces
  begin
    WriteLn('Card Description '+VarToStrNil(colItem.Caption));
    WriteLn('MACAddress       '+VarToStrNil(colItem.MACAddress));
  end;
end;

begin
 try
    CoInitialize(nil);
    try         
      GetMacAddress;
      Readln;
    finally
    CoUninitialize;
    end;
 except
    on E:Exception do
    Begin
        Writeln(E.Classname, ': ', E.Message);
        Readln;
    End;
  end;
end.

【讨论】:

    【解决方案2】:

    这里有一些代码适用于您网络上的任何计算机 - 可以尝试获取自己的代码,使用“127.0.0.1”作为 IP:

    function GetRemoteMacAddress(const IP: AnsiString): TSockData;
    // implements http://msdn.microsoft.com/en-us/library/aa366358(VS.85).aspx
    type
      TSendARP = function(DestIp: DWORD; srcIP: DWORD; pMacAddr: pointer; PhyAddrLen: Pointer): DWORD; stdcall;
    const
      HexChars: array[0..15] of AnsiChar = '0123456789ABCDEF';
    var dwRemoteIP: DWORD;
        PhyAddrLen: Longword;
        pMacAddr : array [0..7] of byte;
        I: integer;
        P: PAnsiChar;
        SendARPLibHandle: THandle;
        SendARP: TSendARP;
    begin
      result := '';
      SendARPLibHandle := LoadLibrary('iphlpapi.dll');
      if SendARPLibHandle<>0 then
      try
        SendARP := GetProcAddress(SendARPLibHandle,'SendARP');
        if @SendARP=nil then
          exit; // we are not under 2K or later
        dwremoteIP := inet_addr(pointer(IP));
        if dwremoteIP<>0 then begin
          PhyAddrLen := 8;
          if SendARP(dwremoteIP, 0, @pMacAddr, @PhyAddrLen)=NO_ERROR then begin
            if PhyAddrLen=6 then begin
              SetLength(result,12);
              P := pointer(result);
              for i := 0 to 5 do begin
                P[0] := HexChars[pMacAddr[i] shr 4];
                P[1] := HexChars[pMacAddr[i] and $F];
                inc(P,2);
              end;
            end;
          end;
        end;
      finally
        FreeLibrary(SendARPLibHandle);
      end;
    end;
    

    此代码是从我们的免费软件和开源框架单元 SynCrtSock.pas 中提取的。见http://synopse.info/fossil

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 2023-04-02
      • 2010-11-30
      • 2011-10-09
      • 2014-07-24
      相关资源
      最近更新 更多