Visual C++ 7.1Visual C++ 8.0Visual C++ 7.0Windows 2003Windows 2000Visual C++ 6.0Windows XP中级开发Visual StudioWindowsC++
提取 Windows 产品密钥——使用 API!






3.07/5 (8投票s)
2006年8月21日

100931
获取窗口的产品密钥!
引言
你好。这篇文章展示了一个非常有用的函数! 这个函数使用 Windows API 获取窗口的产品密钥,可以用于任何窗口,并且可以与任何工具和语言一起使用,例如 VB、C++、VS6、VS 7、8 等。
代码
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(); }
如果您有任何问题,请发邮件给我!