Skip to content

Instantly share code, notes, and snippets.

@Aetopia
Last active January 25, 2023 04:01
Show Gist options
  • Save Aetopia/47833a43009c3d845e933d9dea4561df to your computer and use it in GitHub Desktop.
Save Aetopia/47833a43009c3d845e933d9dea4561df to your computer and use it in GitHub Desktop.
Desktop Window Manager Idle Tool

I wanted to see if there was any benefit of making Desktop Window Manager entirely idle when its running. Which includes settings its thread priorities and process priority to idle.
So feel free to test this.

You can build this project using GCC:

gcc -mwindows -s dwmidle.c -lWtsapi32 -o "DWMIdle.exe"
#include <Windows.h>
#include <stdio.h>
#include <wtsapi32.h>
#include <tlhelp32.h>
// Reference: https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership#examples
BOOL IsUserAdmin()
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if (b)
{
if (!CheckTokenMembership(NULL, AdministratorsGroup, &b))
{
b = FALSE;
}
FreeSid(AdministratorsGroup);
}
return (b);
}
int main(int argc, char *argv[])
{
if (!IsUserAdmin())
{
ShellExecute(0, "runas", argv[0], 0, 0, SW_SHOW);
return 1;
};
WTS_PROCESS_INFO *pWPI;
DWORD dwProcessCount;
DWORD pid;
HANDLE hThread, hProcess, hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
THREADENTRY32 te;
te.dwSize = sizeof(THREADENTRY32);
// Reference: https://stackoverflow.com/a/42126277/16335365
if (WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pWPI, &dwProcessCount))
for (DWORD i = 0; i < dwProcessCount; i++)
if (!strcmp(strlwr(pWPI[i].pProcessName), "dwm.exe"))
{
pid = pWPI[i].ProcessId;
break;
};
if (pWPI)
WTSFreeMemory(pWPI);
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
if (!hProcess)
return 1;
SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS);
if (hThreadSnapshot != INVALID_HANDLE_VALUE)
{
if (Thread32First(hThreadSnapshot, &te))
{
{
do
{
if (te.th32OwnerProcessID == pid)
{
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
if (!SetThreadPriority(hThread, THREAD_MODE_BACKGROUND_BEGIN))
SetThreadPriority(hThread, THREAD_PRIORITY_IDLE);
SetThreadPriorityBoost(hThread, TRUE);
CloseHandle(hThread);
};
} while (Thread32Next(hThreadSnapshot, &te));
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment