Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 1998-1999, Microsoft Corporation
  3. Module Name:
  4. PIDSet.cpp
  5. Abstract:
  6. --*/
  7. #include <windows.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include "hardware.h"
  12. #include "tchar.h"
  13. #include "DigPid.h"
  14. #include "crc-32.h"
  15. BOOL PidRead(LPDIGITALPID pdpid, DWORD cbDpid)
  16. {
  17. BOOL fSuccess = FALSE;
  18. LONG lStatus;
  19. HKEY hkey;
  20. if (NULL != pdpid)
  21. {
  22. lStatus = RegOpenKeyEx(
  23. HKEY_LOCAL_MACHINE,
  24. TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"),
  25. 0,
  26. KEY_QUERY_VALUE,
  27. &hkey);
  28. if ( lStatus == ERROR_SUCCESS )
  29. {
  30. DWORD dwValueType;
  31. lStatus = RegQueryValueEx(
  32. hkey, TEXT("DigitalProductId"), NULL, &dwValueType, (LPBYTE)pdpid, &cbDpid);
  33. fSuccess = (ERROR_SUCCESS == lStatus);
  34. RegCloseKey(hkey);
  35. }
  36. }
  37. return fSuccess;
  38. }
  39. BOOL PidWrite(LPDIGITALPID pdpid, DWORD cbDpid)
  40. {
  41. BOOL fSuccess = FALSE;
  42. LONG lStatus;
  43. HKEY hkey;
  44. if (NULL != pdpid)
  45. {
  46. lStatus = RegOpenKeyEx(
  47. HKEY_LOCAL_MACHINE,
  48. TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"),
  49. 0,
  50. KEY_WRITE,
  51. &hkey);
  52. if ( lStatus == ERROR_SUCCESS )
  53. {
  54. lStatus = RegSetValueEx(
  55. hkey, // handle of key to set value for
  56. TEXT("DigitalProductId"), // name of the value to set
  57. 0, // reserved
  58. REG_BINARY, // flag for value type
  59. (LPBYTE)pdpid, // address of value data
  60. cbDpid); // size of value data
  61. fSuccess = (ERROR_SUCCESS == lStatus);
  62. RegCloseKey(hkey);
  63. }
  64. }
  65. return fSuccess;
  66. }
  67. int PASCAL WinMain(
  68. HINSTANCE, // hInstance, // handle to current instance
  69. HINSTANCE, // hPrevInstance, // handle to previous instance
  70. LPSTR, // lpCmdLine, // pointer to command line
  71. int )// nCmdShow // show state of window)
  72. {
  73. BOOL fOk = TRUE;
  74. BYTE abDigPid[1024] = {0};
  75. LPDIGITALPID pdpid = (LPDIGITALPID)abDigPid;
  76. fOk = PidRead(pdpid, sizeof(abDigPid));
  77. // check the version and ensure the HWID has not been set
  78. if (
  79. fOk &&
  80. 3 == pdpid->wVersionMajor &&
  81. '\0' == pdpid->aszHardwareIdStatic[0] &&
  82. 0 == pdpid->dwBiosChecksumStatic &&
  83. 0 == pdpid->dwVolSerStatic &&
  84. 0 == pdpid->dwTotalRamStatic &&
  85. 0 == pdpid->dwVideoBiosChecksumStatic)
  86. {
  87. BOOL fCrcGood = ( 0 == CRC_32((LPBYTE)pdpid, sizeof(*pdpid)) );
  88. CHardware hwid;
  89. strcpy(pdpid->aszHardwareIdStatic, hwid.GetID());
  90. pdpid->dwBiosChecksumStatic = hwid.GetBiosCrc32();
  91. pdpid->dwVolSerStatic = hwid.GetVolSer();
  92. pdpid->dwTotalRamStatic = hwid.GetTotalRamMegs();
  93. pdpid->dwVideoBiosChecksumStatic = hwid.GetVideoBiosCrc32();
  94. if (fCrcGood)
  95. {
  96. pdpid->dwCrc32 = CRC_32((LPBYTE)pdpid, sizeof(*pdpid)-sizeof(pdpid->dwCrc32));
  97. }
  98. else
  99. {
  100. pdpid->dwCrc32 = 0;
  101. }
  102. PidWrite(pdpid, pdpid->dwLength);
  103. }
  104. return 0;
  105. }