From df4b84677be2c8b00c69ecd9b973e3c57e337a4c Mon Sep 17 00:00:00 2001 From: felikcat <29991266+felikcat@users.noreply.github.com> Date: Sun, 24 Nov 2024 21:09:48 -0800 Subject: [PATCH] 1.2.1 WIP1 --- W11Boost/AppxSupport.cc | 10 +- W11Boost/Common.cc | 96 +++++++++---------- W11Boost/Common.cppm | 37 ------- W11Boost/Common.h | 48 ++++++++++ W11Boost/DisableSleep.cpp | 5 +- W11Boost/Edits.cc | 10 +- W11Boost/PrivacyMode.cc | 6 +- W11Boost/RestorePoint.cc | 29 +++--- W11Boost/W11Boost.cc | 154 ++++++++++++++++-------------- W11Boost/W11Boost.vcxproj | 10 +- W11Boost/W11Boost.vcxproj.filters | 2 +- 11 files changed, 204 insertions(+), 203 deletions(-) delete mode 100644 W11Boost/Common.cppm create mode 100644 W11Boost/Common.h diff --git a/W11Boost/AppxSupport.cc b/W11Boost/AppxSupport.cc index d4447ef..dac4cc8 100644 --- a/W11Boost/AppxSupport.cc +++ b/W11Boost/AppxSupport.cc @@ -1,12 +1,4 @@ -import Common; -#include -#include -#include -#include -#include -#include -#include -#include +#include "Common.h" auto install_appx_support() -> int { CURL *curl = curl_easy_init(); diff --git a/W11Boost/Common.cc b/W11Boost/Common.cc index 7dbbbcb..928b6d4 100644 --- a/W11Boost/Common.cc +++ b/W11Boost/Common.cc @@ -1,23 +1,9 @@ -import Common; -#define WIN32_LEAN_AND_MEAN -#define NTDDI_VERSION NTDDI_WIN10_RS4 -#include // Always first -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "Common.h" + +#define MAX_PATH_DOUBLE_NULL (MAX_PATH + 2) // accounts for double-null termination auto get_log_directory() -> wchar_t* { - wchar_t currentPath[MAX_PATH + 1]; + wchar_t currentPath[MAX_PATH_DOUBLE_NULL]; GetModuleFileNameW(NULL, currentPath, MAX_PATH); wchar_t *removeExe = wcsrchr(currentPath, L'\\'); @@ -26,13 +12,13 @@ auto get_log_directory() -> wchar_t* { } const wchar_t *dirName = L"W11Boost Logs"; - wchar_t *fullPath = (wchar_t *)malloc(MAX_PATH * sizeof(wchar_t)); + wchar_t *fullPath = static_cast(malloc(MAX_PATH_DOUBLE_NULL * sizeof(wchar_t))); if (fullPath != NULL) { swprintf_s(fullPath, MAX_PATH, L"%s\\%s", currentPath, dirName); // Ensure double-null termination size_t len = wcslen(fullPath); - if (len != NULL && len + 2 < MAX_PATH) { + if (len != NULL && len + 2 < MAX_PATH_DOUBLE_NULL) { fullPath[len + 1] = L'\0'; } } @@ -40,9 +26,9 @@ auto get_log_directory() -> wchar_t* { return fullPath; } -auto wide_string_to_utf8(const wchar_t *wide_string) -> utf8_result { +auto wide_string_to_utf8(const wchar_t* wide_string) -> utf8_result { if (wide_string == NULL || *wide_string == U'\0') - return {NULL, false}; + return {NULL, true}; int wide_length = 0; while (wide_string[wide_length] != U'\0') @@ -50,11 +36,11 @@ auto wide_string_to_utf8(const wchar_t *wide_string) -> utf8_result { int size_needed = WideCharToMultiByte(CP_UTF8, 0, wide_string, wide_length, NULL, 0, NULL, NULL); if (size_needed <= 0) - return {NULL, false}; + return {NULL, true}; - char *result = (char *)malloc(size_needed + 1); + char* result = static_cast(malloc(size_needed + 2)); if (result == NULL) - return {NULL, false}; + return {NULL, true}; int converted_result = WideCharToMultiByte(CP_UTF8, 0, wide_string, wide_length, &result[0], size_needed, NULL, NULL); if (converted_result <= 0) { @@ -62,12 +48,12 @@ auto wide_string_to_utf8(const wchar_t *wide_string) -> utf8_result { return {NULL, true}; } result[size_needed] = '\0'; - return {result, false}; // if SUCCESS (0), proceed + return {result, false}; // if SUCCESS (0 / false), proceed } -auto log_registry(const wchar_t *subKey, const wchar_t *valueName, const char *typeName) -> int { +auto log_registry(const wchar_t* subKey, const wchar_t* valueName, const char* typeName) -> int { wchar_t *currentDir = get_log_directory(); - wchar_t logLocation[MAX_PATH + 1]; + wchar_t logLocation[MAX_PATH_DOUBLE_NULL]; if (currentDir == NULL) return EXIT_FAILURE; @@ -91,13 +77,13 @@ auto log_registry(const wchar_t *subKey, const wchar_t *valueName, const char *t struct tm timeInfo; localtime_s(&timeInfo, &now); - char timeString[20]; // 19 char + 1 null terminator + char timeString[21]; // 19 char + double null terminator strftime(timeString, sizeof(timeString), "%d-%m-%Y %H:%M:%S", &timeInfo); utf8_result narrow_subKey = wide_string_to_utf8(subKey); utf8_result narrow_valueName = wide_string_to_utf8(valueName); - if (!narrow_subKey.error && !narrow_valueName.error) { + if (!narrow_subKey.return_code && !narrow_valueName.return_code) { fprintf_s(logFile, "%s %s %s\\%s\n", timeString, typeName, narrow_subKey.string, narrow_valueName.string); } @@ -113,7 +99,7 @@ auto log_registry(const wchar_t *subKey, const wchar_t *valueName, const char *t } auto delete_directory_and_subfolders(LPCWSTR directory) -> long { - wchar_t dir[MAX_PATH + 1]; + wchar_t dir[MAX_PATH_DOUBLE_NULL]; SHFILEOPSTRUCTW fos; wcscpy_s(dir, MAX_PATH, directory); @@ -127,59 +113,65 @@ auto delete_directory_and_subfolders(LPCWSTR directory) -> long { return SHFileOperationW(&fos); } -auto set_dword(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName, const DWORD value) -> LSTATUS { - result = RegCreateKeyExW(hKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL); +auto set_dword(HKEY hKey, const wchar_t* subKey, const wchar_t* valueName, const DWORD value) -> LSTATUS { + HKEY hSubKey; + long_result = RegCreateKeyExW(hKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL); - if (result == ERROR_SUCCESS) - result = RegSetValueExW(hSubKey, valueName, 0, REG_DWORD, (const BYTE *)&value, sizeof(DWORD)); + if (long_result == ERROR_SUCCESS) + long_result = + RegSetValueExW(hSubKey, valueName, 0, REG_DWORD, std::bit_cast(&value), sizeof(DWORD)); const char *typeName = " - DWORD: "; log_registry(subKey, valueName, typeName); - result = RegCloseKey(hSubKey); - return result; + long_result = RegCloseKey(hSubKey); + return long_result; } -auto set_string(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName, const wchar_t *value) -> LSTATUS { - if ((result = RegCreateKeyExW(hKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL)) != +auto set_string(HKEY hKey, const wchar_t* subKey, const wchar_t* valueName, const wchar_t* value) -> LSTATUS { + HKEY hSubKey; + if ((long_result = RegCreateKeyExW(hKey, subKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, + NULL)) != ERROR_SUCCESS || - (result = RegSetValueExW(hSubKey, valueName, 0, REG_SZ, (const BYTE *)value, + (long_result = RegSetValueExW(hSubKey, valueName, 0, REG_SZ, (const BYTE *)value, (DWORD)wcslen(value) * sizeof(wchar_t))) != ERROR_SUCCESS || - (result = RegCloseKey(hSubKey)) != ERROR_SUCCESS) + (long_result = RegCloseKey(hSubKey)) != ERROR_SUCCESS) return EXCEPTION_EXECUTE_HANDLER; { const char *typeName = " - SZ: "; log_registry(subKey, valueName, typeName); } - return result; + return long_result; } auto set_environment(HKEY hKey, const wchar_t *valueName, const wchar_t *value) -> LSTATUS { + HKEY hSubKey; const wchar_t *subKey = L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"; - result = RegOpenKeyExW(hKey, subKey, 0, KEY_ALL_ACCESS, &hSubKey); + long_result = RegOpenKeyExW(hKey, subKey, 0, KEY_ALL_ACCESS, &hSubKey); - if (result == ERROR_SUCCESS) - result = RegSetValueExW(hSubKey, valueName, 0, REG_EXPAND_SZ, (const BYTE *)value, + if (long_result == ERROR_SUCCESS) + long_result = RegSetValueExW(hSubKey, valueName, 0, REG_EXPAND_SZ, (const BYTE *)value, (DWORD)(wcslen(value) * sizeof(wchar_t))); const char *typeName = " - EXPAND_SZ: "; log_registry(subKey, valueName, typeName); - result = RegCloseKey(hSubKey); - return result; + long_result = RegCloseKey(hSubKey); + return long_result; } auto remove_subkey(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName) -> LSTATUS { - result = RegOpenKeyExW(hKey, subKey, 0, KEY_WRITE, &hSubKey); + HKEY hSubKey; + long_result = RegOpenKeyExW(hKey, subKey, 0, KEY_WRITE, &hSubKey); - if (result == ERROR_SUCCESS) - result = RegDeleteValueW(hSubKey, valueName); + if (long_result == ERROR_SUCCESS) + long_result = RegDeleteValueW(hSubKey, valueName); const char *typeName = " - Removed subkey: "; log_registry(subKey, valueName, typeName); - result = RegCloseKey(hSubKey); - return result; + long_result = RegCloseKey(hSubKey); + return long_result; } auto start_command_and_wait(wchar_t *cmdLine) -> int { diff --git a/W11Boost/Common.cppm b/W11Boost/Common.cppm deleted file mode 100644 index cdb4c0d..0000000 --- a/W11Boost/Common.cppm +++ /dev/null @@ -1,37 +0,0 @@ -module; // Use global module fragments -#define WIN32_LEAN_AND_MEAN -#include // Always first -#include -#include -export module Common; - -export { - inline HKEY hKey; - inline HKEY hSubKey; - inline LONG result; - - typedef struct { - char *string; - bool error; - } utf8_result; - - auto install_privacy_mode() -> int; - auto wide_string_to_utf8(const wchar_t *wide_string) -> utf8_result; - auto log_registry(const wchar_t *subKey, const wchar_t *valueName, const char *typeName) -> int; - auto set_dword(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName, const DWORD value) -> LSTATUS; - - auto set_string(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName, const wchar_t *value) -> LSTATUS; - auto set_environment(HKEY hKey, const wchar_t *valueName, const wchar_t *value) -> LSTATUS; - auto remove_subkey(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName) -> LSTATUS; - auto start_command_and_wait(wchar_t * cmdLine) -> int; - auto append_wchar_t(const wchar_t *original, const wchar_t *append, wchar_t *result) -> bool; - auto restorepoint_prep() -> void; - auto create_restore_point() -> int; - auto main_registry_edits() -> int; - auto get_log_directory() -> wchar_t*; - auto install_appx_support() -> int; - auto delete_directory_and_subfolders(LPCWSTR directory) -> long; - auto disable_sleep() -> int; - auto write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) -> size_t; - auto get_windows_path(const GUID folderID) -> std::wstring; -} diff --git a/W11Boost/Common.h b/W11Boost/Common.h new file mode 100644 index 0000000..5b3208a --- /dev/null +++ b/W11Boost/Common.h @@ -0,0 +1,48 @@ +#define WIN32_LEAN_AND_MEAN +#include // Always first +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +inline LONG long_result; + +typedef struct { +char *string; +bool return_code; +} utf8_result; + +auto install_privacy_mode() -> int; +auto wide_string_to_utf8(const wchar_t *wide_string) -> utf8_result; +auto log_registry(const wchar_t *subKey, const wchar_t *valueName, const char *typeName) -> int; +auto set_dword(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName, const DWORD value) -> LSTATUS; + +auto set_string(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName, const wchar_t *value) -> LSTATUS; +auto set_environment(HKEY hKey, const wchar_t *valueName, const wchar_t *value) -> LSTATUS; +auto remove_subkey(HKEY hKey, const wchar_t *subKey, const wchar_t *valueName) -> LSTATUS; +auto start_command_and_wait(wchar_t * cmdLine) -> int; +auto append_wchar_t(const wchar_t *original, const wchar_t *append, wchar_t *result) -> bool; +auto restorepoint_prep() -> void; +auto create_restore_point() -> int; +auto main_registry_edits() -> int; +auto get_log_directory() -> wchar_t*; +auto install_appx_support() -> int; +auto delete_directory_and_subfolders(LPCWSTR directory) -> long; +auto disable_sleep() -> int; +auto write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) -> size_t; +auto get_windows_path(const GUID folderID) -> std::wstring; diff --git a/W11Boost/DisableSleep.cpp b/W11Boost/DisableSleep.cpp index 1c9f065..eb7351b 100644 --- a/W11Boost/DisableSleep.cpp +++ b/W11Boost/DisableSleep.cpp @@ -1,8 +1,7 @@ -import Common; -#include // Always first +#include "Common.h" int disable_sleep() { - hKey = HKEY_LOCAL_MACHINE; + HKEY hKey = HKEY_LOCAL_MACHINE; // Globally disable hibernation set_dword(hKey, LR"(SYSTEM\CurrentControlSet\Control\Power)", L"HibernateEnabledDefault", 0); diff --git a/W11Boost/Edits.cc b/W11Boost/Edits.cc index 58f2738..a9d6885 100644 --- a/W11Boost/Edits.cc +++ b/W11Boost/Edits.cc @@ -1,13 +1,7 @@ -import Common; -#define WIN32_LEAN_AND_MEAN -#define NTDDI_VERSION NTDDI_WIN10_RS4 -#include // Always first -#include -#include -#include +#include "Common.h" auto main_registry_edits() -> int { - hKey = HKEY_LOCAL_MACHINE; + HKEY hKey = HKEY_LOCAL_MACHINE; // If allowed (1): unused apps would be uninstalled with their user data left // intact, then reinstalled if launched afterwards at any point in time. diff --git a/W11Boost/PrivacyMode.cc b/W11Boost/PrivacyMode.cc index c963c81..32129b2 100644 --- a/W11Boost/PrivacyMode.cc +++ b/W11Boost/PrivacyMode.cc @@ -1,8 +1,4 @@ -import Common; -#define WIN32_LEAN_AND_MEAN -#define NTDDI_VERSION NTDDI_WIN10_RS4 -#include // Always first -#include +#include "Common.h" auto install_privacy_mode() -> int { HKEY hKey = HKEY_LOCAL_MACHINE; diff --git a/W11Boost/RestorePoint.cc b/W11Boost/RestorePoint.cc index b9540dc..2c826af 100644 --- a/W11Boost/RestorePoint.cc +++ b/W11Boost/RestorePoint.cc @@ -1,7 +1,4 @@ -import Common; -#include -#include -#include +#include "Common.h" typedef bool(WINAPI *PFN_SETRESTOREPTW)(PRESTOREPOINTINFOW, PSTATEMGRSTATUS); @@ -10,8 +7,8 @@ auto InitializeCOMSecurity() -> bool { // CoInitializeSecurity() will not accept the relative security descriptors // returned by ConvertStringSecurityDescriptorToSecurityDescriptor(). - SECURITY_DESCRIPTOR securityDesc = {0}; - EXPLICIT_ACCESS ea[5] = {{0}}; + SECURITY_DESCRIPTOR securityDesc = {}; + EXPLICIT_ACCESS ea[5] = {{}}; ACL *pAcl = NULL; unsigned __int64 rgSidBA[(SECURITY_MAX_SID_SIZE + sizeof(unsigned __int64) - 1) / sizeof(unsigned __int64)] = {0}; unsigned __int64 rgSidLS[(SECURITY_MAX_SID_SIZE + sizeof(unsigned __int64) - 1) / sizeof(unsigned __int64)] = {0}; @@ -158,19 +155,20 @@ auto InitializeCOMSecurity() -> bool { auto restorepoint_prep() -> void { HKEY hKey = HKEY_LOCAL_MACHINE; + HKEY hSubKey; - result = RegCreateKeyExW(hKey, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore", 0, NULL, + long_result = RegCreateKeyExW(hKey, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL); - if (result == ERROR_SUCCESS) { + if (long_result == ERROR_SUCCESS) { unsigned long value = 0; RegSetValueExW(hSubKey, L"SystemRestorePointCreationFrequency", 0, REG_DWORD, (const BYTE *)&value, sizeof(value)); } - result = RegCreateKeyExW(hKey, L"SOFTWARE\\Policies\\Microsoft\\Windows NT\\SystemRestore", 0, NULL, + long_result = RegCreateKeyExW(hKey, L"SOFTWARE\\Policies\\Microsoft\\Windows NT\\SystemRestore", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL); - if (result == ERROR_SUCCESS) { + if (long_result == ERROR_SUCCESS) { RegDeleteValueW(hSubKey, L"DisableConfig"); RegDeleteValueW(hSubKey, L"DisableSR"); } @@ -181,11 +179,12 @@ auto restorepoint_prep() -> void { // own auto restorepoint_prep_revert() -> void { HKEY hKey = HKEY_LOCAL_MACHINE; + HKEY hSubKey; - result = RegCreateKeyExW(hKey, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore", 0, NULL, + long_result = RegCreateKeyExW(hKey, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL); - if (result == ERROR_SUCCESS) { + if (long_result == ERROR_SUCCESS) { RegDeleteValueW(hSubKey, L"SystemRestorePointCreationFrequency"); } @@ -210,8 +209,10 @@ auto create_restore_point() -> int { if (NULL == hSrClient) return EXIT_FAILURE; - RESTOREPOINTINFOW RestorePtInfo = { - .dwEventType = BEGIN_SYSTEM_CHANGE, .dwRestorePtType = APPLICATION_INSTALL, .llSequenceNumber = 0}; + RESTOREPOINTINFOW RestorePtInfo = {.dwEventType = BEGIN_SYSTEM_CHANGE, + .dwRestorePtType = APPLICATION_INSTALL, + .llSequenceNumber = 0, + .szDescription = L"\0"}; wcscpy_s(RestorePtInfo.szDescription, _countof(RestorePtInfo.szDescription), L"Installed W11Boost"); diff --git a/W11Boost/W11Boost.cc b/W11Boost/W11Boost.cc index a5e005b..e322504 100644 --- a/W11Boost/W11Boost.cc +++ b/W11Boost/W11Boost.cc @@ -1,7 +1,5 @@ #include "Resource.h" -import Common; -#include -#include +#include "Common.h" /* Type notes: HINTERNET = void * @@ -20,23 +18,25 @@ import Common; HINSTANCE hInst; // Current instance wchar_t szTitle[MAX_LOADSTRING]; // The title bar text wchar_t szWindowClass[MAX_LOADSTRING]; // The main window class name -UINT rpCheckboxStatus, lpCheckboxStatus, msCheckboxStatus, appxCheckboxStatus, opCheckboxStatus, sleepCheckboxStatus; -int mainResult; +struct { + UINT restorePoint, localPrivacy, microsoftStore, appxSupport, disableSleep; +} checkboxStatus; +int int_result; // Forward declarations of functions included in this code module: -unsigned short MyRegisterClass(HINSTANCE hInstance); -bool InitInstance(HINSTANCE, int); -LRESULT CALLBACK WndProc(HWND, unsigned int, WPARAM, LPARAM); +auto MyRegisterClass(HINSTANCE hInstance) -> unsigned short; +auto InitInstance(HINSTANCE, int) -> bool; +auto CALLBACK WndProc(HWND, unsigned int, WPARAM, LPARAM) -> LRESULT; -int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, - _In_ int nCmdShow) { +auto APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, + _In_ int nCmdShow) -> int { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // Place code that should always be ran here: wchar_t *fullPath = get_log_directory(); if (PathFileExistsW(fullPath)) { - SHFILEOPSTRUCTW dir = {0}; + SHFILEOPSTRUCTW dir = {}; dir.wFunc = FO_DELETE; dir.pFrom = fullPath; dir.fFlags = FOF_NO_UI | FOF_NOERRORUI; @@ -75,7 +75,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance // // PURPOSE: Registers the window class. // -unsigned short MyRegisterClass(HINSTANCE hInstance) { +auto MyRegisterClass(HINSTANCE hInstance) -> unsigned short { WNDCLASSEXW wcex = { .cbSize = sizeof(WNDCLASSEX), .style = CS_HREDRAW | CS_VREDRAW, @@ -83,10 +83,12 @@ unsigned short MyRegisterClass(HINSTANCE hInstance) { .cbClsExtra = 0, .cbWndExtra = 0, .hInstance = hInstance, + .hIcon = NULL, .hCursor = LoadCursor(NULL, IDC_ARROW), .hbrBackground = (HBRUSH)(COLOR_WINDOW + 1), .lpszMenuName = MAKEINTRESOURCEW(IDC_W11BOOST), .lpszClassName = szWindowClass, + .hIconSm = NULL, }; return RegisterClassExW(&wcex); @@ -102,11 +104,11 @@ unsigned short MyRegisterClass(HINSTANCE hInstance) { // In this function, we save the instance handle in a global variable and // create and display the main program window. // -bool InitInstance(HINSTANCE hInstance, int nCmdShow) { +auto InitInstance(HINSTANCE hInstance, int nCmdShow) -> bool { hInst = hInstance; // Store instance handle in our global variable - int width = 800; - int height = 600; + int width = 640; + int height = 480; HWND hWnd = CreateWindowExW(0, szWindowClass, szTitle, WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, 0, width, height, NULL, NULL, hInstance, NULL); @@ -116,15 +118,19 @@ bool InitInstance(HINSTANCE hInstance, int nCmdShow) { } HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST); - MONITORINFO mi = {sizeof(mi)}; + MONITORINFO mi = {.cbSize = sizeof(mi), .rcMonitor = NULL, .rcWork = NULL, .dwFlags = NULL}; - // Puts W11Boost's window in the center of the current monitor if (GetMonitorInfoW(monitor, &mi)) { + UINT screen_dpi = GetDpiForWindow(hWnd); + int adjusted_width = MulDiv(width, screen_dpi, USER_DEFAULT_SCREEN_DPI); + int adjusted_height = MulDiv(height, screen_dpi, USER_DEFAULT_SCREEN_DPI); + + // Puts W11Boost's window in the center of the current monitor RECT rcWork = mi.rcWork; - int x = rcWork.left + (rcWork.right - rcWork.left - width) / 2; - int y = rcWork.top + (rcWork.bottom - rcWork.top - height) / 2; + long x = rcWork.left + (rcWork.right - rcWork.left - width) / 2; + long y = rcWork.top + (rcWork.bottom - rcWork.top - height) / 2; - SetWindowPos(hWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); + SetWindowPos(hWnd, NULL, x, y, adjusted_width, adjusted_height, SWP_NOZORDER | SWP_NOACTIVATE); } ShowWindow(hWnd, nCmdShow); @@ -133,10 +139,10 @@ bool InitInstance(HINSTANCE hInstance, int nCmdShow) { return TRUE; } -void appx_void(HWND hWnd) { - int appxResult = install_appx_support(); +auto appx_void(HWND hWnd) -> void { + int_result = install_appx_support(); - if (appxResult != 0) + if (int_result != 0) MessageBoxW(hWnd, L"Failed to install .appx/.appxbundle and WinGet support!", L"W11Boost -> Error", MB_OK); } @@ -158,9 +164,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM } font; font.size = 24; - font.dpi = 96; + font.dpi = USER_DEFAULT_SCREEN_DPI; UINT screen_dpi = GetDpiForWindow(hWnd); + int scale_font = MulDiv(font.size, screen_dpi, font.dpi); HFONT hFont = CreateFontW(scale_font, 0, 0, 0, FW_LIGHT, FALSE, FALSE, 0, ANSI_CHARSET, OUT_OUTLINE_PRECIS, @@ -183,28 +190,35 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM HWND localPrivacy, backup, store, appx, sleep; } checkbox; - common.centerWidth = rcClient.right / 2; - common.centerHeight = rcClient.bottom / 2; + int scale_factor = MulDiv(screen_dpi, 100, USER_DEFAULT_SCREEN_DPI); - common.left = rcClient.left + 4; - common.right = rcClient.right - 4; + common.left = rcClient.left + MulDiv(4, scale_factor, 100); + common.right = MulDiv(rcClient.right, scale_factor, 100); - common.top = rcClient.top + 2; - common.bottom = rcClient.bottom - 2; + common.top = rcClient.top; + common.bottom = MulDiv(rcClient.bottom, scale_factor, 100); - // button.width = common.centerWidth; // Use if I ever add "Remove W11Boost" - button.width = common.right - 4; + common.centerWidth = common.right / 2; + common.centerHeight = common.bottom / 2; + + // button.width = common.centerWidth; // Use if I add "Remove W11Boost" + button.width = common.right; button.height = (common.centerHeight * 4) / 10; // 40% - checkbox.width = rcClient.right; + checkbox.width = MulDiv(rcClient.right, scale_factor, 100); checkbox.height = (common.centerHeight * 2) / 10; // 20% struct s_button const aButton = { - .apply = CreateWindowW(L"BUTTON", L"Apply W11Boost", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_FLAT, - common.left, common.bottom - button.height, button.width, button.height, hWnd, - (HMENU)IDC_APPLY_W11BOOST, GetModuleHandle(NULL), NULL), + .width = {}, + .height = {}, + .apply = CreateWindowW(L"BUTTON", L"Apply W11Boost", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_FLAT, common.left, + common.bottom - button.height, button.width, button.height, hWnd, + (HMENU)IDC_APPLY_W11BOOST, GetModuleHandle(NULL), NULL), + }; struct s_checkbox const aCheckbox = { + .width = {}, + .height = {}, .localPrivacy = CreateWindowW(L"BUTTON", L"Reduce local data collection", WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_FLAT, common.left, common.top + (checkbox.height), checkbox.width, checkbox.height, hWnd, @@ -253,62 +267,62 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM switch (wmId) { case IDC_APPLY_W11BOOST: if (MessageBoxW(hWnd, L"Do you want to apply W11Boost?", L"W11Boost", MB_YESNO) == IDYES) { - rpCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_CREATE_RESTORE_POINT); - lpCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_LOCAL_PRIVACY); - msCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_INSTALL_MICROSOFT_STORE); - appxCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_INSTALL_APPX_SUPPORT); - sleepCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_DISABLE_SLEEP); + checkboxStatus.restorePoint = IsDlgButtonChecked(hWnd, IDC_CREATE_RESTORE_POINT); + checkboxStatus.localPrivacy = IsDlgButtonChecked(hWnd, IDC_LOCAL_PRIVACY); + checkboxStatus.microsoftStore = IsDlgButtonChecked(hWnd, IDC_INSTALL_MICROSOFT_STORE); + checkboxStatus.appxSupport = IsDlgButtonChecked(hWnd, IDC_INSTALL_APPX_SUPPORT); + checkboxStatus.disableSleep = IsDlgButtonChecked(hWnd, IDC_DISABLE_SLEEP); - if (rpCheckboxStatus) { - int srResult = create_restore_point(); + if (checkboxStatus.restorePoint) { + int_result = create_restore_point(); - if (srResult != 0) + if (int_result != 0) MessageBoxW(hWnd, L"System Restore point failed to be created!", L"W11Boost -> Error", MB_OK); } - if (lpCheckboxStatus) { - int pmResult = install_privacy_mode(); + if (checkboxStatus.localPrivacy) { + int_result = install_privacy_mode(); - if (pmResult != 0) + if (int_result != 0) MessageBoxW(hWnd, L"Failed to install 'Reduce local data collection'!", L"W11Boost -> Error", MB_OK); } - if (msCheckboxStatus) { + if (checkboxStatus.microsoftStore) { wchar_t cmdLine1[] = L"wsreset.exe -i"; - int msResult = start_command_and_wait(cmdLine1); + int_result = start_command_and_wait(cmdLine1); - if (msResult != 0) + if (int_result != 0) MessageBoxW(hWnd, L"Failed to install the Microsoft Store!", L"W11Boost -> Error", MB_OK); } - if (msCheckboxStatus != TRUE && appxCheckboxStatus) { + if (checkboxStatus.microsoftStore != TRUE && checkboxStatus.appxSupport) { if (MessageBoxW(hWnd, L"Are you certain the Microsoft Store is already installed? It is required for " L".appx/.appxbundle and WinGet support.", L"W11Boost", MB_YESNO) == IDYES) { appx_void(hWnd); } - } else if (msCheckboxStatus && appxCheckboxStatus) { + } else if (checkboxStatus.microsoftStore && checkboxStatus.appxSupport) { appx_void(hWnd); } - if (sleepCheckboxStatus) { - int sleepResult = disable_sleep(); + if (checkboxStatus.disableSleep) { + int_result = disable_sleep(); - if (sleepResult != 0) + if (int_result != 0) MessageBoxW(hWnd, L"Disabling sleep and hibernation failed!", L"W11Boost -> Error", MB_OK); } - if (appxCheckboxStatus) { - int appxResult = install_appx_support(); + if (checkboxStatus.appxSupport) { + int_result = install_appx_support(); - if (appxResult != 0) + if (int_result != 0) MessageBoxW(hWnd, L"Installing .appx support failed!", L"W11Boost -> Error", MB_OK); } - mainResult = main_registry_edits(); + int_result = main_registry_edits(); - if (mainResult == 0) { + if (int_result == 0) { MessageBoxW(hWnd, L"Complete. Manually reboot to apply all changes.", L"W11Boost", MB_OK); } else { MessageBoxW(hWnd, L"Failed to install W11Boost!", L"W11Boost -> Error", MB_OK); @@ -317,9 +331,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM break; case IDC_CREATE_RESTORE_POINT: - rpCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_CREATE_RESTORE_POINT); + checkboxStatus.restorePoint = IsDlgButtonChecked(hWnd, IDC_CREATE_RESTORE_POINT); - if (rpCheckboxStatus) { + if (checkboxStatus.restorePoint) { CheckDlgButton(hWnd, IDC_CREATE_RESTORE_POINT, BST_UNCHECKED); } else { CheckDlgButton(hWnd, IDC_CREATE_RESTORE_POINT, BST_CHECKED); @@ -327,9 +341,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM break; case IDC_LOCAL_PRIVACY: - lpCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_LOCAL_PRIVACY); + checkboxStatus.localPrivacy = IsDlgButtonChecked(hWnd, IDC_LOCAL_PRIVACY); - if (lpCheckboxStatus) { + if (checkboxStatus.localPrivacy) { CheckDlgButton(hWnd, IDC_LOCAL_PRIVACY, BST_UNCHECKED); } else { CheckDlgButton(hWnd, IDC_LOCAL_PRIVACY, BST_CHECKED); @@ -337,9 +351,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM break; case IDC_INSTALL_MICROSOFT_STORE: - msCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_INSTALL_MICROSOFT_STORE); + checkboxStatus.microsoftStore = IsDlgButtonChecked(hWnd, IDC_INSTALL_MICROSOFT_STORE); - if (msCheckboxStatus) { + if (checkboxStatus.microsoftStore) { CheckDlgButton(hWnd, IDC_INSTALL_MICROSOFT_STORE, BST_UNCHECKED); } else { CheckDlgButton(hWnd, IDC_INSTALL_MICROSOFT_STORE, BST_CHECKED); @@ -347,9 +361,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM break; case IDC_INSTALL_APPX_SUPPORT: - appxCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_INSTALL_APPX_SUPPORT); + checkboxStatus.appxSupport = IsDlgButtonChecked(hWnd, IDC_INSTALL_APPX_SUPPORT); - if (appxCheckboxStatus) { + if (checkboxStatus.appxSupport) { CheckDlgButton(hWnd, IDC_INSTALL_APPX_SUPPORT, BST_UNCHECKED); } else { CheckDlgButton(hWnd, IDC_INSTALL_APPX_SUPPORT, BST_CHECKED); @@ -357,9 +371,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM break; case IDC_DISABLE_SLEEP: - sleepCheckboxStatus = IsDlgButtonChecked(hWnd, IDC_DISABLE_SLEEP); + checkboxStatus.disableSleep = IsDlgButtonChecked(hWnd, IDC_DISABLE_SLEEP); - if (sleepCheckboxStatus) { + if (checkboxStatus.disableSleep) { CheckDlgButton(hWnd, IDC_DISABLE_SLEEP, BST_UNCHECKED); } else { CheckDlgButton(hWnd, IDC_DISABLE_SLEEP, BST_CHECKED); diff --git a/W11Boost/W11Boost.vcxproj b/W11Boost/W11Boost.vcxproj index c3d21d7..70c9481 100644 --- a/W11Boost/W11Boost.vcxproj +++ b/W11Boost/W11Boost.vcxproj @@ -48,13 +48,13 @@ - true + false true - false + true true - false + true true @@ -68,6 +68,7 @@ true stdcpplatest stdclatest + true Windows @@ -91,6 +92,7 @@ stdcpplatest stdclatest Full + true Windows @@ -114,7 +116,7 @@ - + Document diff --git a/W11Boost/W11Boost.vcxproj.filters b/W11Boost/W11Boost.vcxproj.filters index 5380911..9a538b8 100644 --- a/W11Boost/W11Boost.vcxproj.filters +++ b/W11Boost/W11Boost.vcxproj.filters @@ -33,7 +33,7 @@ Source Files - + Header Files