Click here to Skip to main content
Licence 
First Posted 21 Aug 2006
Views 54,720
Bookmarked 18 times

Extract Window Product Key - With an API !

By | 21 Aug 2006 | Article
Get a window's product key!

Introduction

Hi. This article shows a very usuful function!! This function gets a window's product key using the Windows API that can used with any window and can be used with any tool and language, VB, C++, VS6, VS 7, 8, and so on.

The code

BSTR GetProductKey()
/*
 Window Product Key Extract

*/
{
 CString strResult;        //Return a Window Product Key
  
 HKEY hRegistryKey;        //Registry Handler 
 BYTE   *DigitalProductID; //Digital Product Key Value 
 DWORD DataLength;         //Digital Product Key Length 

 BYTE ProductKeyExtract [15]; //Extract Key 

char sCDKey  [256];   //Temp, adding a Window Product Key
 
 long ByteCounter;    //Counter
 long ByteConvert;    //Convert

 int  nCur;      //XOR calculate 

 
 char *KeyChars[] = {
       "B","C","D","F","G","H","J","K","M",
       "P","Q","R","T","V","W","X","Y",
       "2","3","4","6","7","8","9",NULL
      }; 

 // HKLM\\SOFTWARE\\MICROSOFT\\Windows NT\\CurrentVersion 열기  
 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NT_CURRENT, 
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 
    &hRegistryKey) == ERROR_SUCCESS)
 {
  
  DataLength = 164; 

  //Allocate Memory
  DigitalProductID = (BYTE *)malloc(DataLength);    

  //Memory Initializationd
  memset(DigitalProductID, 0, DataLength); 

 //Digital Product Key Open

  if(RegQueryValueEx(hRegistryKey, "DigitalProductId", 
   NULL, NULL, DigitalProductID, &DataLength) == ERROR_SUCCESS)
  {   
   //reading a value start position 52, by 66
   for(ByteCounter=52; ByteCounter<=66; ByteCounter++)
   {
    ProductKeyExtract[ByteCounter - 52] = 
             DigitalProductID[ByteCounter];
   }

   //Last Indexer
   ProductKeyExtract[sizeof(ProductKeyExtract)] = NULL;
  }
 } 

 //Start Converting job, Next Step 

 memset(sCDKey, 0, sizeof(sCDKey)); 

 for(ByteCounter=24; ByteCounter>=0; ByteCounter--)
 {
  nCur = 0; 

  for(ByteConvert=14; ByteConvert>=0; ByteConvert--)
  {
   

   nCur = (nCur * 256) ^ ProductKeyExtract[ByteConvert];  //XOR&#44228;&#49328; 
   ProductKeyExtract[ByteConvert] = nCur / 24;
   nCur = nCur % 24;
  }   
  
  strrev(sCDKey);
  strcat(sCDKey, KeyChars[nCur]);
  strrev(sCDKey);

  //Insert "-" 

  if(!(ByteCounter % 5) && (ByteCounter))
  {
   strrev(sCDKey);
   
   strcat(sCDKey, "-"); 
   
   strrev(sCDKey);
  }
 }

 //Insert Product Key into Return value 

 strResult.Format("%s", sCDKey); 

 //Close Registry
 RegCloseKey(hRegistryKey); 

 //Release Memory
 if(DigitalProductID) free(DigitalProductID); 

 return strResult.AllocSysString();
}

If you have any questions, mail them to me!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

handsomeme

Web Developer

United States United States

Member

Yeah, I'm programmer of the mediasolution in SEOUL, Korea. KKK
 
Bye

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWork version is here [modified] Pinmemberdima_kdl7:03 30 Jan '09  
GeneralRe: Work version is here Pinmembertttony0717:36 25 Aug '09  
AnswerRe: Work version is here Pinmemberdanielsage11:07 6 Feb '11  
GeneralRe: Work version is here Pinmembertttony0714:46 6 Feb '11  
GeneralC# & VB.NET Code Pinmembercrouchie19985:46 4 Apr '07  
GeneralNice code PinmemberDouglas R. Keesler20:15 6 Jan '07  
GeneralSmall bug Pinmembermcanti3:42 6 Jan '07  
GeneralRe: Small bug Pinmembersstraw12:21 17 Oct '07  

Yea, I found that bug right away as well (though didn't visit this article until just this morning).
 
NT_CURRENT isn't a standard define - it should be REGSTR_PATH_NT_CURRENTVERSION (from regstr.h). Perhaps the NT_CURRENT definition is part of some other devtool the author is using.
 
The method employed to insert hyphens is absolutely painful, repeatedly reversing the string to insert a hyphen (or even, just to add the next decoded character), and reversing it back. Since the characters are emitted last first, just skip the whole strrev business until the very end:
 
strcat(sCDKey, KeyChars[nCur]);
if(!(ByteCounter % 5) && (ByteCounter))
{
//Insert "-" between every five character sequence
strcat(sCDKey, "-");
}
}
//reverse the ENTIRE sequence ONCE.
_strrev(sCDKey);
 
OR add the following above the ByteCounter loop:
 
// 5 sequences of 5 chars + 4 intermediate hyphens
int MasterIndex= 5*5 + 4;
 
and replace the strcat/strrev stuff:
 
// Simply construct the code in reverse order. no strcat/strrev.
sCDKey[--MasterIndex] = *KeyChars[nCur];
if( ByteCounter && !(ByteCounter % 5) )
{
sCDKey[--MasterIndex] = '-';
}
 
This would insert the characters of the decoded key in the appropriate locations right from the start, no _strrev or strcat necessary.
 
Look Ma, no compiler warnings...
 

I found it curious that with all the variables crammed on the stack, the storage for the DigitalProductID registry value was actually _allocated_. There isn't a problem with allocating, but it could just as easily have been a stack variable -- sCDKey for instance doesn't need 256 bytes - it's a 30 character string (including null termination). DigitalProductID of course should be freed within the RegKeyOpenEx() conditional, right after the necessary bytes have been copied out of it. Likewise, the RegCloseKey() should be within the same conditional - and for that matter, all the decoding should be dependant upon a successful key retrieval.
 

I fixed issues I had with the code, and modified the function to take a passed BYTE array which may have been retrieved manually (say, from the registry on a non-booting HD, which puts the key elsewhere). In all, useful information to have.
 
I'm a bit more interested in what the conversion is between the CD key and the numeric product code.
 
-S. Straw
GeneralWindows Vista PinmemberGabriel Topala17:14 16 Sep '06  
GeneralRe: Windows Vista Pinmemberhandsomeme16:59 17 Sep '06  
GeneralRe: Windows Vista Pinmembertzukuei0:49 18 Oct '06  
GeneralRe: Windows Vista PinmemberGabriel Topala1:35 18 Oct '06  
GeneralRe: Windows Vista Pinmembertzukuei17:10 18 Oct '06  
GeneralRe: Windows Vista PinmemberGabriel Topala18:00 18 Oct '06  
GeneralRe: Windows Vista [modified] Pinmembertzukuei16:33 19 Oct '06  
GeneralRe: Windows Vista Pinmemberawjapp7:34 21 Apr '07  
GeneralSolo Pinmemberhvw19:13 22 Aug '06  
GeneralWow Good C# I like C# and used this! Pinmemberhandsomeme14:38 22 Aug '06  
Generalin c# Pinmembersolo6:17 22 Aug '06  
GeneralRe: in c#, not work! PinmemberBug Me Not1:56 7 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 21 Aug 2006
Article Copyright 2006 by handsomeme
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid