Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 10.43 KB | None | 0 0
  1. unit MSProdKey;
  2.  
  3. {
  4. **************************************************************************************
  5. * Unit MSProdKey v2.2                                                                *
  6. *                                                                                    *
  7. *  Description: Decode and View the Product Key, Product ID and Product Name used to *
  8. *               install: Windows 2000, XP, Server 2003, Office XP, 2003.             *
  9. *               *Updated* Now works for users with Non-Administrative Rights.        *
  10. *               Code cleanup and changes, Commented.                                 *
  11. *                                                                                    *
  12. *  Usage: Add MSProdKey to your Application's uses clause.                           *
  13. *                                                                                    *
  14. *  Example 1:                                                                        *
  15. *                                                                                    *
  16. * procedure TForm1.Button1Click(Sender: TObject);                                    *
  17. * begin                                                                              *
  18. *   if not IS_WinVerMin2K then // If the Windows version isn't at least Windows 2000 *
  19. *   Edit1.Text := 'Windows 2000 or Higher Required!' // Display this message         *
  20. *   else // If the Windows version is at least Windows 2000                          *
  21. *   Edit1.Text := View_Win_Key; // View the Windows Product Key                      *
  22. *   Label1.Caption := PN; // View the Windows Product Name                           *
  23. *   Label2.Caption := PID; // View the Windows Product ID                            *
  24. * end;                                                                               *
  25. *                                                                                    *
  26. *  Example 2:                                                                        *
  27. * procedure TForm1.Button2Click(Sender: TObject);                                    *
  28. * begin                                                                              *
  29. *   if not IS_OXP_Installed then // If Office XP isn't installed                     *
  30. *   Edit1.Text := 'Office XP Required!' // Display this message                      *
  31. *   else // If Office XP is installed                                                *
  32. *   Edit1.Text := View_OXP_Key; // View the Office XP Product Key                    *
  33. *   Label1.Caption := DN; // View the Office XP Product Name                         *
  34. *   Label2.Caption := PID; // View the Office XP Product ID                          *
  35. * end;                                                                               *
  36. *                                                                                    *
  37. *  Example 3:                                                                        *
  38. * procedure TForm1.Button3Click(Sender: TObject);                                    *
  39. * begin                                                                              *
  40. *   if not IS_O2K3_Installed then // If Office 2003 isn't installed                  *
  41. *   Edit1.Text := 'Office 2003 Required!' // Display this message                    *
  42. *   else // If Office 2003 is installed                                              *
  43. *   Edit1.Text := View_O2K3_Key; // View the Office 2003 Product Key                 *
  44. *   Label1.Caption := DN; // View the Office 2003 Product Name                       *
  45. *   Label2.Caption := PID; // View the Office 2003 Product ID                        *
  46. * end;                                                                               *
  47. *                                                                                    *
  48. **************************************************************************************
  49. }
  50.  
  51. interface
  52.  
  53. uses Registry, Windows, SysUtils, Classes;
  54.  
  55. function IS_WinVerMin2K: Boolean; // Check OS for Win 2000 or higher
  56. function View_Win_Key: string; // View the Windows Product Key
  57. function IS_OXP_Installed: Boolean;  // Check if Office XP is installed
  58. function View_OXP_Key: string;  // View the Office XP Product Key
  59. function IS_O2K3_Installed: Boolean; // Check if Office 2003 is installed
  60. function View_O2K3_Key: string; // View the Office 2003 Product Key
  61. function DecodeProductKey(const HexSrc: array of Byte): string;
  62.   // Decodes the Product Key(s) from the Registry
  63.  
  64. var
  65.   Reg: TRegistry;
  66.   binarySize: INTEGER;
  67.   HexBuf: array of BYTE;
  68.   temp: TStringList;
  69.   KeyName, KeyName2, SubKeyName, PN, PID, DN: string;
  70.  
  71. implementation
  72.  
  73. function IS_WinVerMin2K: Boolean;
  74. var
  75.   OS: TOSVersionInfo;
  76. begin
  77.   ZeroMemory(@OS, SizeOf(OS));
  78.   OS.dwOSVersionInfoSize := SizeOf(OS);
  79.   GetVersionEx(OS);
  80.   Result := (OS.dwMajorVersion >= 5) and
  81.     (OS.dwPlatformId = VER_PLATFORM_WIN32_NT);
  82.   PN     := ''; // Holds the Windows Product Name
  83.   PID    := ''; // Holds the Windows Product ID
  84. end;
  85.  
  86.  
  87. function View_Win_Key: string;
  88. begin
  89.   Reg := TRegistry.Create;
  90.   try
  91.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  92.     if Reg.OpenKeyReadOnly('\SOFTWARE\Microsoft\Windows NT\CurrentVersion') then
  93.     begin
  94.       if Reg.GetDataType('DigitalProductId') = rdBinary then
  95.       begin
  96.         PN         := (Reg.ReadString('ProductName'));
  97.         PID        := (Reg.ReadString('ProductID'));
  98.         binarySize := Reg.GetDataSize('DigitalProductId');
  99.         SetLength(HexBuf, binarySize);
  100.         if binarySize > 0 then
  101.         begin
  102.           Reg.ReadBinaryData('DigitalProductId', HexBuf[0], binarySize);
  103.         end;
  104.       end;
  105.     end;
  106.   finally
  107.     FreeAndNil(Reg);
  108.   end;
  109.  
  110.   Result := '';
  111.   Result := DecodeProductKey(HexBuf);
  112. end;
  113.  
  114. function IS_OXP_Installed: Boolean;
  115. var
  116.   Reg: TRegistry;
  117. begin
  118.   Reg := TRegistry.Create;
  119.   try
  120.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  121.     Result      := Reg.KeyExists('SOFTWARE\MICROSOFT\Office\10.0\Registration');
  122.   finally
  123.     Reg.CloseKey;
  124.     Reg.Free;
  125.   end;
  126.   DN  := ''; // Holds the Office XP Product Display Name
  127.   PID := ''; // Holds the Office XP Product ID
  128. end;
  129.  
  130. function View_OXP_Key: string;
  131. begin
  132.   try
  133.     Reg         := TRegistry.Create;
  134.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  135.     KeyName     := 'SOFTWARE\MICROSOFT\Office\10.0\Registration\';
  136.     Reg.OpenKeyReadOnly(KeyName);
  137.     temp := TStringList.Create;
  138.     Reg.GetKeyNames(temp); // Enumerate and hold the Office XP Product(s) Key Name(s)
  139.     Reg.CloseKey;
  140.     SubKeyName  := temp.Strings[0]; // Hold the first Office XP Product Key Name
  141.     Reg         := TRegistry.Create;
  142.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  143.     KeyName2    := 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\';
  144.     Reg.OpenKeyReadOnly(KeyName2 + SubKeyName);
  145.     DN := (Reg.ReadString('DisplayName'));
  146.     Reg.CloseKey;
  147.   except
  148.     on E: EStringListError do
  149.       Exit
  150.   end;
  151.   try
  152.     if Reg.OpenKeyReadOnly(KeyName + SubKeyName) then
  153.     begin
  154.       if Reg.GetDataType('DigitalProductId') = rdBinary then
  155.       begin
  156.         PID        := (Reg.ReadString('ProductID'));
  157.         binarySize := Reg.GetDataSize('DigitalProductId');
  158.         SetLength(HexBuf, binarySize);
  159.         if binarySize > 0 then
  160.         begin
  161.           Reg.ReadBinaryData('DigitalProductId', HexBuf[0], binarySize);
  162.         end;
  163.       end;
  164.     end;
  165.   finally
  166.     FreeAndNil(Reg);
  167.   end;
  168.  
  169.   Result := '';
  170.   Result := DecodeProductKey(HexBuf);
  171. end;
  172.  
  173. function IS_O2K3_Installed: Boolean;
  174. var
  175.   Reg: TRegistry;
  176. begin
  177.   Reg := TRegistry.Create;
  178.   try
  179.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  180.     Result      := Reg.KeyExists('SOFTWARE\MICROSOFT\Office\11.0\Registration');
  181.   finally
  182.     Reg.CloseKey;
  183.     Reg.Free;
  184.   end;
  185.   DN  := ''; // Holds the Office 2003 Product Display Name
  186.   PID := ''; // Holds the Office 2003 Product ID
  187. end;
  188.  
  189. function View_O2K3_Key: string;
  190. begin
  191.   try
  192.     Reg         := TRegistry.Create;
  193.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  194.     KeyName     := 'SOFTWARE\MICROSOFT\Office\11.0\Registration\';
  195.     Reg.OpenKeyReadOnly(KeyName);
  196.     temp := TStringList.Create;
  197.     Reg.GetKeyNames(temp);
  198.     // Enumerate and hold the Office 2003 Product(s) Key Name(s)
  199.     Reg.CloseKey;
  200.     SubKeyName  := temp.Strings[0]; // Hold the first Office 2003 Product Key Name
  201.     Reg         := TRegistry.Create;
  202.     Reg.RootKey := HKEY_LOCAL_MACHINE;
  203.     KeyName2    := 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\';
  204.     Reg.OpenKeyReadOnly(KeyName2 + SubKeyName);
  205.     DN := (Reg.ReadString('DisplayName'));
  206.     Reg.CloseKey;
  207.   except
  208.     on E: EStringListError do
  209.       Exit
  210.   end;
  211.   try
  212.     if Reg.OpenKeyReadOnly(KeyName + SubKeyName) then
  213.     begin
  214.       if Reg.GetDataType('DigitalProductId') = rdBinary then
  215.       begin
  216.         PID        := (Reg.ReadString('ProductID'));
  217.         binarySize := Reg.GetDataSize('DigitalProductId');
  218.         SetLength(HexBuf, binarySize);
  219.         if binarySize > 0 then
  220.         begin
  221.           Reg.ReadBinaryData('DigitalProductId', HexBuf[0], binarySize);
  222.         end;
  223.       end;
  224.     end;
  225.   finally
  226.     FreeAndNil(Reg);
  227.   end;
  228.  
  229.   Result := '';
  230.   Result := DecodeProductKey(HexBuf);
  231. end;
  232.  
  233. function DecodeProductKey(const HexSrc: array of Byte): string;
  234. const
  235.   StartOffset: Integer = $34; { //Offset 34 = Array[52] }
  236.   EndOffset: Integer   = $34 + 15; { //Offset 34 + 15(Bytes) = Array[64] }
  237.   Digits: array[0..23] of CHAR = ('B', 'C', 'D', 'F', 'G', 'H', 'J',
  238.     'K', 'M', 'P', 'Q', 'R', 'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9');
  239.   dLen: Integer = 29; { //Length of Decoded Product Key }
  240.   sLen: Integer = 15;
  241.   { //Length of Encoded Product Key in Bytes (An total of 30 in chars) }
  242. var
  243.   HexDigitalPID: array of CARDINAL;
  244.   Des: array of CHAR;
  245.   I, N: INTEGER;
  246.   HN, Value: CARDINAL;
  247. begin
  248.   SetLength(HexDigitalPID, dLen);
  249.   for I := StartOffset to EndOffset do
  250.   begin
  251.     HexDigitalPID[I - StartOffSet] := HexSrc[I];
  252.   end;
  253.  
  254.   SetLength(Des, dLen + 1);
  255.  
  256.   for I := dLen - 1 downto 0 do
  257.   begin
  258.     if (((I + 1) mod 6) = 0) then
  259.     begin
  260.       Des[I] := '-';
  261.     end
  262.     else
  263.     begin
  264.       HN := 0;
  265.       for N := sLen - 1 downto 0 do
  266.       begin
  267.         Value := (HN shl 8) or HexDigitalPID[N];
  268.         HexDigitalPID[N] := Value div 24;
  269.         HN    := Value mod 24;
  270.       end;
  271.       Des[I] := Digits[HN];
  272.     end;
  273.   end;
  274.   Des[dLen] := Chr(0);
  275.  
  276.   for I := 0 to Length(Des) do
  277.   begin
  278.     Result := Result + Des[I];
  279.   end;
  280. end;
  281.  
  282. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement