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()
{
CString strResult;
HKEY hRegistryKey;
BYTE *DigitalProductID;
DWORD DataLength;
BYTE ProductKeyExtract [15];
char sCDKey [256];
long ByteCounter;
long ByteConvert;
int nCur;
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
};
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, NT_CURRENT,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
&hRegistryKey) == ERROR_SUCCESS)
{
DataLength = 164;
DigitalProductID = (BYTE *)malloc(DataLength);
memset(DigitalProductID, 0, DataLength);
if(RegQueryValueEx(hRegistryKey, "DigitalProductId",
NULL, NULL, DigitalProductID, &DataLength) == ERROR_SUCCESS)
{
for(ByteCounter=52; ByteCounter<=66; ByteCounter++)
{
ProductKeyExtract[ByteCounter - 52] =
DigitalProductID[ByteCounter];
}
ProductKeyExtract[sizeof(ProductKeyExtract)] = NULL;
}
}
memset(sCDKey, 0, sizeof(sCDKey));
for(ByteCounter=24; ByteCounter>=0; ByteCounter--)
{
nCur = 0;
for(ByteConvert=14; ByteConvert>=0; ByteConvert--)
{
nCur = (nCur * 256) ^ ProductKeyExtract[ByteConvert];
ProductKeyExtract[ByteConvert] = nCur / 24;
nCur = nCur % 24;
}
strrev(sCDKey);
strcat(sCDKey, KeyChars[nCur]);
strrev(sCDKey);
if(!(ByteCounter % 5) && (ByteCounter))
{
strrev(sCDKey);
strcat(sCDKey, "-");
strrev(sCDKey);
}
}
strResult.Format("%s", sCDKey);
RegCloseKey(hRegistryKey);
if(DigitalProductID) free(DigitalProductID);
return strResult.AllocSysString();
}
If you have any questions, mail them to me!