如何在Windows7中获取MAC地址? [英] How to get MAC address in windows7?

查看:108
本文介绍了如何在Windows7中获取MAC地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
获取计算机的MAC地址—好的解决方案?
如何获取MAC使用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地址) 还是有什么好的代码可以在所有OS上检索常量数据?

提前感谢

解决方案

@ steve0,要获取网络适配器的mac地址,可以使用WMIGetting Machine’s MAC Address — Good Solution?
How do I get the MAC address of a network card using Delphi?

I am using MAC address as hardware id for protection(ofcourse I have encrypted this data) I am using below code to get MAC address on user computer

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;

above code works perfectly on windows XP but its giving different values in windows7 ,the value changing every time after computer resratred :( is there any chance of getting MAC address thats constant (unless user changed his MAC address) or is there any good code which retrvies constant data on all OS ?

thanks in advance

解决方案

@steve0, to retrieve the mac address of an Network Adapter you can use the WMI and the Win32_NetworkAdapterConfiguration Class and check the MACAddress property.

Check this code:

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.

这篇关于如何在Windows7中获取MAC地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆