This commit is contained in:
TheTank20 2025-07-23 02:18:27 -05:00
parent 01d6d8fc1a
commit 696162e916

820
xpmgr.cpp
View File

@ -6,6 +6,7 @@ typedef struct IUnknown IUnknown;
#include <comdef.h> #include <comdef.h>
#include <regex> #include <regex>
#include <TlHelp32.h> #include <TlHelp32.h>
#include <string>
// Check windows // Check windows
#if _WIN32 || _WIN64 #if _WIN32 || _WIN64
@ -16,6 +17,11 @@ typedef struct IUnknown IUnknown;
#endif #endif
#endif #endif
// Forward declarations of helper functions
BSTR FormatErrorMessage(const char* context, HRESULT status, ULONG returnCode = 0);
std::string ConvertToStdString(BSTR bstr);
BSTR ConvertCharToBSTR(const char* charString);
const char* specifiedProduct = nullptr; const char* specifiedProduct = nullptr;
// This is a really weird way of making a GUID. In short, this becomes ACADF079-CBCD-4032-83F2-FA47C4DB096F. Ignore the 0x and it makes sense. // This is a really weird way of making a GUID. In short, this becomes ACADF079-CBCD-4032-83F2-FA47C4DB096F. Ignore the 0x and it makes sense.
@ -100,44 +106,31 @@ public:
static BOOL XP_ComInitialized = FALSE; static BOOL XP_ComInitialized = FALSE;
static ICOMLicenseAgent* XP_LicenseAgent = nullptr; static ICOMLicenseAgent* XP_LicenseAgent = nullptr;
static BOOL XP_LoadLicenseManager() static BOOL XP_LoadLicenseManager() {
{ if (!XP_ComInitialized) {
if (!XP_ComInitialized) { const HRESULT status = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
const HRESULT status = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); if (FAILED(status)) {
if (FAILED(status)) { std::cout << ConvertToStdString(FormatErrorMessage("CoInitializeEx", status));
const char* errorString = "An error occurred at CoInitializeEx:"; return FALSE;
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X\n", errorString, static_cast<unsigned int>(status)); }
char* result = new char[bufferSize + 1]; XP_ComInitialized = TRUE;
snprintf(result, bufferSize + 1, "%s 0x%08X\n", errorString, static_cast<unsigned int>(status)); }
std::cout << result;
return FALSE; if (!XP_LicenseAgent) {
} HRESULT status = CoCreateInstance(XP_CLSID, nullptr, CLSCTX_INPROC_SERVER, XP_IID, reinterpret_cast<void **>(&XP_LicenseAgent));
XP_ComInitialized = TRUE; if (SUCCEEDED(status)) {
} ULONG dwRetCode;
if (!XP_LicenseAgent) { status = XP_LicenseAgent->Initialize(0xC475, 3, nullptr, &dwRetCode);
HRESULT status = CoCreateInstance(XP_CLSID, nullptr, CLSCTX_INPROC_SERVER, XP_IID, reinterpret_cast<void **>(&XP_LicenseAgent)); if (SUCCEEDED(status) && dwRetCode == 0) {
int good = 0; return TRUE;
if (SUCCEEDED(status)) { }
ULONG dwRetCode; XP_LicenseAgent->Release();
status = XP_LicenseAgent->Initialize(0xC475 /* This needs to be changed, I believe it's causing the crashing.*/ , 3, nullptr, &dwRetCode); XP_LicenseAgent = nullptr;
if (SUCCEEDED(status) && dwRetCode == 0) { }
good = 1; std::cout << ConvertToStdString(FormatErrorMessage("CoCreateInstance", status));
} return FALSE;
else { }
XP_LicenseAgent->Release(); return TRUE;
XP_LicenseAgent = nullptr;
}
}
if (!good) {
const char* errorString = "An error occurred at CoCreateInstance:";
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X\n", errorString, static_cast<unsigned int>(status));
char* result = new char[bufferSize + 1];
snprintf(result, bufferSize + 1, "%s 0x%08X\n", errorString, static_cast<unsigned int>(status));
std::cout << result;
return FALSE;
}
}
return TRUE;
} }
#pragma region Internals #pragma region Internals
@ -205,33 +198,6 @@ OLECHAR SizeToOLECHAR(size_t value)
return oChar; return oChar;
} }
BSTR ConvertToBSTR(const std::string& str) {
const int size = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
// ReSharper disable once CppLocalVariableMayBeConst
BSTR bstr = SysAllocStringLen(nullptr, size - 1);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, bstr, size);
return bstr;
}
std::string ConvertToStdString(BSTR bstr) {
const int size = WideCharToMultiByte(CP_UTF8, 0, bstr, -1, nullptr, 0, nullptr, nullptr);
const auto buffer = new char[size];
WideCharToMultiByte(CP_UTF8, 0, bstr, -1, buffer, size, nullptr, nullptr);
std::string result(buffer);
delete[] buffer;
return result;
}
BSTR ConvertCharToBSTR(const char* charString) {
const int size = MultiByteToWideChar(CP_UTF8, 0, charString, -1, nullptr, 0);
// ReSharper disable once CppLocalVariableMayBeConst
BSTR bstr = SysAllocStringLen(nullptr, size - 1);
MultiByteToWideChar(CP_UTF8, 0, charString, -1, bstr, size);
return bstr;
}
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
void safeStrncat(char* destination, const char* source, size_t size) { void safeStrncat(char* destination, const char* source, size_t size) {
@ -248,417 +214,445 @@ void safeStrncat(char* destination, const char* source, size_t size) {
} }
#pragma clang diagnostic pop #pragma clang diagnostic pop
bool IsProcessRunning(const wchar_t* processName) #pragma region Process Management
{
// ReSharper disable once CppLocalVariableMayBeConst
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
{
std::cout << "An error occurred at IsProcessRunning: Failed to create process snapshot." << std::endl;
return false;
}
PROCESSENTRY32W processEntry{}; struct ProcessHandle {
processEntry.dwSize = sizeof(PROCESSENTRY32W); HANDLE handle;
if (!Process32FirstW(snapshot, &processEntry)) explicit ProcessHandle(HANDLE h) : handle(h) {}
{ ~ProcessHandle() { if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle); }
CloseHandle(snapshot); operator HANDLE() const { return handle; }
std::cout << "An error occurred at IsProcessRunning: Failed to retrieve process information." << std::endl; };
return false;
}
while (Process32NextW(snapshot, &processEntry)) bool IsProcessRunning(const wchar_t* processName) {
{ ProcessHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
if (wcscmp(processEntry.szExeFile, processName) == 0) if (snapshot == INVALID_HANDLE_VALUE) {
{ std::cout << "Failed to create process snapshot: " << GetLastError() << std::endl;
CloseHandle(snapshot); return false;
return true; // Process found }
}
}
CloseHandle(snapshot); PROCESSENTRY32W processEntry{};
return false; // Process not found processEntry.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(snapshot, &processEntry)) {
std::cout << "Failed to retrieve process information: " << GetLastError() << std::endl;
return false;
}
do {
if (wcscmp(processEntry.szExeFile, processName) == 0) {
return true;
}
} while (Process32NextW(snapshot, &processEntry));
return false;
} }
bool TerminateProcessByName(const wchar_t* processName) bool TerminateProcessByName(const wchar_t* processName) {
{ ProcessHandle snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0));
// ReSharper disable once CppLocalVariableMayBeConst if (snapshot == INVALID_HANDLE_VALUE) {
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); std::cout << "Failed to create process snapshot: " << GetLastError() << std::endl;
if (snapshot == INVALID_HANDLE_VALUE) return false;
{ }
return false;
}
PROCESSENTRY32W processEntry{}; PROCESSENTRY32W processEntry{};
processEntry.dwSize = sizeof(PROCESSENTRY32W); processEntry.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(snapshot, &processEntry))
{
CloseHandle(snapshot);
return false;
}
while (Process32NextW(snapshot, &processEntry)) if (!Process32FirstW(snapshot, &processEntry)) {
{ std::cout << "Failed to retrieve process information: " << GetLastError() << std::endl;
if (wcscmp(processEntry.szExeFile, processName) == 0) return false;
{ }
// ReSharper disable once CppLocalVariableMayBeConst
HANDLE processHandle = OpenProcess(PROCESS_TERMINATE, FALSE, processEntry.th32ProcessID);
if (processHandle != nullptr)
{
if (TerminateProcess(processHandle, 0))
{
CloseHandle(processHandle);
CloseHandle(snapshot);
return true; // Process terminated successfully
}
else
{
CloseHandle(processHandle);
CloseHandle(snapshot);
return false; // Failed to terminate the process
}
}
else
{
CloseHandle(snapshot);
return false; // Failed to open process handle
}
}
}
CloseHandle(snapshot); do {
return false; // Process not found if (wcscmp(processEntry.szExeFile, processName) == 0) {
ProcessHandle processHandle(OpenProcess(PROCESS_TERMINATE, FALSE, processEntry.th32ProcessID));
if (processHandle == nullptr) {
std::cout << "Failed to open process: " << GetLastError() << std::endl;
return false;
}
if (!TerminateProcess(processHandle, 0)) {
std::cout << "Failed to terminate process: " << GetLastError() << std::endl;
return false;
}
return true;
}
} while (Process32NextW(snapshot, &processEntry));
std::cout << "Process not found: " << ConvertToStdString(ConvertCharToBSTR(reinterpret_cast<const char*>(processName))) << std::endl;
return false;
} }
#pragma endregion #pragma endregion
#pragma region Helper Functions
BSTR ConvertCharToBSTR(const char* charString) {
if (!charString) return nullptr;
const int size = MultiByteToWideChar(CP_UTF8, 0, charString, -1, nullptr, 0);
BSTR bstr = SysAllocStringLen(nullptr, size - 1);
MultiByteToWideChar(CP_UTF8, 0, charString, -1, bstr, size);
return bstr;
}
std::string ConvertToStdString(BSTR bstr) {
if (!bstr) return "";
const int size = WideCharToMultiByte(CP_UTF8, 0, bstr, -1, nullptr, 0, nullptr, nullptr);
std::string result(size - 1, '\0');
WideCharToMultiByte(CP_UTF8, 0, bstr, -1, &result[0], size, nullptr, nullptr);
return result;
}
BSTR FormatErrorMessage(const char* context, HRESULT status, ULONG returnCode) {
std::string errorMsg = "An error occurred at " + std::string(context) + ": ";
char statusBuffer[32];
snprintf(statusBuffer, sizeof(statusBuffer), "0x%08X", static_cast<unsigned int>(status));
errorMsg += statusBuffer;
if (returnCode != 0) {
char returnCodeBuffer[32];
snprintf(returnCodeBuffer, sizeof(returnCodeBuffer), " %lu", returnCode);
errorMsg += returnCodeBuffer;
}
return ConvertCharToBSTR(errorMsg.c_str());
}
#pragma region Windows XP #pragma region Windows XP
static BSTR XP_GetWPALeft() { static BSTR XP_GetWPALeft() {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
ULONG dwWPALeft = 0, dwEvalLeft = 0; ULONG dwWPALeft = 0, dwEvalLeft = 0;
const HRESULT status = XP_LicenseAgent->GetExpirationInfo(&dwWPALeft, &dwEvalLeft); const HRESULT status = XP_LicenseAgent->GetExpirationInfo(&dwWPALeft, &dwEvalLeft);
if (FAILED(status)) { if (FAILED(status)) {
XP_LicenseAgent->Release(); XP_LicenseAgent->Release();
XP_LicenseAgent = nullptr; XP_LicenseAgent = nullptr;
const char* errorString = "An error occurred at CoInitializeEx:"; return FormatErrorMessage("GetExpirationInfo", status);
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); }
char* result = new char[bufferSize + 1]; if (dwWPALeft == 0x7FFFFFFF) {
snprintf(result, bufferSize + 1, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated");
const int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, result, -1, nullptr, 0); }
auto* wideResult = new OLECHAR[wideCharSize];
MultiByteToWideChar(CP_UTF8, 0, result, -1, wideResult, wideCharSize);
return SysAllocString(wideResult);
}
if (dwWPALeft == 0x7FFFFFFF) {
return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated");
}
wchar_t buffer[16]; wchar_t buffer[16];
swprintf(buffer, L"%lu", dwWPALeft); swprintf(buffer, L"%lu", dwWPALeft);
return SysAllocString(buffer); return SysAllocString(buffer);
} }
static BSTR XP_GetEvalLeft() { static BSTR XP_GetEvalLeft() {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
ULONG dwWPALeft = 0, dwEvalLeft = 0; ULONG dwWPALeft = 0, dwEvalLeft = 0;
const HRESULT status = XP_LicenseAgent->GetExpirationInfo(&dwWPALeft, &dwEvalLeft); const HRESULT status = XP_LicenseAgent->GetExpirationInfo(&dwWPALeft, &dwEvalLeft);
if (FAILED(status)) { if (FAILED(status)) {
XP_LicenseAgent->Release(); XP_LicenseAgent->Release();
XP_LicenseAgent = nullptr; XP_LicenseAgent = nullptr;
const char* errorString = "An error occurred at GetExpirationInfo:"; return FormatErrorMessage("GetExpirationInfo", status);
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); }
char* result = new char[bufferSize + 1]; if (dwEvalLeft == 0x7FFFFFFF) {
snprintf(result, bufferSize + 1, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); return SysAllocString(L"An error occurred at GetEvalLeft: Not an evaluation copy");
const int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, result, -1, nullptr, 0); }
auto* wideResult = new OLECHAR[wideCharSize]; wchar_t buffer[16];
MultiByteToWideChar(CP_UTF8, 0, result, -1, wideResult, wideCharSize); swprintf(buffer, L"%lu", dwEvalLeft);
return SysAllocString(wideResult); return SysAllocString(buffer);
} }
if (dwEvalLeft == 0x7FFFFFFF) {
return SysAllocString(L"An error occurred at GetEvalLeft: Not an evaluation copy"); #pragma region Windows XP Activation
}
wchar_t buffer[16]; enum class VerificationResult {
swprintf(buffer, L"%lu", dwWPALeft); Success,
return SysAllocString(buffer); InvalidFormat,
VerificationFailed,
AlreadyActivated,
LoadError
};
static VerificationResult VerifyConfirmationIDChunk(const std::string& chunk) {
if (chunk.length() != 6) {
return VerificationResult::InvalidFormat;
}
LONG pbValue;
const HRESULT status = XP_LicenseAgent->VerifyCheckDigits(ConvertCharToBSTR(chunk.c_str()), &pbValue);
return (SUCCEEDED(status) && pbValue) ? VerificationResult::Success : VerificationResult::VerificationFailed;
} }
static BSTR XP_VerifyCheckDigits(BSTR cidChunk) { static BSTR XP_VerifyCheckDigits(BSTR cidChunk) {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
if (0 == wcscmp(XP_GetWPALeft(), L"An error occurred at GetWPALeft: Windows is activated")) { const std::string activationCheck = ConvertToStdString(XP_GetWPALeft());
return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated"); if (activationCheck == "An error occurred at GetWPALeft: Windows is activated") {
} return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated");
}
LONG pbValue; LONG pbValue;
const HRESULT status = XP_LicenseAgent->VerifyCheckDigits(cidChunk, &pbValue); const HRESULT status = XP_LicenseAgent->VerifyCheckDigits(cidChunk, &pbValue);
if (FAILED(status) || !pbValue) { return SUCCEEDED(status) && pbValue
char errorMessage[70] = "An error occurred at XP_VerifyCheckDigits:"; ? SysAllocString(L"Successfully verified CID chunk")
char pbValueChar[20]; : FormatErrorMessage("XP_VerifyCheckDigits", status);
snprintf(errorMessage, sizeof(errorMessage), "%ld", pbValue);
safeStrncat(errorMessage, pbValueChar, sizeof(errorMessage));
const int len = MultiByteToWideChar(CP_UTF8, 0, errorMessage, -1, nullptr, 0);
auto* oleCharString = new OLECHAR[len];
MultiByteToWideChar(CP_UTF8, 0, errorMessage, -1, oleCharString, len);
return SysAllocString(oleCharString);
}
return SysAllocString(L"Successfully verified CID chunk");
} }
static BSTR XP_SetConfirmationID(BSTR confirmationID) { static BSTR XP_SetConfirmationID(BSTR confirmationID) {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
if (0 == wcscmp(XP_GetWPALeft(), L"An error occurred at GetWPALeft: Windows is activated")) { const std::string activationCheck = ConvertToStdString(XP_GetWPALeft());
return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated"); if (activationCheck == "An error occurred at GetWPALeft: Windows is activated") {
} return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated");
}
const int length = static_cast<int>(SysStringLen(confirmationID)); // Convert and clean up the confirmation ID
char* str = new char[length + 1]; std::string cidStr = ConvertToStdString(confirmationID);
WideCharToMultiByte(CP_UTF8, 0, confirmationID, length, str, length, nullptr, nullptr); cidStr.erase(std::remove(cidStr.begin(), cidStr.end(), '-'), cidStr.end());
str[length] = '\0';
std::string inputString(str); // Verify each 6-character chunk
inputString.erase(std::remove(inputString.begin(), inputString.end(), '-'), inputString.end()); // remove -'s for (size_t i = 0; i < cidStr.length(); i += 6) {
for (size_t i = 0; i < inputString.length(); i += 6) { const std::string chunk = cidStr.substr(i, 6);
std::string substring = inputString.substr(i, 6); const auto result = VerifyConfirmationIDChunk(chunk);
const char* cstr = substring.c_str();
if (0 != wcscmp(XP_VerifyCheckDigits(ConvertCharToBSTR(cstr)), L"Successfully verified CID chunk")) {
return SysAllocString(L"An error occurred at XP_VerifyCheckDigits: Check for misspelling");
}
}
ULONG dwRetCode; switch (result) {
const HRESULT status = XP_LicenseAgent->DepositConfirmationId(confirmationID, &dwRetCode); case VerificationResult::InvalidFormat:
if (FAILED(status) || dwRetCode) { return SysAllocString(L"An error occurred: Invalid confirmation ID format");
const char* errorString = "An error occurred at DepositConfirmationId:"; case VerificationResult::VerificationFailed:
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X %lu", errorString, static_cast<unsigned int>(status), dwRetCode); return SysAllocString(L"An error occurred: Confirmation ID verification failed");
char* result = new char[bufferSize + 1]; case VerificationResult::Success:
snprintf(result, bufferSize + 1, "%s 0x%08X %lu", errorString, static_cast<unsigned int>(status), dwRetCode); continue;
default:
return SysAllocString(L"An error occurred: Unexpected verification result");
}
}
const int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, result, -1, nullptr, 0); // Deposit the full confirmation ID
auto* wideResult = new OLECHAR[wideCharSize]; ULONG dwRetCode;
MultiByteToWideChar(CP_UTF8, 0, result, -1, wideResult, wideCharSize); const HRESULT status = XP_LicenseAgent->DepositConfirmationId(confirmationID, &dwRetCode);
return SysAllocString(wideResult); if (FAILED(status) || dwRetCode) {
} return FormatErrorMessage("DepositConfirmationId", status, dwRetCode);
}
system("rundll32 setupapi,InstallHinfSection DEL_OOBE_ACTIVATE 132 syssetup.inf"); // remove activation shortcuts // Handle WPA notifier process
if (IsProcessRunning(L"wpabaln.exe")) {
if (!TerminateProcessByName(L"wpabaln.exe")) {
std::cout << "Warning: Failed to terminate WPA notifier process" << std::endl;
}
}
if (IsProcessRunning(L"wpabaln.exe")) { // end WPA notifier process if there // Clean up activation files
TerminateProcessByName(L"wpabaln.exe"); HMODULE hMod = LoadLibraryW(L"setupapi.dll");
} if (hMod) {
using InstallHinfSectionFunc = BOOL (WINAPI*)(HWND, HINSTANCE, PCWSTR, INT);
if (auto installHinfSection = reinterpret_cast<InstallHinfSectionFunc>(GetProcAddress(hMod, "InstallHinfSectionW"))) {
SetLastError(0);
const BOOL result = installHinfSection(nullptr, nullptr, L"DEL_OOBE_ACTIVATE 132 syssetup.inf", 132);
const DWORD error = GetLastError();
return SysAllocString(L"Successfully set confirmation ID"); if (!result && error != 0 && error != 6) { // Ignore error 6 (ERROR_INVALID_HANDLE) as it indicates success
std::cout << "Warning: Failed to remove activation reminders. Error: " << error << std::endl;
std::cout << "You can try to run the following command yourself: " << std::endl;
std::cout << "rundll32 setupapi,InstallHinfSection DEL_OOBE_ACTIVATE 132 syssetup.inf" << std::endl;
}
}
FreeLibrary(hMod);
}
return SysAllocString(L"Successfully set confirmation ID");
} }
static BSTR XP_GetInstallationID() { static BSTR XP_GetInstallationID() {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
if (0 == wcscmp(XP_GetWPALeft(), L"An error occurred at GetWPALeft: Windows is activated")) { if (0 == wcscmp(XP_GetWPALeft(), L"An error occurred at GetWPALeft: Windows is activated")) {
return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated"); return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated");
} }
BSTR installationID = nullptr; BSTR installationID = nullptr;
const HRESULT status = XP_LicenseAgent->GenerateInstallationId(&installationID); const HRESULT status = XP_LicenseAgent->GenerateInstallationId(&installationID);
if (FAILED(status) || !installationID) { if (FAILED(status) || !installationID) {
const char* errorString = "An error occurred at GenerateInstallationId:"; return FormatErrorMessage("GenerateInstallationId", status);
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); }
char* result = new char[bufferSize + 1]; return installationID;
snprintf(result, bufferSize + 1, "%s 0x%08X", errorString, static_cast<unsigned int>(status));
const int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, result, -1, nullptr, 0);
auto* wideResult = new OLECHAR[wideCharSize];
MultiByteToWideChar(CP_UTF8, 0, result, -1, wideResult, wideCharSize);
return SysAllocString(wideResult);
}
else {
return installationID;
}
} }
static BSTR XP_SetProductKey(LPWSTR productKey) { static BSTR XP_SetProductKey(LPWSTR productKey) {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
std::wstring ws(productKey); if (0 == wcscmp(XP_GetWPALeft(), L"An error occurred at GetWPALeft: Windows is activated")) {
const std::string productKeyToRegex = std::string(ws.begin(), ws.end()); return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated");
const std::regex pattern("[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}"); }
if (!std::regex_match(productKeyToRegex, pattern)) { // Validate product key format
return SysAllocString(L"An error occurred at regex_match: Product key is invalid"); std::wstring ws(productKey);
} const std::string productKeyToRegex = std::string(ws.begin(), ws.end());
const std::regex pattern("[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}-[2346789BCDFGHJKMPQRTVWXY]{5}");
if (0 == wcscmp(XP_GetWPALeft(), L"An error occurred at GetWPALeft: Windows is activated")) { if (!std::regex_match(productKeyToRegex, pattern)) {
return SysAllocString(L"An error occurred at GetWPALeft: Windows is activated"); return SysAllocString(L"An error occurred at regex_match: Product key is invalid");
} }
const HRESULT status = XP_LicenseAgent->SetProductKey(productKey); const HRESULT status = XP_LicenseAgent->SetProductKey(productKey);
if (FAILED(status)) { if (FAILED(status)) {
const char* errorString = "An error occurred at SetProductKey:"; return FormatErrorMessage("SetProductKey", status);
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); }
char* result = new char[bufferSize + 1]; return SysAllocString(L"Successfully set product key");
snprintf(result, bufferSize + 1, "%s 0x%08X", errorString, static_cast<unsigned int>(status));
const int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, result, -1, nullptr, 0);
auto* wideResult = new OLECHAR[wideCharSize];
MultiByteToWideChar(CP_UTF8, 0, result, -1, wideResult, wideCharSize);
return SysAllocString(wideResult);
}
else {
return SysAllocString(L"Successfully set product key");
}
} }
static BSTR XP_GetProductID() { static BSTR XP_GetProductID() {
if (!XP_LoadLicenseManager()) { if (!XP_LoadLicenseManager()) {
return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load"); return SysAllocString(L"An error occurred at XP_LoadLicenseManager: Failed to load");
} }
BSTR productID = nullptr; BSTR productID = nullptr;
const HRESULT status = XP_LicenseAgent->GetProductID(&productID);
const HRESULT status = XP_LicenseAgent->GetProductID(&productID); if (FAILED(status)) {
if (FAILED(status)) { return FormatErrorMessage("GetProductID", status);
const char* errorString = "An error occurred at GetProductID:"; }
const int bufferSize = snprintf(nullptr, 0, "%s 0x%08X", errorString, static_cast<unsigned int>(status)); return productID;
char* result = new char[bufferSize + 1];
snprintf(result, bufferSize + 1, "%s 0x%08X", errorString, static_cast<unsigned int>(status));
const int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, result, -1, nullptr, 0);
auto* wideResult = new OLECHAR[wideCharSize];
MultiByteToWideChar(CP_UTF8, 0, result, -1, wideResult, wideCharSize);
return SysAllocString(wideResult);
}
else {
return productID;
}
} }
#pragma endregion #pragma endregion
#pragma region Command Line Handling
struct CommandLineArgs {
static const char* getCmdOption(char** begin, char** end, const std::string& option) {
char** itr = std::find(begin, end, option);
if (itr != end && ++itr != end) {
return *itr;
}
return nullptr;
}
static bool cmdOptionExists(char** begin, char** end, const std::string& option) {
return std::find(begin, end, option) != end;
}
static std::string readFromStdin() {
std::string input;
if (!std::cin.eof() && std::getline(std::cin, input)) {
input.erase(std::find_if(input.rbegin(), input.rend(), [](unsigned char ch) {
return !std::isspace(ch);
}).base(), input.end());
return input;
}
return "";
}
};
#pragma endregion
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
int main(int argc, char* argv[]) int main(int argc, char* argv[]) {
{ const char* USAGE_TEXT =
const char* text = "xpmgr - Windows XP License Manager (compiled on " __DATE__ " " __TIME__ ")\n"
"xpmgr - Windows XP License Manager (compiled on " __DATE__ " " __TIME__ ")\n" "\n"
"\n" "Usage: \n"
"Usage: \n" "/dti: Gets the Installation ID\n"
"/dti: Gets the Installation ID\n" "/atp [cid]: Sets Confirmation ID (If successful, activates Windows/Office) (can also read from stdin)\n"
"/atp [cid]: Sets Confirmation ID (If successful, activates Windows/Office) (can also read from stdin)\n" "/xpr: Gets the days before activation is required (Grace period)\n"
"/xpr: Gets the days before activation is required (Grace period)\n" "/xpr-eval: Gets the days before the evaluation period expires (Eval. copies only)\n"
"/xpr-eval: Gets the days before the evaluation period expires (Eval. copies only)\n" "/ipk [pkey]: Sets/changes product key (can also read from stdin)\n"
"/ipk [pkey]: Sets/changes product key (can also read from stdin)\n" "/dli: Gets the product ID of Windows\n"
"/dli: Gets the product ID of Windows\n" "/?: Displays this message";
"/?: Displays this message";
if (cmdOptionExists(argv, argv + argc, "--GetUsage")) { if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "--GetUsage")) {
std::cout << text; std::cout << USAGE_TEXT;
return 0; return 0;
} }
specifiedProduct = "WindowsNT5x"; // Check Windows version
OSVERSIONINFOEX info = {};
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&info));
if (std::strcmp(specifiedProduct, "WindowsNT5x") == 0) { if (info.dwMajorVersion != 5) {
SYSTEM_INFO systemInfo; std::cout << "An error occurred: Windows management only works on Windows NT 5.1 and 5.2.";
GetNativeSystemInfo(&systemInfo); if (info.dwMajorVersion > 5) {
std::cout << " Use slmgr instead: https://learn.microsoft.com/en-us/windows-server/get-started/activation-slmgr-vbs-options";
return 3;
}
return 2;
}
if (info.dwMinorVersion != 1 && info.dwMinorVersion != 2) {
std::cout << "An error occurred: Windows management only works on Windows NT 5.1 and 5.2.";
if (info.dwMinorVersion == 0) {
std::cout << " You should be fine anyways, since Windows 2000 doesn't use Product Activation.";
return 4;
}
return 5;
}
// Check architecture
#ifdef ENVIRONMENT32 #ifdef ENVIRONMENT32
if (systemInfo.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL) { // running under WOW64 SYSTEM_INFO systemInfo;
if (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) { // is AMD64 GetNativeSystemInfo(&systemInfo);
std::cout << "An error occurred at systemInfo: Incorrect version of xpmgr. You need to download the x64 version."; if (systemInfo.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL) {
} if (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {
else if (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) { // is IA64 std::cout << "An error occurred: Incorrect version of xpmgr. You need to download the x64 version.";
std::cout << "An error occurred at systemInfo: Windows Product Activation does not exist on this platform."; return 1;
} }
else { // is PPC, megafart 386, whatever else if (systemInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) {
std::cout << "An error occurred at systemInfo: Incorrect version of xpmgr. Send an issue at https://github.com/UMSKT/xpmgr/issues if you want to help us make a version for your platform!"; std::cout << "An error occurred: Windows Product Activation does not exist on this platform.";
} return 1;
return 1; }
} std::cout << "An error occurred: Incorrect version of xpmgr. Send an issue at https://github.com/UMSKT/xpmgr/issues if you want to help us make a version for your platform!";
return 1;
}
#endif #endif
OSVERSIONINFOEX info = {};
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&info));
if (info.dwMajorVersion != 5) { // Handle commands
std::cout << "An error occurred at OSVERSIONINFOEX: Windows management only works on Windows NT 5.1 and 5.2."; if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "/dti")) {
if (info.dwMajorVersion > 5) { std::cout << ConvertToStdString(XP_GetInstallationID());
std::cout << " Use slmgr instead: https://learn.microsoft.com/en-us/windows-server/get-started/activation-slmgr-vbs-options"; }
return 3; else if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "/atp")) {
} const char* confirmationId = CommandLineArgs::getCmdOption(argv, argv + argc, "/atp");
return 2; if (!confirmationId) {
} std::string pipedInput = CommandLineArgs::readFromStdin();
else { if (!pipedInput.empty()) {
if (info.dwMinorVersion != 1 && info.dwMinorVersion != 2) { std::cout << ConvertToStdString(XP_SetConfirmationID(ConvertCharToBSTR(pipedInput.c_str())));
std::cout << "An error occurred at OSVERSIONINFOEX: Windows management only works on Windows NT 5.1 and 5.2."; } else {
if (info.dwMinorVersion == 0) { std::cout << "An error occurred: No confirmation ID provided via argument or pipe\n\n" << USAGE_TEXT;
std::cout << " You should be fine anyways, since Windows 2000 doesn't use Product Activation."; return 7;
return 4; }
} } else {
return 5; std::cout << ConvertToStdString(XP_SetConfirmationID(ConvertCharToBSTR(confirmationId)));
} }
} }
} else if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "/xpr")) {
std::cout << ConvertToStdString(XP_GetWPALeft());
}
else if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "/xpr-eval")) {
std::cout << ConvertToStdString(XP_GetEvalLeft());
}
else if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "/ipk")) {
const char* productKey = CommandLineArgs::getCmdOption(argv, argv + argc, "/ipk");
if (!productKey) {
std::string pipedInput = CommandLineArgs::readFromStdin();
if (!pipedInput.empty()) {
std::cout << ConvertToStdString(XP_SetProductKey(convertCharArrayToLPCWSTR(pipedInput.c_str())));
} else {
std::cout << "An error occurred: No product key provided via argument or pipe\n\n" << USAGE_TEXT;
return 7;
}
} else {
std::cout << ConvertToStdString(XP_SetProductKey(convertCharArrayToLPCWSTR(productKey)));
}
}
else if (CommandLineArgs::cmdOptionExists(argv, argv + argc, "/dli")) {
std::cout << ConvertToStdString(XP_GetProductID());
}
else {
std::cout << "An error occurred: No arguments listed\n\n" << USAGE_TEXT;
return 7;
}
if (cmdOptionExists(argv, argv + argc, "/dti")) { return 0;
std::cout << ConvertToStdString(XP_GetInstallationID());
return 0;
}
else if (cmdOptionExists(argv, argv + argc, "/atp")) {
const char* confirmationId = getCmdOption(argv, argv + argc, "/atp");
if (!confirmationId) {
// No argument provided, try reading from stdin
std::string pipedInput = readFromStdin();
if (!pipedInput.empty()) {
std::cout << ConvertToStdString(XP_SetConfirmationID(ConvertCharToBSTR(pipedInput.c_str())));
} else {
std::cout << "An error occurred at main: No confirmation ID provided via argument or pipe\n\n";
std::cout << text;
return 7;
}
} else {
std::cout << ConvertToStdString(XP_SetConfirmationID(ConvertCharToBSTR(confirmationId)));
}
return 0;
}
else if (cmdOptionExists(argv, argv + argc, "/xpr")) {
std::cout << ConvertToStdString(XP_GetWPALeft());
return 0;
}
else if (cmdOptionExists(argv, argv + argc, "/xpr-eval")) {
std::cout << ConvertToStdString(XP_GetEvalLeft());
return 0;
}
else if (cmdOptionExists(argv, argv + argc, "/ipk")) {
const char* productKey = getCmdOption(argv, argv + argc, "/ipk");
if (!productKey) {
// No argument provided, try reading from stdin
std::string pipedInput = readFromStdin();
if (!pipedInput.empty()) {
std::cout << ConvertToStdString(XP_SetProductKey(convertCharArrayToLPCWSTR(pipedInput.c_str())));
} else {
std::cout << "An error occurred at main: No product key provided via argument or pipe\n\n";
std::cout << text;
return 7;
}
} else {
std::cout << ConvertToStdString(XP_SetProductKey(convertCharArrayToLPCWSTR(productKey)));
}
return 0;
}
else if (cmdOptionExists(argv, argv + argc, "/dli")) {
std::cout << ConvertToStdString(XP_GetProductID());
return 0;
}
else {
std::cout << "An error occurred at main: No arguments listed\n\n";
std::cout << text;
return 7;
}
} }
#pragma clang diagnostic pop #pragma clang diagnostic pop