Click here to Skip to main content
15,883,901 members
Articles / Programming Languages / C++
Article

Extract Window Product Key - With an API !

Rate me:
Please Sign up or sign in to vote.
3.07/5 (8 votes)
21 Aug 2006 98.8K   23   23
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계산 
   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


Written By
Architect
United States United States
Yeah, I'm programmer in SEOUL, Korea.

Comments and Discussions

 
Questionwin10 Pin
yht790590731-Jul-18 21:37
yht790590731-Jul-18 21:37 
QuestionCouple serious bugs in this Pin
pickaname26-Nov-13 11:20
pickaname26-Nov-13 11:20 
AnswerRe: Couple serious bugs in this Pin
handsomeme8-Apr-14 2:01
handsomeme8-Apr-14 2:01 
GeneralWork version is here [modified] Pin
dima_kdl30-Jan-09 7:03
dima_kdl30-Jan-09 7:03 
GeneralRe: Work version is here Pin
tttony0725-Aug-09 17:36
tttony0725-Aug-09 17:36 
AnswerRe: Work version is here Pin
danielsage6-Feb-11 11:07
danielsage6-Feb-11 11:07 
On 64 bit operating systems, this code is required to either be compiled into a 64 bit executable or detect that host Operating System is 64 and open the registry key with the KEY_WOW64_64KEY flag.

Hope this helps.

Edit: Wow, this is older than I thought it was.
GeneralRe: Work version is here Pin
tttony076-Feb-11 14:46
tttony076-Feb-11 14:46 
GeneralC# & VB.NET Code Pin
crouchie044-Apr-07 5:46
crouchie044-Apr-07 5:46 
GeneralNice code Pin
Douglas R. Keesler6-Jan-07 20:15
Douglas R. Keesler6-Jan-07 20:15 
GeneralSmall bug Pin
mcanti6-Jan-07 3:42
mcanti6-Jan-07 3:42 
GeneralRe: Small bug Pin
sstraw17-Oct-07 12:21
sstraw17-Oct-07 12:21 
GeneralWindows Vista Pin
Gabriel Topala16-Sep-06 17:14
Gabriel Topala16-Sep-06 17:14 
GeneralRe: Windows Vista Pin
handsomeme17-Sep-06 16:59
handsomeme17-Sep-06 16:59 
GeneralRe: Windows Vista Pin
tzukuei18-Oct-06 0:49
tzukuei18-Oct-06 0:49 
GeneralRe: Windows Vista Pin
Gabriel Topala18-Oct-06 1:35
Gabriel Topala18-Oct-06 1:35 
GeneralRe: Windows Vista Pin
tzukuei18-Oct-06 17:10
tzukuei18-Oct-06 17:10 
GeneralRe: Windows Vista Pin
Gabriel Topala18-Oct-06 18:00
Gabriel Topala18-Oct-06 18:00 
GeneralRe: Windows Vista [modified] Pin
tzukuei19-Oct-06 16:33
tzukuei19-Oct-06 16:33 
GeneralRe: Windows Vista Pin
awjapp21-Apr-07 7:34
awjapp21-Apr-07 7:34 
GeneralSolo Pin
hvw22-Aug-06 19:13
hvw22-Aug-06 19:13 
GeneralWow Good C# I like C# and used this! Pin
handsomeme22-Aug-06 14:38
handsomeme22-Aug-06 14:38 
Generalin c# Pin
solo22-Aug-06 6:17
solo22-Aug-06 6:17 
GeneralRe: in c#, not work! Pin
W4Rl0CK477-Nov-09 1:56
W4Rl0CK477-Nov-09 1:56 

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

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