Bootloader installer 2.0 (C++ port)

This commit is contained in:
Paravis committed 2026-11-07 20:41:09 +03:00
commit 288a85a87f
15 files changed
+1569

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include "util.h"
#include <windows.h>
#include <string>
class App {
public:
HINSTANCE hInst = nullptr;
HWND hwnd = nullptr;
HFONT fontTitle = nullptr;
HFONT fontSub = nullptr;
HFONT fontMin = nullptr;
HFONT fontClose = nullptr;
HFONT fontSection = nullptr;
HFONT fontButton = nullptr;
int width = 376;
int height = 208;
std::wstring disk;
std::wstring boot;
std::wstring bootice;
bool busy = false;
bool modal = false;
bool minDown = false;
bool closeDown = false;
bool dragging = false;
int dragX = 0;
int dragY = 0;
int downButton = -1;
COLORREF minBg = ColorFromHex(0x1E2A44);
COLORREF closeBg = ColorFromHex(0x1E2A44);
COLORREF buttonBg[7] = {
RGB(255, 255, 255), RGB(255, 255, 255), RGB(255, 255, 255),
RGB(255, 255, 255), RGB(255, 255, 255), RGB(255, 255, 255),
RGB(255, 255, 255)
};
bool Init(HWND window);
void CreateFonts();
void DestroyFonts();
void Paint(HDC dc);
void OnPoll();
void RunAction(int index);
void ShowMessage(const std::wstring& text,
UINT flags = MB_ICONINFORMATION | MB_OK);
void LegacyXor();
void LegacyMgr();
void LegacyG4d();
void LegacyGrub2();
void UefiMgr();
void UefiGrub2();
void UefiXor();
void UefiClean();
void Done(const std::wstring& name);
};
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="3.2.0.0" processorArchitecture="*" name="PARAVIS.CHANGEBOOTLOADER" type="win32"/>
<description>Change bootLoader</description>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
</assembly>
+33
View File
@@ -0,0 +1,33 @@
#include <windows.h>
1 ICON "sec.ico"
1 24 "app.manifest"
VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,2,0,0
PRODUCTVERSION 3,2,0,0
FILEFLAGSMASK 0x3fL
FILEFLAGS 0x0L
FILEOS VOS_NT_WINDOWS32
FILETYPE VFT_APP
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040904b0"
BEGIN
VALUE "CompanyName", "paravis.ru@2024"
VALUE "FileDescription", "CBL"
VALUE "FileVersion", "3.2.0.0"
VALUE "InternalName", "ChangeBootLoader"
VALUE "OriginalFilename", "Change_BootLoader.exe"
VALUE "ProductName", "Change bootLoader"
VALUE "ProductVersion", "3.2.0.0"
VALUE "LegalCopyright", "paravis.ru@2024"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x409, 1200
END
END
+203
View File
@@ -0,0 +1,203 @@
#include "app.h"
#include "util.h"
#include <string>
void App::ShowMessage(const std::wstring& text, UINT flags)
{
bool previous = modal;
modal = true;
MessageBoxW(hwnd, text.c_str(), L"Change BootLoader", flags);
modal = previous;
InvalidateRect(hwnd, nullptr, FALSE);
}
void App::RunAction(int index)
{
if (busy) {
return;
}
int diskNumber = GetDiskNumberForDrive(disk[0]);
DebugLog(L"RunAction index=%d disk=%s physical=%d", index, disk.c_str(), diskNumber);
if (!FileExists(boot + L"\\NOT.DEL") || diskNumber < 0) {
ShowMessage(L"Целевой диск не подтверждён:\r\n" + disk +
L"\r\nнет \\efi\\Paravis\\Boot\\NOT.DEL или не найден PhysicalDrive.");
return;
}
busy = true;
switch (index) {
case 0:
LegacyXor();
break;
case 1:
LegacyMgr();
break;
case 2:
LegacyG4d();
break;
case 3:
LegacyGrub2();
break;
case 4:
UefiMgr();
break;
case 5:
UefiGrub2();
break;
case 6:
UefiXor();
break;
default:
break;
}
busy = false;
InvalidateRect(hwnd, nullptr, FALSE);
}
void App::Done(const std::wstring& name)
{
ShowMessage(L"Загрузчик изменён: " + name);
}
void App::LegacyXor()
{
DeleteFileIfExists(disk + L"\\bootmgr");
DeleteFileIfExists(disk + L"\\grldr");
RunWait(bootice + L" /DEVICE=" + disk +
L" /mbr /restore /file=" + boot + L"\\xor\\xmbr.bin /keep_dpt /quiet");
RunWait(bootice + L" /DEVICE=" + disk +
L" /pbr /restore /file=" + boot + L"\\xor\\xpbr.bin /keep_bpb /quiet");
Done(L"XorBoot");
}
void App::LegacyMgr()
{
DeleteFileIfExists(disk + L"\\bootmgr");
DeleteFileIfExists(disk + L"\\grldr");
CopyFileOverwrite(boot + L"\\mgr\\bootmgr", disk + L"\\bootmgr");
RunWait(bootice + L" /DEVICE=" + disk + L" /mbr /install /type=nt60 /quiet");
RunWait(bootice + L" /DEVICE=" + disk + L" /pbr /install /type=bootmgr /quiet");
Done(L"Bootmgr");
}
void App::LegacyG4d()
{
DeleteFileIfExists(disk + L"\\bootmgr");
DeleteFileIfExists(disk + L"\\grldr");
CopyFileOverwrite(boot + L"\\g4d\\grldr", disk + L"\\grldr");
RunWait(bootice + L" /DEVICE=" + disk + L" /mbr /install /type=GRUB4DOS /quiet");
RunWait(bootice + L" /DEVICE=" + disk + L" /pbr /install /type=GRUB4DOS /quiet");
Done(L"Grub4Dos");
}
void App::LegacyGrub2()
{
std::wstring source = boot + L"\\loaders\\grub2-bios";
std::wstring destination = boot + L"\\grub2\\bios";
if (!FileExists(source + L"\\boot.img") || !FileExists(source + L"\\core.img")) {
ShowMessage(L"Файлы GRUB2 для Legacy BIOS не найдены:\r\n" + source);
return;
}
if (!FileExists(destination + L"\\grub.cfg")) {
ShowMessage(L"Не найден основной grub.cfg:\r\n" + destination + L"\\grub.cfg");
return;
}
int diskNumber = GetDiskNumberForDrive(disk[0]);
if (diskNumber < 0) {
ShowMessage(L"Не удалось определить номер физического диска для " + disk);
return;
}
DeleteFileIfExists(disk + L"\\bootmgr");
DeleteFileIfExists(disk + L"\\grldr");
EnsureDirectory(destination);
EnsureDirectory(destination + L"\\i386-pc");
CopyFileOverwrite(source + L"\\boot.img", destination + L"\\boot.img");
CopyFileOverwrite(source + L"\\core.img", destination + L"\\core.img");
CopyFileOverwrite(source + L"\\make-paravis-mbr.ps1",
destination + L"\\make-paravis-mbr.ps1");
std::wstring xcopy = L"\"" + GetComSpec() +
L"\" /c xcopy /E /I /Y /Q \"" + source + L"\\i386-pc\" \"" +
destination + L"\\i386-pc\" > nul 2>&1";
RunWait(xcopy);
std::wstring powershell = L"powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"" +
destination + L"\\make-paravis-mbr.ps1\" -Disk " +
std::to_wstring(diskNumber) + L" -Boot \"" + destination +
L"\\boot.img\" -Out \"" + destination + L"\\paravis-mbr-table.img\"";
int rc1 = RunWait(powershell);
if (rc1 != 0) {
ShowMessage(L"Не удалось сформировать MBR-образ (код " +
std::to_wstring(rc1) + L").");
return;
}
std::wstring mbr = L"\"" + bootice + L"\" /DEVICE=" + disk +
L" /sectors /restore /file=\"" + destination +
L"\\paravis-mbr-table.img\" /lba=0 /sectors=1 /quiet";
int rc2 = RunWait(mbr);
if (rc2 != 0) {
ShowMessage(L"Ошибка записи MBR (код " + std::to_wstring(rc2) + L").");
return;
}
std::wstring core = L"\"" + bootice + L"\" /DEVICE=" + disk +
L" /sectors /restore /file=\"" + destination +
L"\\core.img\" /lba=1 /sectors=591 /quiet";
int rc3 = RunWait(core);
if (rc3 != 0) {
ShowMessage(L"Ошибка записи core.img (код " + std::to_wstring(rc3) + L").");
return;
}
Done(L"Grub2 (Legacy)");
}
void App::UefiMgr()
{
UefiClean();
CopyFileOverwrite(boot + L"\\mgr\\UEFI\\bootx64.efi",
disk + L"\\efi\\boot\\bootx64.efi");
CopyFileOverwrite(boot + L"\\mgr\\UEFI\\bootia32.efi",
disk + L"\\efi\\boot\\bootia32.efi");
Done(L"UEFI Bootmgr");
}
void App::UefiGrub2()
{
DeleteFileIfExists(disk + L"\\efi\\boot\\bootx64.efi");
DeleteFileIfExists(disk + L"\\efi\\boot\\fbx64.efi");
DeleteFileIfExists(disk + L"\\efi\\boot\\mmx64.efi");
CopyFileOverwrite(boot + L"\\loaders\\grub2\\bootx64.efi",
disk + L"\\efi\\boot\\bootx64.efi");
CopyFileOverwrite(boot + L"\\loaders\\grub2\\fbx64.efi",
disk + L"\\efi\\boot\\fbx64.efi");
CopyFileOverwrite(boot + L"\\loaders\\grub2\\mmx64.efi",
disk + L"\\efi\\boot\\mmx64.efi");
Done(L"UEFI GRUB2");
}
void App::UefiXor()
{
UefiClean();
CopyFileOverwrite(boot + L"\\xor\\UEFI\\bootx64.efi",
disk + L"\\efi\\boot\\bootx64.efi");
CopyFileOverwrite(boot + L"\\xor\\UEFI\\bootia32.efi",
disk + L"\\efi\\boot\\bootia32.efi");
Done(L"UEFI Xorboot");
}
void App::UefiClean()
{
DeleteFileIfExists(disk + L"\\efi\\boot\\bootx64.efi");
DeleteFileIfExists(disk + L"\\efi\\boot\\bootia32.efi");
DeleteFileIfExists(disk + L"\\efi\\boot\\fbx64.efi");
DeleteFileIfExists(disk + L"\\efi\\boot\\mmx64.efi");
}
+415
View File
@@ -0,0 +1,415 @@
#include "app.h"
#include "util.h"
#include <windowsx.h>
#include <algorithm>
#include <string>
namespace {
const wchar_t* kMainClass = L"ChangeBootLoaderCpp";
const wchar_t* kWindowTitle = L"Change BootLoader";
const DWORD CLR_BG = 0xF1F4F9;
const DWORD CLR_HEAD = 0x1E2A44;
const DWORD CLR_DIM = 0x6B7280;
const DWORD CLR_BORDER = 0xBFC9D6;
const DWORD CLR_HOVER = 0xE4EEFF;
const DWORD CLR_PRESS = 0xCFE0FF;
const DWORD CLR_TEXT = 0x14202E;
const wchar_t* kButtonNames[7] = {
L"XorBoot", L"Bootmgr", L"Grub4Dos", L"Grub2",
L"Bootmgr", L"Grub2", L"Xorboot"
};
const int kButtonPos[7][4] = {
{ 14, 72, 170, 32 },
{ 192, 72, 170, 32 },
{ 14, 110, 170, 32 },
{ 192, 110, 170, 32 },
{ 14, 168, 110, 32 },
{ 132, 168, 110, 32 },
{ 250, 168, 110, 32 }
};
enum class HitKind {
None,
Header,
Title,
Sub,
Min,
Close,
Button
};
struct HitResult {
HitKind kind = HitKind::None;
int index = -1;
};
void FillRectColor(HDC dc, int x, int y, int w, int h, COLORREF color)
{
RECT rect{ x, y, x + w, y + h };
HBRUSH brush = CreateSolidBrush(color);
FillRect(dc, &rect, brush);
DeleteObject(brush);
}
void DrawTextRect(HDC dc, const std::wstring& text, HFONT font,
int x, int y, int w, int h, COLORREF color, UINT flags)
{
HGDIOBJ oldFont = SelectObject(dc, font);
int oldMode = SetBkMode(dc, TRANSPARENT);
COLORREF oldColor = SetTextColor(dc, color);
RECT rect{ x, y, x + w, y + h };
DrawTextW(dc, text.c_str(), -1, &rect, flags | DT_NOPREFIX | DT_SINGLELINE);
SetTextColor(dc, oldColor);
SetBkMode(dc, oldMode);
SelectObject(dc, oldFont);
}
HitResult HitTest(POINT point)
{
auto inside = [&point](int x, int y, int w, int h) {
return point.x >= x && point.y >= y &&
point.x < x + w && point.y < y + h;
};
if (inside(306, 2, 30, 18)) {
return { HitKind::Min, -1 };
}
if (inside(338, 2, 30, 18)) {
return { HitKind::Close, -1 };
}
if (inside(16, 2, 300, 24)) {
return { HitKind::Title, -1 };
}
if (inside(18, 27, 300, 14)) {
return { HitKind::Sub, -1 };
}
if (inside(0, 0, 376, 48)) {
return { HitKind::Header, -1 };
}
for (int i = 0; i < 7; ++i) {
int x = kButtonPos[i][0] - 1;
int y = kButtonPos[i][1] - 1;
int w = kButtonPos[i][2] + 2;
int h = kButtonPos[i][3] + 2;
if (inside(x, y, w, h)) {
return { HitKind::Button, i };
}
}
return {};
}
LRESULT CALLBACK MainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
App* app = reinterpret_cast<App*>(GetWindowLongPtrW(hwnd, GWLP_USERDATA));
switch (message) {
case WM_CREATE: {
auto* create = reinterpret_cast<CREATESTRUCTW*>(lParam);
app = reinterpret_cast<App*>(create->lpCreateParams);
SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(app));
if (app) {
app->Init(hwnd);
}
return 0;
}
case WM_ERASEBKGND:
return 1;
case WM_PAINT: {
PAINTSTRUCT paint{};
HDC dc = BeginPaint(hwnd, &paint);
if (app) {
app->Paint(dc);
}
EndPaint(hwnd, &paint);
return 0;
}
case WM_TIMER:
if (app && wParam == 1) {
app->OnPoll();
}
return 0;
case WM_CLOSE:
DebugLog(L"WM_CLOSE busy=%d", (app && app->busy) ? 1 : 0);
if (app && app->busy) {
return 0;
}
DestroyWindow(hwnd);
return 0;
case WM_DESTROY:
if (app) {
app->DestroyFonts();
}
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, message, wParam, lParam);
}
} // namespace
bool App::Init(HWND window)
{
hwnd = window;
HDC dc = GetDC(hwnd);
int dpi = GetDeviceCaps(dc, LOGPIXELSY);
ReleaseDC(hwnd, dc);
if (dpi <= 0) {
dpi = 96;
}
auto create = [dpi](int points, int weight, const wchar_t* face) -> HFONT {
// AutoIt/Windows GUI font sizing truncates fractional pixel heights;
// this matters for 8 pt fonts (10 px, not 11 px).
int height = -(points * dpi / 72);
return CreateFontW(height, 0, 0, 0, weight,
FALSE, FALSE, FALSE, DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, face);
};
fontTitle = create(15, FW_BOLD, L"Tahoma");
fontSub = create(9, FW_NORMAL, L"Tahoma");
fontMin = create(10, FW_NORMAL, L"Tahoma");
fontClose = create(9, FW_NORMAL, L"Tahoma");
fontSection = create(8, FW_BOLD, L"Tahoma");
fontButton = create(10, FW_NORMAL, L"Segoe UI Semibold");
HICON icon = LoadIconW(hInst, MAKEINTRESOURCEW(1));
if (icon) {
SendMessageW(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM>(icon));
SendMessageW(hwnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(icon));
}
return true;
}
void App::DestroyFonts()
{
for (HFONT* font : { &fontTitle, &fontSub, &fontMin, &fontClose,
&fontSection, &fontButton }) {
if (*font) {
DeleteObject(*font);
*font = nullptr;
}
}
}
void App::Paint(HDC dc)
{
RECT client{};
GetClientRect(hwnd, &client);
int w = client.right;
int h = client.bottom;
if (w <= 0 || h <= 0) {
return;
}
HDC memory = CreateCompatibleDC(dc);
HBITMAP bitmap = CreateCompatibleBitmap(dc, w, h);
HGDIOBJ oldBitmap = SelectObject(memory, bitmap);
FillRectColor(memory, 0, 0, 376, 208, ColorFromHex(CLR_BG));
FillRectColor(memory, 0, 0, 376, 48, ColorFromHex(CLR_HEAD));
DrawTextRect(memory, L"Paravis Flash", fontTitle, 16, 2, 300, 24,
RGB(255, 255, 255), DT_LEFT | DT_TOP);
DrawTextRect(memory, L"Смена загрузчика", fontSub, 18, 27, 300, 14,
ColorFromHex(0xC7D0E0), DT_LEFT | DT_TOP);
FillRectColor(memory, 306, 2, 30, 18, minBg);
DrawTextRect(memory, L"—", fontMin, 306, 2, 30, 18,
ColorFromHex(0xC7D0E0), DT_CENTER | DT_VCENTER);
FillRectColor(memory, 338, 2, 30, 18, closeBg);
DrawTextRect(memory, L"✕", fontClose, 338, 2, 30, 18,
ColorFromHex(0xC7D0E0), DT_CENTER | DT_VCENTER);
DrawTextRect(memory, L"LEGACY BIOS", fontSection, 15, 56, 200, 14,
ColorFromHex(CLR_DIM), DT_LEFT | DT_TOP);
DrawTextRect(memory, L"UEFI", fontSection, 15, 152, 200, 14,
ColorFromHex(CLR_DIM), DT_LEFT | DT_TOP);
for (int i = 0; i < 7; ++i) {
int x = kButtonPos[i][0];
int y = kButtonPos[i][1];
int bw = kButtonPos[i][2];
int bh = kButtonPos[i][3];
FillRectColor(memory, x - 1, y - 1, bw + 2, bh + 2,
ColorFromHex(CLR_BORDER));
FillRectColor(memory, x, y, bw, bh, buttonBg[i]);
DrawTextRect(memory, kButtonNames[i], fontButton, x, y, bw, bh,
ColorFromHex(CLR_TEXT), DT_CENTER | DT_VCENTER);
}
BitBlt(dc, 0, 0, w, h, memory, 0, 0, SRCCOPY);
SelectObject(memory, oldBitmap);
DeleteObject(bitmap);
DeleteDC(memory);
}
void App::OnPoll()
{
if (modal) {
return;
}
POINT screen{};
GetCursorPos(&screen);
bool leftDown = (GetAsyncKeyState(VK_LBUTTON) & 0x8000) != 0;
POINT client = screen;
ScreenToClient(hwnd, &client);
HitResult hit = HitTest(client);
COLORREF minColor = ColorFromHex(CLR_HEAD);
COLORREF closeColor = ColorFromHex(CLR_HEAD);
if (hit.kind == HitKind::Min) {
minColor = leftDown ? ColorFromHex(0x3A4A6B) : ColorFromHex(0x2C3A57);
}
if (hit.kind == HitKind::Close) {
closeColor = leftDown ? ColorFromHex(0xC0392B) : ColorFromHex(0xE04B3F);
}
minBg = minColor;
closeBg = closeColor;
if (hit.kind == HitKind::Min && leftDown) {
minDown = true;
} else if (minDown) {
minDown = false;
if (hit.kind == HitKind::Min && !leftDown) {
ShowWindow(hwnd, SW_MINIMIZE);
}
}
if (hit.kind == HitKind::Close && leftDown) {
closeDown = true;
} else if (closeDown) {
closeDown = false;
if (hit.kind == HitKind::Close && !leftDown && !busy) {
DestroyWindow(hwnd);
return;
}
}
if ((hit.kind == HitKind::Header || hit.kind == HitKind::Title ||
hit.kind == HitKind::Sub) && leftDown && !dragging) {
POINT mouse{};
RECT windowRect{};
GetCursorPos(&mouse);
GetWindowRect(hwnd, &windowRect);
dragging = true;
dragX = mouse.x - windowRect.left;
dragY = mouse.y - windowRect.top;
}
if (dragging) {
if (!leftDown) {
dragging = false;
} else {
POINT mouse{};
GetCursorPos(&mouse);
SetWindowPos(hwnd, nullptr, mouse.x - dragX, mouse.y - dragY,
0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
for (int i = 0; i < 7; ++i) {
COLORREF background = RGB(255, 255, 255);
if (hit.kind == HitKind::Button && hit.index == i) {
background = leftDown ? ColorFromHex(CLR_PRESS)
: ColorFromHex(CLR_HOVER);
}
buttonBg[i] = background;
}
if (busy) {
downButton = -1;
InvalidateRect(hwnd, nullptr, FALSE);
return;
}
if (leftDown) {
if (hit.kind == HitKind::Button) {
downButton = hit.index;
} else {
downButton = -1;
}
} else if (downButton >= 0) {
int index = downButton;
downButton = -1;
if (hit.kind == HitKind::Button && hit.index == index) {
RunAction(index);
}
}
InvalidateRect(hwnd, nullptr, FALSE);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int)
{
App app;
app.hInst = hInstance;
std::wstring exeDir = GetExeDir();
if (exeDir.size() < 2 || exeDir[1] != L':') {
MessageBoxW(nullptr, L"Не удалось определить диск установщика.",
kWindowTitle, MB_ICONERROR | MB_OK);
return 1;
}
wchar_t letter = exeDir[0];
if (letter >= L'a' && letter <= L'z') {
letter = static_cast<wchar_t>(letter - L'a' + L'A');
}
app.disk = std::wstring(1, letter) + L":";
app.boot = app.disk + L"\\efi\\Paravis\\Boot";
app.bootice = app.boot + L"\\bootice.exe";
if (!FileExists(app.bootice) || !FileExists(app.boot + L"\\NOT.DEL")) {
std::wstring text = L"Этот диск не является сборкой Paravis "
L"(нет bootice.exe или NOT.DEL):\r\n" + app.disk;
MessageBoxW(nullptr, text.c_str(), kWindowTitle, MB_ICONERROR | MB_OK);
return 0;
}
WNDCLASSEXW windowClass{};
windowClass.cbSize = sizeof(windowClass);
windowClass.lpfnWndProc = MainProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursorW(nullptr, IDC_ARROW);
windowClass.hIcon = LoadIconW(hInstance, MAKEINTRESOURCEW(1));
windowClass.hIconSm = LoadIconW(hInstance, MAKEINTRESOURCEW(1));
windowClass.lpszClassName = kMainClass;
if (!RegisterClassExW(&windowClass)) {
return 1;
}
RECT workArea{};
SystemParametersInfoW(SPI_GETWORKAREA, 0, &workArea, 0);
int workWidth = workArea.right - workArea.left;
int workHeight = workArea.bottom - workArea.top;
int x = workArea.left + (workWidth - app.width) / 2;
int y = workArea.top + (workHeight - app.height) / 2;
app.hwnd = CreateWindowExW(WS_EX_APPWINDOW, kMainClass, kWindowTitle,
WS_POPUP | WS_CLIPCHILDREN,
x, y, app.width, app.height,
nullptr, nullptr, hInstance, &app);
if (!app.hwnd) {
return 1;
}
ShowWindow(app.hwnd, SW_SHOW);
UpdateWindow(app.hwnd);
SetForegroundWindow(app.hwnd);
SetTimer(app.hwnd, 1, 40, nullptr);
MSG message{};
while (GetMessageW(&message, nullptr, 0, 0) > 0) {
TranslateMessage(&message);
DispatchMessageW(&message);
}
return static_cast<int>(message.wParam);
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

+245
View File
@@ -0,0 +1,245 @@
#include "util.h"
#include <winioctl.h>
#include <cstdarg>
#include <cstdio>
#include <iterator>
#include <vector>
std::wstring Format(const wchar_t* fmt, ...)
{
va_list args;
va_start(args, fmt);
va_list copy;
va_copy(copy, args);
int needed = _vscwprintf(fmt, copy);
va_end(copy);
if (needed < 0) {
va_end(args);
return std::wstring();
}
std::wstring result(static_cast<size_t>(needed), L'\0');
if (needed > 0) {
_vsnwprintf_s(result.data(), result.size() + 1, _TRUNCATE, fmt, args);
}
va_end(args);
return result;
}
std::wstring GetExeDir()
{
wchar_t buffer[MAX_PATH * 4] = {};
DWORD length = GetModuleFileNameW(nullptr, buffer, static_cast<DWORD>(std::size(buffer)));
if (length == 0) {
return std::wstring();
}
std::wstring path(buffer, length);
size_t slash = path.find_last_of(L"\\/");
if (slash != std::wstring::npos) {
path.resize(slash);
}
return path;
}
std::wstring GetComSpec()
{
wchar_t buffer[MAX_PATH * 2] = {};
DWORD length = GetEnvironmentVariableW(L"ComSpec", buffer,
static_cast<DWORD>(std::size(buffer)));
if (length > 0 && length < std::size(buffer)) {
return std::wstring(buffer, length);
}
return L"C:\\Windows\\System32\\cmd.exe";
}
bool FileExists(const std::wstring& path)
{
DWORD attributes = GetFileAttributesW(path.c_str());
return attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
}
void DeleteFileIfExists(const std::wstring& path)
{
DeleteFileW(path.c_str());
}
bool CopyFileOverwrite(const std::wstring& source, const std::wstring& destination)
{
return CopyFileW(source.c_str(), destination.c_str(), FALSE) != FALSE;
}
bool EnsureDirectory(const std::wstring& path)
{
if (path.empty()) {
return false;
}
std::wstring current;
size_t start = 0;
if (path.size() >= 3 && path[1] == L':' && (path[2] == L'\\' || path[2] == L'/')) {
current = path.substr(0, 3);
start = 3;
} else if (path.size() >= 2 && path[0] == L'\\' && path[1] == L'\\') {
current = path.substr(0, 2);
start = 2;
}
while (start <= path.size()) {
size_t slash = path.find_first_of(L"\\/", start);
if (slash == std::wstring::npos) {
current += path.substr(start);
break;
}
current += path.substr(start, slash - start);
if (!current.empty()) {
CreateDirectoryW(current.c_str(), nullptr);
}
current.push_back(L'\\');
start = slash + 1;
}
if (!current.empty()) {
CreateDirectoryW(current.c_str(), nullptr);
}
DWORD attributes = GetFileAttributesW(path.c_str());
return attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
void PumpMessages()
{
MSG message{};
while (PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE)) {
if (message.message == WM_QUIT) {
PostQuitMessage(static_cast<int>(message.wParam));
continue;
}
TranslateMessage(&message);
DispatchMessageW(&message);
}
}
int RunWait(const std::wstring& commandLine)
{
STARTUPINFOW startup{};
startup.cb = sizeof(startup);
startup.dwFlags = STARTF_USESHOWWINDOW;
startup.wShowWindow = SW_HIDE;
PROCESS_INFORMATION process{};
std::vector<wchar_t> buffer(commandLine.begin(), commandLine.end());
buffer.push_back(L'\0');
if (!CreateProcessW(nullptr, buffer.data(), nullptr, nullptr, FALSE,
CREATE_NO_WINDOW, nullptr, nullptr, &startup, &process)) {
return -1;
}
CloseHandle(process.hThread);
for (;;) {
DWORD result = MsgWaitForMultipleObjects(1, &process.hProcess, FALSE,
30, QS_ALLINPUT);
if (result == WAIT_OBJECT_0) {
break;
}
PumpMessages();
}
DWORD exitCode = 1;
GetExitCodeProcess(process.hProcess, &exitCode);
CloseHandle(process.hProcess);
return static_cast<int>(exitCode);
}
int GetDiskNumberForDrive(wchar_t letter)
{
std::wstring path = L"\\\\.\\";
path.push_back(letter);
path.push_back(L':');
HANDLE handle = CreateFileW(path.c_str(), 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
handle = CreateFileW(path.c_str(), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_EXISTING, 0, nullptr);
}
if (handle == INVALID_HANDLE_VALUE) {
return -1;
}
std::vector<BYTE> buffer(sizeof(VOLUME_DISK_EXTENTS) + 16 * sizeof(DISK_EXTENT));
DWORD returned = 0;
bool ok = DeviceIoControl(handle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
nullptr, 0, buffer.data(),
static_cast<DWORD>(buffer.size()), &returned,
nullptr) != FALSE;
CloseHandle(handle);
if (!ok || returned < sizeof(VOLUME_DISK_EXTENTS)) {
return -1;
}
auto* extents = reinterpret_cast<VOLUME_DISK_EXTENTS*>(buffer.data());
if (extents->NumberOfDiskExtents == 0) {
return -1;
}
return static_cast<int>(extents->Extents[0].DiskNumber);
}
COLORREF ColorFromHex(DWORD rgb)
{
return RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF);
}
void DebugLog(const wchar_t* fmt, ...)
{
wchar_t enabled[8] = {};
if (GetEnvironmentVariableW(L"PARAVIS_DEBUG_LOG", enabled,
static_cast<DWORD>(std::size(enabled))) == 0) {
return;
}
va_list args;
va_start(args, fmt);
wchar_t message[4096] = {};
_vsnwprintf_s(message, std::size(message), _TRUNCATE, fmt, args);
va_end(args);
SYSTEMTIME st{};
GetLocalTime(&st);
std::wstring line = Format(L"%02d:%02d:%02d.%03d [%lu] %s\r\n",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds,
GetCurrentThreadId(), message);
wchar_t temp[MAX_PATH] = {};
DWORD length = GetTempPathW(MAX_PATH, temp);
std::wstring path;
if (length > 0) {
path.assign(temp, length);
}
path += L"cbl_cpp_debug.log";
int needed = WideCharToMultiByte(CP_UTF8, 0, line.c_str(),
static_cast<int>(line.size()), nullptr, 0,
nullptr, nullptr);
if (needed > 0) {
std::string bytes(static_cast<size_t>(needed), '\0');
WideCharToMultiByte(CP_UTF8, 0, line.c_str(),
static_cast<int>(line.size()), bytes.data(), needed,
nullptr, nullptr);
HANDLE file = CreateFileW(path.c_str(), FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (file != INVALID_HANDLE_VALUE) {
DWORD written = 0;
WriteFile(file, bytes.data(), static_cast<DWORD>(bytes.size()),
&written, nullptr);
CloseHandle(file);
}
}
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <windows.h>
#include <string>
std::wstring Format(const wchar_t* fmt, ...);
std::wstring GetExeDir();
std::wstring GetComSpec();
bool FileExists(const std::wstring& path);
void DeleteFileIfExists(const std::wstring& path);
bool CopyFileOverwrite(const std::wstring& source, const std::wstring& destination);
bool EnsureDirectory(const std::wstring& path);
int RunWait(const std::wstring& commandLine);
int GetDiskNumberForDrive(wchar_t letter);
void PumpMessages();
COLORREF ColorFromHex(DWORD rgb);
void DebugLog(const wchar_t* fmt, ...);