h_sw_registro/src/gui.cpp

176 lines
7.5 KiB
C++
Raw Normal View History

2025-08-05 15:32:23 +00:00
#define NOMINMAX
2025-07-26 02:32:02 +00:00
#include <windows.h>
#include "registration_api.h"
#include <string>
2025-08-05 15:32:23 +00:00
// --- Control IDs ---
2025-07-26 02:32:02 +00:00
#define IDC_STATUS_LABEL 101
2025-08-05 15:32:23 +00:00
#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
2025-07-26 02:32:02 +00:00
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
2025-08-05 15:32:23 +00:00
// --- Global Handles for UI Controls ---
static HWND hStatusLabel;
static HWND hGenMachineCodeEdit, hGenExpirationEdit;
static HWND hDecInputEdit, hDecOutputEdit;
2025-07-26 02:32:02 +00:00
2025-08-05 15:32:23 +00:00
// --- Function to update the license status label ---
2025-07-26 02:32:02 +00:00
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());
}
2025-08-05 15:32:23 +00:00
// --- Main Entry Point ---
2025-07-26 02:32:02 +00:00
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",
2025-08-05 15:32:23 +00:00
"Software Registration Tool",
2025-07-26 02:32:02 +00:00
(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
2025-08-05 15:32:23 +00:00
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, // Increased window height
2025-07-26 02:32:02 +00:00
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);
}
2025-08-05 15:32:23 +00:00
return (int)Msg.wParam;
2025-07-26 02:32:02 +00:00
}
2025-08-05 15:32:23 +00:00
// --- Window Procedure to Handle Messages ---
2025-07-26 02:32:02 +00:00
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// --- Top Part: Validation Status ---
2025-08-05 15:32:23 +00:00
hStatusLabel = CreateWindow("STATIC", "Checking license...", WS_VISIBLE | WS_CHILD | SS_CENTER, 20, 15, 440, 20, hwnd, (HMENU)IDC_STATUS_LABEL, NULL, NULL);
2025-07-26 02:32:02 +00:00
UpdateValidationStatus(hwnd);
2025-08-05 15:32:23 +00:00
// --- Middle Part: Decryption ---
CreateWindow("STATIC", "Decrypt Encrypted Code", WS_VISIBLE | WS_CHILD | BS_GROUPBOX, 10, 50, 460, 120, 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);
2025-08-05 15:51:10 +00:00
hDecInputEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER, 190, 80, 270, 20, hwnd, (HMENU)IDC_DEC_INPUT_EDIT, NULL, NULL);
2025-08-05 15:32:23 +00:00
CreateWindow("BUTTON", "Decrypt", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 190, 110, 120, 30, hwnd, (HMENU)IDC_DEC_BUTTON, NULL, NULL);
CreateWindow("STATIC", "Original Machine Code:", WS_VISIBLE | WS_CHILD, 30, 145, 150, 20, hwnd, (HMENU)IDC_DEC_OUTPUT_LABEL, NULL, NULL);
hDecOutputEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_READONLY, 190, 145, 260, 20, hwnd, (HMENU)IDC_DEC_OUTPUT_EDIT, NULL, NULL);
2025-07-26 02:32:02 +00:00
2025-08-05 15:32:23 +00:00
// --- Bottom Part: License Generation ---
CreateWindow("STATIC", "Generate License File", WS_VISIBLE | WS_CHILD | BS_GROUPBOX, 10, 180, 460, 160, hwnd, (HMENU)IDC_GEN_GROUP, NULL, NULL);
CreateWindow("STATIC", "Machine Code:", WS_VISIBLE | WS_CHILD, 30, 210, 150, 20, hwnd, (HMENU)IDC_GEN_MC_LABEL, NULL, NULL);
hGenMachineCodeEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER, 190, 210, 260, 20, hwnd, (HMENU)IDC_GEN_MC_EDIT, NULL, NULL);
CreateWindow("STATIC", "Expiration (YYYY-MM-DD):", WS_VISIBLE | WS_CHILD, 30, 240, 150, 20, hwnd, (HMENU)IDC_GEN_EXP_LABEL, NULL, NULL);
hGenExpirationEdit = CreateWindow("EDIT", "2099-12-31", WS_VISIBLE | WS_CHILD | WS_BORDER, 190, 240, 120, 20, hwnd, (HMENU)IDC_GEN_EXP_EDIT, NULL, NULL);
CreateWindow("BUTTON", "Generate License File", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 190, 280, 180, 30, hwnd, (HMENU)IDC_GEN_BUTTON, NULL, NULL);
2025-07-26 02:32:02 +00:00
break;
}
case WM_COMMAND: {
switch (LOWORD(wParam)) {
2025-08-05 15:32:23 +00:00
case IDC_GEN_BUTTON: {
2025-07-26 02:32:02 +00:00
char machineCode[256];
2025-08-05 15:32:23 +00:00
char expirationDate[12];
GetWindowText(hGenMachineCodeEdit, machineCode, sizeof(machineCode));
GetWindowText(hGenExpirationEdit, expirationDate, sizeof(expirationDate));
2025-07-26 02:32:02 +00:00
if (strlen(machineCode) == 0) {
2025-08-05 15:32:23 +00:00
MessageBox(hwnd, "Please enter a machine code for generation.", "Error", MB_ICONERROR);
2025-07-26 02:32:02 +00:00
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;
}
2025-08-05 15:32:23 +00:00
case IDC_DEC_BUTTON: {
char encryptedCode[64];
GetWindowText(hDecInputEdit, encryptedCode, sizeof(encryptedCode));
int len = strlen(encryptedCode);
2025-08-05 15:51:10 +00:00
if (strlen(encryptedCode) != 32) {
2025-08-05 15:32:23 +00:00
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;
}
2025-07-26 02:32:02 +00:00
}
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
2025-08-05 15:32:23 +00:00
}