Thanks to visit codestin.com
Credit goes to www.codeproject.com

65.9K
CodeProject is changing. Read more.
Home

Extract Window Product Key - With an API !

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.07/5 (8 votes)

Aug 21, 2006

viewsIcon

100365

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!