El siguiente ejemplo extrae los adaptadores de red en un equipo local y nos informa de su MAC. La base es la API GetAdaptersInfo que puede extraer bastantes datos mas de interés.
El ejemplo en C/C++:
cpp
//--------------------------------------------------------------------------- #include <windows.h> #include <winsock2.h> #include <Iphlpapi.h> #include <stdio.h> #include <conio.h> #pragma comment(lib, "iphlpapi.lib") #pragma hdrstop //--------------------------------------------------------------------------- void GetMACaddress(void) { PIP_ADAPTER_INFO AI, pAI; DWORD AILen; GetAdaptersInfo(0, &AILen); pAI = AI = (PIP_ADAPTER_INFO) new BYTE[AILen]; if(!GetAdaptersInfo(pAI, &AILen)){ do{ printf("%s: %.2X-%.2X-%.2X-%.2X-%.2X-%.2X", pAI->Description, pAI->Address[0], pAI->Address[1], pAI->Address[2], pAI->Address[3], pAI->Address[4], pAI->Address[5]); pAI = pAI->Next; } while(pAI); } delete [] AI; getch(); } #pragma argsused int main(int argc, char* argv[]) { GetMACaddress(); return 0; }
Y ahora el ejemplo en delphi:
delphi
program GetMACAddresses; {$APPTYPE CONSOLE} uses Windows, Winsock; type PIP_ADDR_STRING = ^IP_ADDR_STRING; IP_ADDR_STRING = record Next: PIP_ADDR_STRING; IpAddress: array [0..15] of Char; IpMask: array [0..15] of Char; Context: DWORD; end; PIP_ADAPTER_INFO = ^IP_ADAPTER_INFO; IP_ADAPTER_INFO = record Next: PIP_ADAPTER_INFO; ComboIndex: DWORD; AdapterName: array [0..259] of Char; Description: array [0..131] of Char; AddressLength: UINT; Address: array [0..7] 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; function wsprintf(lpOut, lpFmt: PChar): Integer; cdecl; varargs; external 'User32.dll' name 'wsprintfA'; function GetAdaptersInfo(pAdapterInfo: PIP_ADAPTER_INFO; var pOutBufLen: ULONG): DWORD; stdcall; external 'iphlpapi.dll'; procedure Get_MAC_Addresses; var AI, pAI: PIP_ADAPTER_INFO; AILen: DWORD; Buff: array [0..19] of char; begin GetAdaptersInfo(nil, AILen); GetMem(AI, AILen); pAI:= AI; if GetAdaptersInfo(pAI, AILen) = 0 then repeat wsprintf(Buff, '%.2X-%.2X-%.2X-%.2X-%.2X-%.2X', pAI.Address[0], pAI.Address[1], pAI.Address[2], pAI.Address[3], pAI.Address[4], pAI.Address[5], pAI.Address[6]); Writeln(pAI.Description + ': ' + Buff); pAI:= pAI.Next; until pAI = nil; FreeMem(AI); ReadLn; end; begin Get_MAC_Addresses; end.
Ambos son programas de consola que muestran los adaptadores de red instalados en el equipo junto con su MAC.
Saludos.