[C++] 纯文本查看 复制代码
#include "pch.h" // 若为较新的 VS 空项目,PCH 头文件可能名为 "pch.h",按需替换
#include <windows.h>
#include <strsafe.h> // StringCchCopy* 系列:带缓冲区大小的安全拷贝
#include "SecureEnginePluginsSdk.h"
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
/******************************************************************************
* 手写对话框的控件 ID(不再依赖 Resource.h / .rc)
*****************************************************************************/
#define IDC_HWID_EDIT 1001 // 机器码显示框(只读)
#define IDC_ACT_EDIT 1002 // 激活码输入框
#define IDC_ACTIVATE_BTN 1003 // “激活”按钮
// “取消”按钮直接复用系统 IDCANCEL(=2),这样 IsDialogMessage 处理 Esc 时也能触发它
/******************************************************************************
* 全局状态
*****************************************************************************/
static HINSTANCE g_hDll = NULL;
static TWDCfunctionsArrayW g_RegFuncs = {};
static BOOL g_bRegistered = FALSE;
static HFONT g_hFont = NULL;
static int g_dpi = 96; // 运行时探测到的系统 DPI
static wchar_t g_HardwareId[256] = {};
static BOOL g_bTrialMsgShown = FALSE; // 试用提示每进程仅弹一次,避免重复打扰
// 96 DPI 基准坐标 -> 当前 DPI 下的实际像素
#define DPI(v) MulDiv((v), g_dpi, 96)
static LRESULT CALLBACK ActivationWndProc(HWND, UINT, WPARAM, LPARAM);
static void ShowActivationDialogW(void);
/******************************************************************************
* 激活窗口过程
*****************************************************************************/
static LRESULT CALLBACK
ActivationWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
// 一张简单的控件布局表(坐标以 96 DPI 为基准,创建时统一按 DPI 缩放)
struct CtrlDef {
LPCWSTR cls;
LPCWSTR text;
DWORD style;
int x, y, cx, cy;
int id;
} const items[] = {
{ L"STATIC", L"机器码(Machine Code):",
WS_CHILD | WS_VISIBLE | SS_LEFT, 15, 12, 350, 18, 0 },
{ L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY | ES_AUTOHSCROLL, 15, 32, 350, 24, IDC_HWID_EDIT },
{ L"STATIC", L"激活码(Activation Code):",
WS_CHILD | WS_VISIBLE | SS_LEFT, 15, 68, 350, 18, 0 },
{ L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | WS_TABSTOP, 15, 88, 350, 24, IDC_ACT_EDIT },
{ L"BUTTON", L"激活",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_DEFPUSHBUTTON, 90, 128, 275, 28, IDC_ACTIVATE_BTN },
{ L"BUTTON", L"关闭",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, 15, 128, 75, 28, IDCANCEL },
};
for (int i = 0; i < ARRAYSIZE(items); ++i)
{
HWND hCtrl = CreateWindowExW(
0, items[i].cls, items[i].text, items[i].style,
DPI(items[i].x), DPI(items[i].y), DPI(items[i].cx), DPI(items[i].cy),
hWnd, (HMENU)(INT_PTR)items[i].id, g_hDll, NULL);
if (hCtrl && g_hFont)
SendMessageW(hCtrl, WM_SETFONT, (WPARAM)g_hFont, TRUE);
}
// 机器码回填到只读框(用户可选中复制)
SetDlgItemTextW(hWnd, IDC_HWID_EDIT, g_HardwareId);
return 0;
}
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_ACTIVATE_BTN: // 回车(默认按钮)也会走到这里
{
wchar_t key[2048] = {};
GetDlgItemTextW(hWnd, IDC_ACT_EDIT, key, ARRAYSIZE(key));
if (key[0] == L'\0')
{
MessageBoxW(hWnd, L"请输入激活码。", L"提示", MB_OK | MB_ICONINFORMATION);
return 0; // 留在对话框
}
// 注意:本示例针对 *UNICODE* SmartKey,生成许可证时须勾选 "UNICODE"
if (g_RegFuncs.pfWLRegNormalKeyCheck(key))
{
// 两处安装都尝试:lnk 模式下写文件可能失败,注册表可作后备(反之亦然)
BOOL okReg = g_RegFuncs.pfWLRegNormalKeyInstallToRegistry(key);
BOOL okFile = g_RegFuncs.pfWLRegNormalKeyInstallToFile(key);
if (okReg || okFile)
{
// license 已写入。当前进程注册态无法热刷新,提示后结束进程让用户重启。
MessageBoxW(hWnd, L"激活成功,请重新启动程序。",
L"成功", MB_OK | MB_ICONINFORMATION);
ExitProcess(0); // 立即退出(不跑全局析构,避免二次弹框/额外检查)
}
else
{
// 校验通过但没写进去(通常是注册表/文件写入权限不足)。
// 重启也无济于事,因此【不退出】,让用户用管理员身份重试或取消。
MessageBoxW(hWnd,
L"激活码有效,但许可证保存失败。\n"
L"请右键“以管理员身份运行”后重试。",
L"警告", MB_OK | MB_ICONWARNING);
}
}
else
{
MessageBoxW(hWnd, L"激活码无效,请检查后重试。",
L"错误", MB_OK | MB_ICONERROR); // 留在对话框重输
}
return 0;
}
case IDCANCEL: // 取消按钮 / Esc:未激活 -> 退出程序
ExitProcess(0);
}
break;
case WM_CLOSE: // 点标题栏“×”:等同取消 -> 退出程序
ExitProcess(0);
case WM_DESTROY:
// 正常情况下各出口都走 ExitProcess,不会到这里;保留以保证结构自洽,
// 若将来新增“仅关闭对话框而不退出进程”的分支,此路径可正常结束下方消息循环。
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hWnd, msg, wParam, lParam);
}
/******************************************************************************
* 创建并模态显示激活对话框(纯 API,不用 DialogBox / 资源模板)
*****************************************************************************/
static void
ShowActivationDialogW(void)
{
// 探测系统 DPI —— GetDeviceCaps 自 Win95 起可用,最稳,且与下方字体高度口径一致
HDC hdc = GetDC(NULL);
if (hdc)
{
g_dpi = GetDeviceCaps(hdc, LOGPIXELSX);
ReleaseDC(NULL, hdc);
}
// 取系统消息字体(中文系统上通常是微软雅黑),比默认 System 字体好看很多
NONCLIENTMETRICSW ncm = { sizeof(ncm) };
if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0))
g_hFont = CreateFontIndirectW(&ncm.lfMessageFont);
// 注册窗口类(容忍重复注册:回调可能被多次调用)
const wchar_t* kClass = L"WLActivationWndCls";
WNDCLASSEXW wc = { sizeof(wc) };
wc.lpfnWndProc = ActivationWndProc;
wc.hInstance = g_hDll;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); // 经典对话框灰底,static 文字天然融合
wc.hIcon = NULL;
wc.hIconSm = NULL; // 小图标也一并置空,确保任务栏等也不会显示
wc.lpszClassName = kClass;
if (!RegisterClassExW(&wc) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
{
if (g_hFont) { DeleteObject(g_hFont); g_hFont = NULL; }
return;
}
// 以客户区目标尺寸反推整窗尺寸,并在主屏居中
DWORD style = WS_POPUP | WS_CAPTION | WS_SYSMENU;
RECT rc = { 0, 0, DPI(380), DPI(200) };
AdjustWindowRect(&rc, style, FALSE);
int w = rc.right - rc.left;
int h = rc.bottom - rc.top;
int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
HWND hWnd = CreateWindowExW(
WS_EX_DLGMODALFRAME, kClass, L"软件激活",
style, x, y, w, h, NULL, NULL, g_hDll, NULL);
if (!hWnd)
{
if (g_hFont) { DeleteObject(g_hFont); g_hFont = NULL; }
return;
}
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
SetForegroundWindow(hWnd);
SetFocus(GetDlgItem(hWnd, IDC_ACT_EDIT)); // 焦点直接落在激活码输入框
// 自建模态消息循环:IsDialogMessageW 提供 Tab 切换 / 回车默认按钮 / Esc 取消。
// (用户点激活成功、取消或关闭都会 ExitProcess,此循环由系统一并终止。)
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0))
{
if (!IsDialogMessageW(hWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
if (g_hFont) { DeleteObject(g_hFont); g_hFont = NULL; }
}
/******************************************************************************
* DllMain
*****************************************************************************/
extern "C" BOOL __stdcall
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hDll = hInstance;
DisableThreadLibraryCalls(hInstance); // 插件不关心线程附加/分离,省去无谓回调
}
return TRUE;
}
/******************************************************************************
* 以下为 WinLicense SDK 规定的导出回调,签名/调用约定不可更改
*****************************************************************************/
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineInitialize(void)
{
return true; // 继续执行
}
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineFinalize(void)
{
return true; // 继续执行
}
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineProcessHardwareIdW(wchar_t* HardwareId)
{
// 硬件 ID 长度由保护壳决定,务必有界拷贝,避免栈溢出
if (HardwareId)
StringCchCopyW(g_HardwareId, ARRAYSIZE(g_HardwareId), HardwareId);
return true;
}
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineShowCustomMessageA(int MsgId, char* MsgBody)
{
UNREFERENCED_PARAMETER(MsgId);
MessageBoxA(NULL, MsgBody ? MsgBody : "", "Tips", MB_OK | MB_ICONINFORMATION);
return true; // TRUE = 消息已由插件处理,保护壳不再自行弹出
}
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineGetEncryptionKey(int ZoneId, char* pOutputEncryptionKey)
{
UNREFERENCED_PARAMETER(ZoneId);
// 说明:SDK 未提供该输出缓冲区大小,按其约定通常 >= 256 字节。
// 只要你的密钥字符串短于该容量即安全;若要用更长的密钥,请先确认 SDK 缓冲区上限。
if (pOutputEncryptionKey)
StringCchCopyA(pOutputEncryptionKey, 256, "My Encryption Key");
return true; // 继续执行
}
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineGetApplicationStatus(
int ApplicationStatus, int ExtendedStatus,
TTrialExpInfo * TrialExpInfo, TRegExpInfo * RegExpInfo)
{
UNREFERENCED_PARAMETER(TrialExpInfo);
UNREFERENCED_PARAMETER(RegExpInfo);
if (ApplicationStatus == wdcRegistered)
g_bRegistered = TRUE;
// 仅当尚未提示过才弹窗
if (!g_bTrialMsgShown)
{
const wchar_t* pMsg = NULL;
switch (ExtendedStatus)
{
case wdcLicenseExpired: pMsg = L"许可证已到期。"; break;
case wdcTrialExpired: pMsg = L"试用已到期。"; break;
case wdcInvalidLicense: pMsg = L"许可文件无效。"; break;
case wdcInvalidHardwareLicense: pMsg = L"硬件与许可不匹配。"; break;
case wdcWrongLicenseHardware: pMsg = L"许可硬件校验失败。"; break;
case wdcWrongLicenseExp: pMsg = L"许可有效期校验失败。"; break;
// 可根据需要继续添加
}
if (pMsg)
{
MessageBoxW(NULL, pMsg, L"错误", MB_OK | MB_ICONERROR);
g_bTrialMsgShown = TRUE;
}
}
// 任何非正常状态都允许弹出激活窗口
if (ExtendedStatus != wdcNoError)
g_bRegistered = FALSE;
return TRUE; // 绝不退出,留给用户激活机会
}
extern "C" __declspec(dllexport)
bool __stdcall SecureEngineDoRegistrationW(TWDCfunctionsArrayW * pArrayFunctions)
{
if (pArrayFunctions)
g_RegFuncs = *pArrayFunctions;
// 仅在未注册时弹出激活对话框
if (!g_bRegistered)
ShowActivationDialogW();
return TRUE; // 继续执行
}