Click here to Skip to main content
Licence 
First Posted 21 Aug 2006
Views 54,703
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  
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  
Elegant code solo
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
Web04 | 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