Como obter o Mac Address do adaptador de rede?
A função abaixo retorna o Mac Address do adaptador de rede, porém
ela só funciona para aplicações copiladas para win32.
É necessário estar declarado Windows na seção uses,
em versões unicode declare Winapi.Windows.
Abaixo segue o código fonte da função:
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
| function GetMacAddress: 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; |
function GetMacAddress: 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;
Exemplo de uso:
1
2
3
4
| procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetMacAddress);
end; |
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetMacAddress);
end;
Exemplo de uso 2
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
56
57
58
59
60
61
62
63
64
| unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetMacAddress: 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;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(GetMacAddress);
end;
end. |
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetMacAddress: 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;
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(GetMacAddress);
end;
end.
-
InfusTec
-
8.971 views
- 6 comentários
- 2 de abril de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo
por que quando coloco esse código no oncreate do form ele da erro no end; sempre. não consigo resolver, se alguém puder me ajudar, desde já agradeço
Estranho!
Testei agora no Delphi 10 seattle e foi de boa!
Verifique se colocas-te o código corretamente no form.
Exemplo de uso da função:
procedure TForm1.FormCreate(Sender: TObject);
begin
ShowMessage(GetMacAddress);
end;
Não consegui implementar esse código, copiei todo o código desde a function GetMacAddress: string; da Linha [ 1 ] até a[ 33 ].
coloquei toda ela em form create e o show address em um botão, testei com Winapi.Windows. e com windows na uses uma de cada vez, só que no final do código fica marcado como se um end; estivesse errado, sublinhado em vermelho, e o projeto não roda por conta dele. já apaguei, já acrescentei, e não resolve, [ DELPHI XE 8 ]
Coloque mais um exemplo no post, desta vez tem todo o código da unit, tenta ver se o seu
código esta parecido.
1 Agora sim ^_^, tive que fazer algumas modificações para rodar no meu projeto,
{$R *.dfm}
function GetMacAddress: string;
coloquei o código no local correto dessa vez. kk >_< vivendo e aprendendo … sacomé..
2 uses tive que apagar algumas linhas mas ficou assim pra rodar.
uses
Winapi.Windows, Winapi.Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Vcl.Buttons;
apaguei 1 ou 2 end;s do final e não mostrou o mac com oncreate mas coloquei a função em um botão e ela apareceu normalmente.
obrigado pela ajuda, se eu fosse mulher te daria um bj mano kkkkk
quando eu crescer quero ser igual vc kkk
vlw mesmo adoro esse site tudo que eu procuro eu acho aqui. *__* e tem respostas rápidas também vlw mesmo.
Fiz uns testes com esse comando, entretanto não é muito seguro. Se você for no prompt e dar um getmac. Executar este comando vai pegar o mesmo mac. Ai você desativa a rede ativa novamente e faz o mesmo processo vai ver que outro mac será listado.