Witam, mam problem z przepisywaniem kodu z vb do c++. Program polega na tym, że odczytuje klucz produktu nawet z systemu windows 8. Mógłby ktoś zerknąć i pomóc mi gdzie popełniam błąd. Od razu mówię że skrypt w vb pobiera dane z rejestru, a ja przykładowo pobieram tylko potrzebne dane z tablicy int.
Kod vb (nie jest mój, pochodzi on z programu ShowKey_v1.3.vbs) (interesuje mnie funkcja tylko ConvertToKey.
'Option Explicit
If WScript.Arguments.Count = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", Chr(34) & WScript.ScriptFullName & Chr(34) & " Run", , "runas", 1
Else
On Error Resume Next
Dim OEM , objWMIService , colItems , objItem , verItems, ver , name
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set verItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_OperatingSystem",,48)
For Each objItem in verItems
ver = objItem.Version
name = Replace (objItem.Caption,"Microsoft ","")
Next
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM SoftwareLicensingService",,48)
For Each objItem in colItems
OEM = objItem.OA3xOriginalProductKey
Next
If OEM = "" Then
If CLng(Replace(ver,".","")) < 630000 Then
OEM = name & " not supported"
Else
OEM = "Key not present in firmware"
End If
End If
Set WshShell = CreateObject("WScript.Shell")
Key = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
DigitalID = WshShell.RegRead(key & "DigitalProductId")
ProductName = "Product Name: " & vbTab & WshShell.RegRead(Key & "ProductName") & vbNewLine
ProductID = "Product ID: " & vbTab & WshShell.RegRead(Key & "ProductID") & vbNewLine
ProductKey = "Installed Key: " & vbTab & ConvertToKey(DigitalID)
Product = ProductName & ProductID & ProductKey & vbNewLine & "OEM Key: " & vbTab & OEM
If vbYes = MsgBox(Product & vbNewLine & vbNewLine & vbNewLine & vbNewLine & "Save to a file?", vbYesNo + vbInformation, "ShowKey: Windows Product Key Information") then
Save(Product)
End if
Function ConvertToKey(Key)
Const KeyOffset = 52
isWin8 = (Key(66) \ 6) And 1
Key(66) = (Key(66) And &HF7) Or ((isWin8 And 2) * 4)
i = 24
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
X = 14
Do
Cur = Cur * 256
Cur = Key(X + KeyOffset) + Cur
Key(X + KeyOffset) = (Cur \ 24)
Cur = Cur Mod 24
X = X -1
Loop While X >= 0
i = i -1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
Last = Cur
Loop While i >= 0
If (isWin8 = 1) Then
keypart1 = Mid(KeyOutput, 2, Last)
insert = "N"
KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
If Last = 0 Then KeyOutput = insert & KeyOutput
End If
a = Mid(KeyOutput, 1, 5)
b = Mid(KeyOutput, 6, 5)
c = Mid(KeyOutput, 11, 5)
d = Mid(KeyOutput, 16, 5)
e = Mid(KeyOutput, 21, 5)
ConvertToKey = a & "-" & b & "-" & c & "-" & d & "-" & e
End Function
Function Save(Data)
Const ForWRITING = 2
Const asASCII = 0
Dim fso, f, fName, ts
fName = "Windows Key.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile fName
Set f = fso.GetFile(fName)
Set f = f.OpenAsTextStream(ForWRITING, asASCII)
f.Writeline Data
f.Close
End Function
End If
Mój program w C++
int Key[15] = {0x37, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x4C, 0x4C, 0x2C, 0x53, 0x50, 0x4A, 0xAD, 0x95, 0x09};
int isWin8 = ((Key[14] / 6) & 1);
//Key[14] = (Key[14] & 0xF7) | ((isWin8 & 2) * 4);
Key[14] = 0;
int i = 24;
string Chars = "BCDFGHJKMPQRTVWXY2346789";
string KeyOutput;
int Last;
do
{
int Cur = 0;
int X = 14;
do
{
Cur = Cur * 256;
Cur = Key[X] + Cur;
Key[X] = (Cur / 24);
Cur = (Cur % 24);
X = X - 1;
}while(X >= 0);
i = i - 1;
KeyOutput = Chars.substr(Cur + 1, 1) + KeyOutput;
Last = Cur;
}while(i >= 0);
if(isWin8 == 1)
{
string keypart1 = KeyOutput.substr(2, Last);
string insert = "N";
size_t pos = 0;
if((pos = KeyOutput.find(keypart1, 0)) != string::npos){
KeyOutput = KeyOutput.replace(2, 1, keypart1 + insert);
if(Last == 0)
{
KeyOutput = insert + KeyOutput;
}
}
}
cout << KeyOutput << endl;