h_sw_registro/src/gui.cpp
2025-08-15 14:05:04 +08:00

184 lines
7.9 KiB
C++

#define NOMINMAX
#include <windows.h>
#include "registration_api.h"
#include <string>
// --- Control IDs ---
#define IDC_STATUS_LABEL 101
#define IDC_GEN_GROUP 102
#define IDC_GEN_MC_LABEL 103
#define IDC_GEN_MC_EDIT 104
#define IDC_GEN_EXP_LABEL 105
#define IDC_GEN_EXP_EDIT 106
#define IDC_GEN_BUTTON 107
#define IDC_DEC_GROUP 201
#define IDC_DEC_INPUT_LABEL 202
#define IDC_DEC_INPUT_EDIT 203
#define IDC_DEC_BUTTON 204
#define IDC_DEC_OUTPUT_LABEL 205
#define IDC_DEC_OUTPUT_EDIT 206
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// --- Global Handles for UI Controls ---
static HWND hStatusLabel;
static HWND hGenMachineCodeEdit, hGenExpirationEdit;
static HWND hDecInputEdit, hDecOutputEdit;
// --- Function to update the license status label ---
void UpdateValidationStatus(HWND hwnd) {
int result = ValidateLicenseFile();
std::string statusText;
switch (result) {
case 0:
statusText = "License Status: VALID";
break;
case 1:
statusText = "License Status: INVALID or NOT FOUND";
break;
case 2:
statusText = "License Status: EXPIRED";
break;
}
SetWindowText(hStatusLabel, statusText.c_str());
}
// --- Main Entry Point ---
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "RegistrationWindowClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"RegistrationWindowClass",
"Software Registration Tool",
(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, // Increased window height
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return (int)Msg.wParam;
}
// --- Window Procedure to Handle Messages ---
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// --- Top Part: Validation Status ---
hStatusLabel = CreateWindow("STATIC", "Checking license...", WS_VISIBLE | WS_CHILD | SS_CENTER, 20, 15, 440, 20, hwnd, (HMENU)IDC_STATUS_LABEL, NULL, NULL);
UpdateValidationStatus(hwnd);
// --- Middle Part: Decryption ---
CreateWindow("STATIC", "Decrypt Encrypted Code", WS_VISIBLE | WS_CHILD | BS_GROUPBOX, 10, 50, 460, 140, hwnd, (HMENU)IDC_DEC_GROUP, NULL, NULL);
CreateWindow("STATIC", "Encrypted Code:", WS_VISIBLE | WS_CHILD, 30, 80, 150, 20, hwnd, (HMENU)IDC_DEC_INPUT_LABEL, NULL, NULL);
hDecInputEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | WS_HSCROLL | ES_AUTOHSCROLL, 190, 80, 275, 40, hwnd, (HMENU)IDC_DEC_INPUT_EDIT, NULL, NULL);
SendMessage(hDecInputEdit, EM_SETLIMITTEXT, 64, 0);
CreateWindow("BUTTON", "Decrypt", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 190, 130, 120, 30, hwnd, (HMENU)IDC_DEC_BUTTON, NULL, NULL);
CreateWindow("STATIC", "Original Machine Code:", WS_VISIBLE | WS_CHILD, 30, 165, 150, 20, hwnd, (HMENU)IDC_DEC_OUTPUT_LABEL, NULL, NULL);
hDecOutputEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY, 190, 165, 260, 20, hwnd, (HMENU)IDC_DEC_OUTPUT_EDIT, NULL, NULL);
// --- Bottom Part: License Generation ---
CreateWindow("STATIC", "Generate License File", WS_VISIBLE | WS_CHILD | BS_GROUPBOX, 10, 200, 460, 160, hwnd, (HMENU)IDC_GEN_GROUP, NULL, NULL);
CreateWindow("STATIC", "Machine Code:", WS_VISIBLE | WS_CHILD, 30, 230, 150, 20, hwnd, (HMENU)IDC_GEN_MC_LABEL, NULL, NULL);
hGenMachineCodeEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER, 190, 230, 260, 20, hwnd, (HMENU)IDC_GEN_MC_EDIT, NULL, NULL);
CreateWindow("STATIC", "Expiration (YYYY-MM-DD):", WS_VISIBLE | WS_CHILD, 30, 260, 150, 20, hwnd, (HMENU)IDC_GEN_EXP_LABEL, NULL, NULL);
hGenExpirationEdit = CreateWindow("EDIT", "2099-12-31", WS_VISIBLE | WS_CHILD | WS_BORDER, 190, 260, 120, 20, hwnd, (HMENU)IDC_GEN_EXP_EDIT, NULL, NULL);
CreateWindow("BUTTON", "Generate License File", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 190, 300, 180, 30, hwnd, (HMENU)IDC_GEN_BUTTON, NULL, NULL);
break;
}
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDC_GEN_BUTTON: {
char machineCode[256];
char expirationDate[12];
GetWindowText(hGenMachineCodeEdit, machineCode, sizeof(machineCode));
GetWindowText(hGenExpirationEdit, expirationDate, sizeof(expirationDate));
if (strlen(machineCode) == 0) {
MessageBox(hwnd, "Please enter a machine code for generation.", "Error", MB_ICONERROR);
break;
}
int result = GenerateLicenseFileFromCode(machineCode, expirationDate);
if (result == 0) {
MessageBox(hwnd, "License.data generated successfully!", "Success", MB_OK);
UpdateValidationStatus(hwnd);
} else {
MessageBox(hwnd, "Failed to generate license file!", "Error", MB_ICONERROR);
}
break;
}
case IDC_DEC_BUTTON: {
char encryptedCode[64];
GetWindowText(hDecInputEdit, encryptedCode, sizeof(encryptedCode));
// --- DIAGNOSTIC ---
std::string text = encryptedCode;
std::string msg = "Pasted Text: '" + text + "'\nLength: " + std::to_string(text.length());
MessageBox(hwnd, msg.c_str(), "Debug Info", MB_OK);
// --- END DIAGNOSTIC ---
int len = strlen(encryptedCode);
if (strlen(encryptedCode) != 32) {
MessageBox(hwnd, "Please enter a valid 32-character encrypted code.", "Error", MB_ICONERROR);
break;
}
char decryptedResult[256];
int result = DecryptCodeAndGetMachineCode(encryptedCode, decryptedResult, sizeof(decryptedResult));
if (result == 0) {
SetWindowText(hDecOutputEdit, decryptedResult);
// Also copy the result to the generation input box for convenience
SetWindowText(hGenMachineCodeEdit, decryptedResult);
} else {
SetWindowText(hDecOutputEdit, ""); // Clear previous result
MessageBox(hwnd, "Decryption failed. The code is invalid or corrupted.", "Error", MB_ICONERROR);
}
break;
}
}
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}