thardware.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "thardware.h"
  2. #include <Windows.h>
  3. #include <cstdio>
  4. #include <Iphlpapi.h>
  5. #include <NtDDNdis.h>
  6. #include <string>
  7. #pragma comment(lib, "Iphlpapi.lib")
  8. namespace TC {
  9. THardWare::THardWare() {}
  10. static bool GetPhyMacAddress(const char *strServiceName, char *RealMAC)
  11. {
  12. bool bRet = false;
  13. // char pstrBuf[512] = {0};
  14. // sprintf(pstrBuf, "//./%s", strServiceName);
  15. // HANDLE hDev = CreateFileA(pstrBuf,
  16. // GENERIC_READ | GENERIC_WRITE,
  17. // FILE_SHARE_READ | FILE_SHARE_WRITE,
  18. // NULL,
  19. // OPEN_EXISTING,
  20. // 0,
  21. // 0);
  22. // if (hDev != INVALID_HANDLE_VALUE) {
  23. // int inBuf;
  24. // BYTE outBuf[256] = {0};
  25. // DWORD BytesReturned;
  26. // inBuf = OID_802_3_PERMANENT_ADDRESS;
  27. // if (DeviceIoControl(hDev,
  28. // IOCTL_NDIS_QUERY_GLOBAL_STATS,
  29. // (LPVOID) &inBuf,
  30. // 4,
  31. // outBuf,
  32. // 256,
  33. // &BytesReturned,
  34. // NULL)) {
  35. // char buff[256] = {0};
  36. // sprintf(buff,
  37. // "%d%d%d%d%d%d",
  38. // outBuf[0],
  39. // outBuf[1],
  40. // outBuf[2],
  41. // outBuf[3],
  42. // outBuf[4],
  43. // outBuf[5]);
  44. // bRet = true;
  45. // memcpy(RealMAC, buff, strlen(buff));
  46. // }
  47. // CloseHandle(hDev);
  48. // }
  49. return bRet;
  50. }
  51. std::string THardWare::machineCode()
  52. {
  53. char Macstr[256] = {0};
  54. ULONG uSize = 0;
  55. DWORD dwResult = GetAdaptersInfo(NULL, &uSize);
  56. if (dwResult == ERROR_BUFFER_OVERFLOW) {
  57. PIP_ADAPTER_INFO piai1 = (PIP_ADAPTER_INFO) new BYTE[uSize];
  58. PIP_ADAPTER_INFO piai = piai1;
  59. dwResult = GetAdaptersInfo(piai, &uSize);
  60. if (ERROR_SUCCESS == dwResult) {
  61. const char *card1 = "QF9700";
  62. const char *card2 = "RD9700";
  63. while (piai) //获取多个网卡MAC地址
  64. {
  65. //QF9700 RD9700 特殊USB网卡处理
  66. if (strncmp(piai->Description, card1, strlen(card1)) == 0
  67. || strncmp(piai->Description, card2, strlen(card2)) == 0) {
  68. piai = piai->Next;
  69. continue;
  70. }
  71. char mac[32] = {0};
  72. GetPhyMacAddress(piai->AdapterName, mac);
  73. piai = piai->Next;
  74. ::sprintf(Macstr, "%s%s", Macstr, mac); //把多个网卡MAC地址拼接
  75. }
  76. }
  77. delete[] piai1;
  78. }
  79. return std::string(Macstr);
  80. }
  81. } // namespace TC