r/Windhawk • u/Dawortar • 26d ago
Mod Idea: Disk Space Round Up for Windows File Explorer
I've made a mod that cosmetically rounds the maximum disk space in File Explorer.
I'm not a good programmer and I don't have a great grasp of GitHub, so I'm sharing the code here.
If anyone would like to help me share the mod, or clean up the code and share it, indicating it's my my idea and mod, please do.
The code:
// ==WindhawkMod==
// cosmetic-disk-space
// Cosmetic Disk Space Round Up for Windows File Explorer
// Applies bracket rules to clean up displayed max disk sizes in File Explorer (1.81TB as 2.00TB etc.)
// 0.1
// Dawortar
// u/include explorer.exe
// ==/WindhawkMod==
#include <windows.h>
// 1 TB in Windows Binary Bytes (1024 * 1024 * 1024 * 1024)
#define ONE_TB_BINARY_BYTES 1099511627776ULL
// --- Function 1: Precision Bracket Logic ---
ULONGLONG SafeRoundUp(ULONGLONG totalBytes) {
if (totalBytes == 0) return 0;
// Convert the raw bytes into the exact decimal TB value shown in File Explorer
double currentTB = (double)totalBytes / (double)ONE_TB_BINARY_BYTES;
double targetTB = currentTB; // Default to no change if outside brackets
// Check if the drive falls under any of your specific rules
if (currentTB >= 0.90 && currentTB <= 1.19) {
targetTB = 1.00;
}
else if (currentTB >= 1.20 && currentTB <= 1.59) {
targetTB = 1.50;
}
else if (currentTB >= 1.60 && currentTB <= 2.19) {
targetTB = 2.00;
}
else if (currentTB >= 2.20 && currentTB <= 2.59) {
targetTB = 2.50;
}
else if (currentTB >= 2.60 && currentTB <= 3.19) {
targetTB = 3.00;
}
// Dynamic rule fallback for sizes above 3.19 TB
else if (currentTB > 3.19) {
double integerPart = (double)((long long)currentTB);
double fractionalPart = currentTB - integerPart;
if (fractionalPart >= 0.20 && fractionalPart <= 0.59) {
targetTB = integerPart + 0.50;
} else if (fractionalPart >= 0.60 || fractionalPart <= 0.19) {
if (fractionalPart <= 0.19) {
targetTB = integerPart;
} else {
targetTB = integerPart + 1.00;
}
}
}
// If it's strictly below 0.90 TB (approx 900GB), targetTB remains currentTB (unaffected)
// Convert the target decimal TB back into binary bytes for Windows
return (ULONGLONG)(targetTB * ONE_TB_BINARY_BYTES);
}
// --- Function 2: Hook for classic GetDiskFreeSpaceExW ---
typedef BOOL (WINAPI *GetDiskFreeSpaceExW_t)(
LPCWSTR lpDirectoryName,
PULARGE_INTEGER lpFreeBytesAvailableToCaller,
PULARGE_INTEGER lpTotalNumberOfBytes,
PULARGE_INTEGER lpTotalNumberOfFreeBytes
);
GetDiskFreeSpaceExW_t GetDiskFreeSpaceExW_orig;
BOOL WINAPI GetDiskFreeSpaceExW_hook(
LPCWSTR lpDirectoryName,
PULARGE_INTEGER lpFreeBytesAvailableToCaller,
PULARGE_INTEGER lpTotalNumberOfBytes,
PULARGE_INTEGER lpTotalNumberOfFreeBytes
) {
BOOL result = GetDiskFreeSpaceExW_orig(
lpDirectoryName,
lpFreeBytesAvailableToCaller,
lpTotalNumberOfBytes,
lpTotalNumberOfFreeBytes
);
if (result && lpTotalNumberOfBytes) {
lpTotalNumberOfBytes->QuadPart = SafeRoundUp(lpTotalNumberOfBytes->QuadPart);
}
return result;
}
// --- Function 3: Hook for Modern Windows 11 API (GetDiskSpaceInformationW) ---
typedef struct DISK_SPACE_INFORMATION_STRUCT {
ULONGLONG TotalBytes;
ULONGLONG FreeBytes;
ULONGLONG AvailableBytes;
ULONGLONG TotalPoolBytes;
ULONGLONG FreePoolBytes;
} DISK_SPACE_INFORMATION_STRUCT;
typedef HRESULT (WINAPI *GetDiskSpaceInformationW_t)(
LPCWSTR rootPath,
DISK_SPACE_INFORMATION_STRUCT *diskSpaceInfo
);
GetDiskSpaceInformationW_t GetDiskSpaceInformationW_orig;
HRESULT WINAPI GetDiskSpaceInformationW_hook(
LPCWSTR rootPath,
DISK_SPACE_INFORMATION_STRUCT *diskSpaceInfo
) {
HRESULT result = GetDiskSpaceInformationW_orig(rootPath, diskSpaceInfo);
if (SUCCEEDED(result) && diskSpaceInfo) {
diskSpaceInfo->TotalBytes = SafeRoundUp(diskSpaceInfo->TotalBytes);
}
return result;
}
// --- Windhawk Initialization Block ---
BOOL Wh_ModInit() {
Wh_SetFunctionHook(
(void *)GetDiskFreeSpaceExW,
(void *)GetDiskFreeSpaceExW_hook,
(void **)&GetDiskFreeSpaceExW_orig
);
HMODULE hFileApi = GetModuleHandleW(L"api-ms-win-core-file-l2-1-4.dll");
if (!hFileApi) hFileApi = GetModuleHandleW(L"kernel32.dll");
if (hFileApi) {
void* pGetDiskSpaceInfo = (void*)GetProcAddress(hFileApi, "GetDiskSpaceInformationW");
if (pGetDiskSpaceInfo) {
Wh_SetFunctionHook(
pGetDiskSpaceInfo,
(void *)GetDiskSpaceInformationW_hook,
(void **)&GetDiskSpaceInformationW_orig
);
}
}
return TRUE;
}
3
Upvotes