Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
1.2.1 WIP1
Browse files Browse the repository at this point in the history
  • Loading branch information
felikcat committed Nov 25, 2024
1 parent 57a9bc7 commit df4b846
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 203 deletions.
10 changes: 1 addition & 9 deletions W11Boost/AppxSupport.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import Common;
#include <windows.h>
#include <KnownFolders.h>
#include <shlobj.h>
#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <iostream>
#include <fstream>
#include "Common.h"

auto install_appx_support() -> int {
CURL *curl = curl_easy_init();
Expand Down
96 changes: 44 additions & 52 deletions W11Boost/Common.cc
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import Common;
#define WIN32_LEAN_AND_MEAN
#define NTDDI_VERSION NTDDI_WIN10_RS4
#include <windows.h> // Always first
#include <UserEnv.h>
#include <Shlwapi.h>
#include <fcntl.h>
#include <shellapi.h>
#include <libloaderapi.h>
#include <wchar.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <sstream>
#include <ShlObj.h>
#include <ostream>
#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'\\');
Expand All @@ -26,48 +12,48 @@ 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<wchar_t*>(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';
}
}

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')
wide_length++;

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<char *>(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) {
free(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;
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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<const BYTE *>(&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 {
Expand Down
37 changes: 0 additions & 37 deletions W11Boost/Common.cppm

This file was deleted.

48 changes: 48 additions & 0 deletions W11Boost/Common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // Always first
#include <cstdio>
#include <string>
#include <Shlwapi.h>
#include <shellapi.h>
#include <KnownFolders.h>
#include <shlobj.h>
#include <stdio.h>
#include <curl/curl.h>
#include <iostream>
#include <fstream>
#include <UserEnv.h>
#include <fcntl.h>
#include <libloaderapi.h>
#include <wchar.h>
#include <time.h>
#include <sstream>
#include <AccCtrl.h>
#include <AclAPI.h>
#include <SrRestorePtApi.h>

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;
5 changes: 2 additions & 3 deletions W11Boost/DisableSleep.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Common;
#include <windows.h> // 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);
Expand Down
10 changes: 2 additions & 8 deletions W11Boost/Edits.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import Common;
#define WIN32_LEAN_AND_MEAN
#define NTDDI_VERSION NTDDI_WIN10_RS4
#include <windows.h> // Always first
#include <combaseapi.h>
#include <stdio.h>
#include <stdlib.h>
#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.
Expand Down
6 changes: 1 addition & 5 deletions W11Boost/PrivacyMode.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import Common;
#define WIN32_LEAN_AND_MEAN
#define NTDDI_VERSION NTDDI_WIN10_RS4
#include <windows.h> // Always first
#include <combaseapi.h>
#include "Common.h"

auto install_privacy_mode() -> int {
HKEY hKey = HKEY_LOCAL_MACHINE;
Expand Down
Loading

0 comments on commit df4b846

Please sign in to comment.