获取MAC地址


/ Published in: C++
Save to your folder(s)

获取MAC地址


Copy this code and paste it in your HTML
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13. : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17.  
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21.  
  22. #define MAX_ADAPTER_DESCRIPTION_LENGTH 128 // arb.
  23. #define MAX_ADAPTER_NAME_LENGTH 256 // arb.
  24. #define MAX_ADAPTER_ADDRESS_LENGTH 8 // arb.
  25.  
  26. //
  27. // IP_ADDRESS_STRING - store an IP address as a dotted decimal string
  28. //
  29.  
  30. typedef struct
  31. {
  32. char String[4 * 4];
  33. } IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;
  34.  
  35. //
  36. // IP_ADDR_STRING - store an IP address with its corresponding subnet mask,
  37. // both as dotted decimal strings
  38. //
  39.  
  40. typedef struct _IP_ADDR_STRING
  41. {
  42. struct _IP_ADDR_STRING* Next;
  43. IP_ADDRESS_STRING IpAddress;
  44. IP_MASK_STRING IpMask;
  45. DWORD Context;
  46. } IP_ADDR_STRING, *PIP_ADDR_STRING;
  47.  
  48. //
  49. // ADAPTER_INFO - per-adapter information. All IP addresses are stored as strings
  50. //
  51.  
  52. typedef struct _IP_ADAPTER_INFO
  53. {
  54. struct _IP_ADAPTER_INFO* Next;
  55. DWORD ComboIndex;
  56. char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
  57. char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
  58. UINT AddressLength;
  59. BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
  60. DWORD Index;
  61. UINT Type;
  62. UINT DhcpEnabled;
  63. PIP_ADDR_STRING CurrentIpAddress;
  64. IP_ADDR_STRING IpAddressList;
  65. IP_ADDR_STRING GatewayList;
  66. IP_ADDR_STRING DhcpServer;
  67. BOOL HaveWins;
  68. IP_ADDR_STRING PrimaryWinsServer;
  69. IP_ADDR_STRING SecondaryWinsServer;
  70. time_t LeaseObtained;
  71. time_t LeaseExpires;
  72. } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
  73.  
  74. typedef DWORD (WINAPI* GetAdaptersInfoFunc)(
  75. PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data
  76. PULONG pOutBufLen // size of data returned
  77. );
  78. ///////////////////////////////////////////////////////////////////////////////
  79.  
  80. void MacAddressToString(const LPBYTE Address, LPSTR lpsz, int nAddressLength = 6)
  81. {
  82. int n;
  83. LPSTR p = lpsz;
  84. for (n = 0; n < nAddressLength; n++)
  85. {
  86. p += sprintf(p, n ? "-%02x" : "%02x", Address[n]);
  87. }
  88. }
  89. //---------------------------------------------------------------------------
  90.  
  91. AnsiString __fastcall TForm1::GetMac(void)
  92. {
  93. //TODO: Add your source code here
  94. HMODULE hLib;
  95. GetAdaptersInfoFunc GetAdaptersInfo = NULL;
  96.  
  97. PIP_ADAPTER_INFO pai = NULL;
  98. DWORD dwSize = 0;
  99. CHAR szMac[64];
  100.  
  101. hLib = LoadLibrary("Iphlpapi.dll");
  102. if (!hLib)
  103. {
  104. //puts("Failed to load Iphlpapi.dll");
  105. exit(1);
  106. }
  107.  
  108. GetAdaptersInfo = (GetAdaptersInfoFunc)GetProcAddress(hLib, "GetAdaptersInfo");
  109. if (GetAdaptersInfo == NULL)
  110. {
  111. //puts("Failed to load GetAdaptersInfo in Iphlpapi.dll");
  112. }
  113.  
  114. // Get size of buffer needed:
  115. GetAdaptersInfo(NULL, &dwSize);
  116.  
  117. pai = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, dwSize);
  118. GetAdaptersInfo(pai, &dwSize);
  119.  
  120. PIP_ADAPTER_INFO p = pai;
  121.  
  122. MacAddressToString(p->Address, szMac, p->AddressLength);
  123.  
  124. GlobalFree(pai);
  125. FreeLibrary(hLib);
  126. return ((AnsiString)szMac).UpperCase();
  127. }
  128. //---------------------------------------------------------------------------
  129.  
  130. void __fastcall TForm1::btn1Click(TObject *Sender)
  131. {
  132. lbl1->Caption = GetMac();
  133. }
  134. //---------------------------------------------------------------------------

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.