From ab6ca5e93ab7994853bb4838c724a576484e43b5 Mon Sep 17 00:00:00 2001 From: Valentin Radu Date: Sun, 14 Nov 2021 18:29:07 +0200 Subject: [PATCH] Housekeeping --- ExplorerPatcher/symbols.c | 2 +- ExplorerPatcher/utility.c | 233 +++++++++++++++++--------------------- ExplorerPatcher/utility.h | 37 +++--- 3 files changed, 129 insertions(+), 143 deletions(-) diff --git a/ExplorerPatcher/symbols.c b/ExplorerPatcher/symbols.c index be1458a..f0ab783 100644 --- a/ExplorerPatcher/symbols.c +++ b/ExplorerPatcher/symbols.c @@ -125,7 +125,7 @@ DWORD DownloadSymbols(DownloadSymbolsParams* params) ); SHGetFolderPathA( NULL, - SPECIAL_FOLDER, + SPECIAL_FOLDER_LEGACY, NULL, SHGFP_TYPE_CURRENT, szSettingsPath diff --git a/ExplorerPatcher/utility.c b/ExplorerPatcher/utility.c index 2211807..81f9534 100644 --- a/ExplorerPatcher/utility.c +++ b/ExplorerPatcher/utility.c @@ -1,17 +1,17 @@ #include "utility.h" #pragma region "Weird stuff" -INT64 nimpl4_1(INT64 a1, DWORD* a2) +INT64 STDMETHODCALLTYPE nimpl4_1(INT64 a1, DWORD* a2) { *a2 = 1; return 0; } -INT64 nimpl4_0(INT64 a1, DWORD* a2) +INT64 STDMETHODCALLTYPE nimpl4_0(INT64 a1, DWORD* a2) { *a2 = 0; return 0; } -__int64 __fastcall nimpl2(__int64 a1, uintptr_t* a2) +__int64 STDMETHODCALLTYPE nimpl2(__int64 a1, uintptr_t* a2) { __int64 v2; // rax @@ -22,15 +22,15 @@ __int64 __fastcall nimpl2(__int64 a1, uintptr_t* a2) *a2 = v2; return 0i64; } -ULONG nimpl3() +ULONG STDMETHODCALLTYPE nimpl3() { return 1; } -HRESULT nimpl() +HRESULT STDMETHODCALLTYPE nimpl() { return E_NOTIMPL; } -HRESULT nimpl1(__int64 a1, uintptr_t* a2, uintptr_t* a3) +HRESULT STDMETHODCALLTYPE nimpl1(__int64 a1, uintptr_t* a2, uintptr_t* a3) { __int64 v4 = a1; // rcx @@ -40,7 +40,7 @@ HRESULT nimpl1(__int64 a1, uintptr_t* a2, uintptr_t* a3) *a3 = v4; return S_OK; } -HRESULT nimpl1_2(__int64 a1, uintptr_t* a2, uintptr_t* a3) +HRESULT STDMETHODCALLTYPE nimpl1_2(__int64 a1, uintptr_t* a2, uintptr_t* a3) { __int64 v4 = a1 - sizeof(__int64); // rcx @@ -50,7 +50,7 @@ HRESULT nimpl1_2(__int64 a1, uintptr_t* a2, uintptr_t* a3) *a3 = v4; return S_OK; } -HRESULT nimpl1_3(__int64 a1, uintptr_t* a2, uintptr_t* a3) +HRESULT STDMETHODCALLTYPE nimpl1_3(__int64 a1, uintptr_t* a2, uintptr_t* a3) { __int64 v4 = a1 - 2 * sizeof(__int64); // rcx @@ -60,7 +60,7 @@ HRESULT nimpl1_3(__int64 a1, uintptr_t* a2, uintptr_t* a3) *a3 = v4; return S_OK; } -__int64 nimpl4(__int64 a1, __int64 a2, __int64 a3, BYTE* a4) +__int64 STDMETHODCALLTYPE nimpl4(__int64 a1, __int64 a2, __int64 a3, BYTE* a4) { *a4 = 0; return 0i64; @@ -270,123 +270,6 @@ __declspec(dllexport) CALLBACK ZZRestartExplorer(HWND hWnd, HINSTANCE hInstance, FinishExplorerRestart(); } -POINT GetDefaultWinXPosition(BOOL bUseRcWork, BOOL* lpBottom, BOOL* lpRight, BOOL bAdjust) -{ - if (lpBottom) *lpBottom = FALSE; - if (lpRight) *lpRight = FALSE; - POINT point; - point.x = 0; - point.y = 0; - POINT ptCursor; - GetCursorPos(&ptCursor); - MONITORINFO mi; - mi.cbSize = sizeof(MONITORINFO); - HWND hWnd = GetMonitorInfoFromPointForTaskbarFlyoutActivation( - ptCursor, - MONITOR_DEFAULTTOPRIMARY, - &mi - ); - if (hWnd) - { - RECT rc; - GetWindowRect(hWnd, &rc); - if (rc.left - mi.rcMonitor.left <= 0) - { - if (bUseRcWork) - { - point.x = mi.rcWork.left; - } - else - { - point.x = mi.rcMonitor.left; - } - if (bAdjust) - { - point.x++; - } - if (rc.top - mi.rcMonitor.top <= 0) - { - if (bUseRcWork) - { - point.y = mi.rcWork.top; - } - else - { - point.y = mi.rcMonitor.top; - } - if (bAdjust) - { - point.y++; - } - } - else - { - if (lpBottom) *lpBottom = TRUE; - if (bUseRcWork) - { - point.y = mi.rcWork.bottom; - } - else - { - point.y = mi.rcMonitor.bottom; - } - if (bAdjust) - { - point.y--; - } - } - } - else - { - if (lpRight) *lpRight = TRUE; - if (bUseRcWork) - { - point.x = mi.rcWork.right; - } - else - { - point.x = mi.rcMonitor.right; - } - if (bAdjust) - { - point.x--; - } - if (rc.top - mi.rcMonitor.top <= 0) - { - if (bUseRcWork) - { - point.y = mi.rcWork.top; - } - else - { - point.y = mi.rcMonitor.top; - } - if (bAdjust) - { - point.y++; - } - } - else - { - if (lpBottom) *lpBottom = TRUE; - if (bUseRcWork) - { - point.y = mi.rcWork.bottom; - } - else - { - point.y = mi.rcMonitor.bottom; - } - if (bAdjust) - { - point.y--; - } - } - } - } - return point; -} - void* ReadFromFile(wchar_t* wszFileName, DWORD* dwSize) { void* ok = NULL; @@ -414,7 +297,7 @@ void* ReadFromFile(wchar_t* wszFileName, DWORD* dwSize) return ok; } -int ComputeFileHash(LPCWSTR filename, LPCWSTR hash, DWORD dwHash) +int ComputeFileHash(LPCWSTR filename, LPCSTR hash, DWORD dwHash) { DWORD dwStatus = 0; BOOL bResult = FALSE; @@ -510,7 +393,7 @@ int ComputeFileHash(LPCWSTR filename, LPCWSTR hash, DWORD dwHash) { for (DWORD i = 0; i < cbHash; i++) { - swprintf_s(hash + (i * 2), dwHash, L"%c%c", rgbDigits[rgbHash[i] >> 4], rgbDigits[rgbHash[i] & 0xf]); + sprintf_s(hash + (i * 2), 3, "%c%c", rgbDigits[rgbHash[i] >> 4], rgbDigits[rgbHash[i] & 0xf]); } } else @@ -524,4 +407,96 @@ int ComputeFileHash(LPCWSTR filename, LPCWSTR hash, DWORD dwHash) free(rgbFile); return dwStatus; -} \ No newline at end of file +} + +void LaunchPropertiesGUI(HMODULE hModule) +{ + //CreateThread(0, 0, ZZGUI, 0, 0, 0); + wchar_t wszPath[MAX_PATH * 2]; + ZeroMemory( + wszPath, + (MAX_PATH * 2) * sizeof(wchar_t) + ); + wszPath[0] = '\"'; + GetSystemDirectoryW( + wszPath + 1, + MAX_PATH + ); + wcscat_s( + wszPath, + MAX_PATH * 2, + L"\\rundll32.exe\" \"" + ); + GetModuleFileNameW( + hModule, + wszPath + wcslen(wszPath), + MAX_PATH + ); + wcscat_s( + wszPath, + MAX_PATH * 2, + L"\",ZZGUI" + ); + wprintf(L"Launching : %s\n", wszPath); + STARTUPINFO si; + ZeroMemory(&si, sizeof(STARTUPINFO)); + si.cb = sizeof(si); + PROCESS_INFORMATION pi; + ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); + if (CreateProcessW( + NULL, + wszPath, + NULL, + NULL, + TRUE, + CREATE_UNICODE_ENVIRONMENT, + NULL, + NULL, + &si, + &pi + )) + { + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + } +} + + +BOOL SystemShutdown(BOOL reboot) +{ + HANDLE hToken; + TOKEN_PRIVILEGES tkp; + + // Get a token for this process. + + if (!OpenProcessToken(GetCurrentProcess(), + TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) + return(FALSE); + + // Get the LUID for the shutdown privilege. + + LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, + &tkp.Privileges[0].Luid); + + tkp.PrivilegeCount = 1; // one privilege to set + tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; + + // Get the shutdown privilege for this process. + + AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, + (PTOKEN_PRIVILEGES)NULL, 0); + + if (GetLastError() != ERROR_SUCCESS) + return FALSE; + + // Shut down the system and force all applications to close. + + if (!ExitWindowsEx((reboot ? EWX_REBOOT : EWX_SHUTDOWN) | EWX_FORCE, + SHTDN_REASON_MAJOR_OPERATINGSYSTEM | + SHTDN_REASON_MINOR_UPGRADE | + SHTDN_REASON_FLAG_PLANNED)) + return FALSE; + + //shutdown was successful + return TRUE; +} diff --git a/ExplorerPatcher/utility.h b/ExplorerPatcher/utility.h index c775f5f..c4fdfab 100644 --- a/ExplorerPatcher/utility.h +++ b/ExplorerPatcher/utility.h @@ -16,11 +16,18 @@ #define _LIBVALINET_INCLUDE_UNIVERSAL #include #include "queryversion.h" +#pragma comment(lib, "Psapi.lib") #define APPID L"Microsoft.Windows.Explorer" #define REGPATH "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ExplorerPatcher" -#define SPECIAL_FOLDER CSIDL_APPDATA -#define APP_RELATIVE_PATH "\\ExplorerPatcher" +#define SPECIAL_FOLDER CSIDL_PROGRAM_FILES +#define SPECIAL_FOLDER_LEGACY CSIDL_APPDATA +#define PRODUCT_NAME "ExplorerPatcher" +#define PRODUCT_PUBLISHER "VALINET Solutions SRL" +#define APP_RELATIVE_PATH "\\" PRODUCT_NAME +#define EP_CLSID "{D17F1E1A-5919-4427-8F89-A1A8503CA3EB}" +#define DOSMODE_OFFSET 78 +#define SETUP_UTILITY_NAME "ep_setup.exe" // This allows compiling with older Windows SDKs as well #ifndef DWMWA_USE_HOSTBACKDROPBRUSH @@ -61,15 +68,15 @@ #endif #pragma region "Weird stuff" -INT64 nimpl4_1(INT64 a1, DWORD* a2); -INT64 nimpl4_0(INT64 a1, DWORD* a2); -__int64 __fastcall nimpl2(__int64 a1, uintptr_t* a2); -ULONG nimpl3(); -HRESULT nimpl(); -HRESULT nimpl1(__int64 a1, uintptr_t* a2, uintptr_t* a3); -HRESULT nimpl1_2(__int64 a1, uintptr_t* a2, uintptr_t* a3); -HRESULT nimpl1_3(__int64 a1, uintptr_t* a2, uintptr_t* a3); -__int64 nimpl4(__int64 a1, __int64 a2, __int64 a3, BYTE* a4); +INT64 STDMETHODCALLTYPE nimpl4_1(INT64 a1, DWORD* a2); +INT64 STDMETHODCALLTYPE nimpl4_0(INT64 a1, DWORD* a2); +__int64 STDMETHODCALLTYPE nimpl2(__int64 a1, uintptr_t* a2); +ULONG STDMETHODCALLTYPE nimpl3(); +HRESULT STDMETHODCALLTYPE nimpl(); +HRESULT STDMETHODCALLTYPE nimpl1(__int64 a1, uintptr_t* a2, uintptr_t* a3); +HRESULT STDMETHODCALLTYPE nimpl1_2(__int64 a1, uintptr_t* a2, uintptr_t* a3); +HRESULT STDMETHODCALLTYPE nimpl1_3(__int64 a1, uintptr_t* a2, uintptr_t* a3); +__int64 STDMETHODCALLTYPE nimpl4(__int64 a1, __int64 a2, __int64 a3, BYTE* a4); typedef struct _IActivationFactoryAA { CONST_VTBL struct IActivationFactoryVtbl* lpVtbl; @@ -104,7 +111,7 @@ __declspec(dllexport) CALLBACK ZZLaunchExplorer(HWND hWnd, HINSTANCE hInstance, __declspec(dllexport) CALLBACK ZZLaunchExplorerDelayed(HWND hWnd, HINSTANCE hInstance, LPSTR lpszCmdLine, int nCmdShow); -POINT GetDefaultWinXPosition(BOOL bUseRcWork, BOOL* lpBottom, BOOL* lpRight, BOOL bAdjust); +__declspec(dllexport) CALLBACK ZZRestartExplorer(HWND hWnd, HINSTANCE hInstance, LPSTR lpszCmdLine, int nCmdShow); #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) @@ -213,7 +220,11 @@ static BOOL AppsShouldUseDarkMode() { return TRUE; } void* ReadFromFile(wchar_t* wszFileName, DWORD* dwSize); -int ComputeFileHash(LPCWSTR filename, LPCWSTR hash, DWORD dwHash); +int ComputeFileHash(LPCWSTR filename, LPCSTR hash, DWORD dwHash); + +void LaunchPropertiesGUI(HMODULE hModule); + +BOOL SystemShutdown(BOOL reboot); inline long long milliseconds_now() { LARGE_INTEGER s_frequency;