int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HANDLE hProcess; HANDLE hToken; TOKEN_PRIVILEGES tkp; /* Initialize global variables */ hInst = hInstance; /* Change our priority class to HIGH */ hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()); SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS); CloseHandle(hProcess); /* Now let's get the SE_DEBUG_NAME privilege * so that we can debug processes */ /* Get a token for this process. */ if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { static const WCHAR SeDebugPrivilegeW[] = {'S','e','D','e','b','u','g','P','r','i','v','i','l','e','g','e',0}; /* Get the LUID for the debug privilege. */ LookupPrivilegeValueW(NULL, SeDebugPrivilegeW, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; /* one privilege to set */ tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; /* Get the debug privilege for this process. */ AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0); } /* Load our settings from the registry */ LoadSettings(); /* Initialize perf data */ if (!PerfDataInitialize()) { return -1; } DialogBoxW(hInst, (LPWSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc); /* Save our settings to the registry */ SaveSettings(); PerfDataUninitialize(); return 0; }
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { HANDLE hProcess; HANDLE hToken; TOKEN_PRIVILEGES tkp; HANDLE hMutex; /* check wether we're already running or not */ hMutex = CreateMutexW(NULL, TRUE, L"taskmgrros"); if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS) { /* Restore existing taskmanager and bring window to front */ /* Relies on the fact that the application title string and window title are the same */ HWND hTaskMgr; TCHAR szTaskmgr[128]; LoadString(hInst, IDS_APP_TITLE, szTaskmgr, sizeof(szTaskmgr)/sizeof(TCHAR)); hTaskMgr = FindWindow(NULL, szTaskmgr); if (hTaskMgr != NULL) { SendMessage(hTaskMgr, WM_SYSCOMMAND, SC_RESTORE, 0); SetForegroundWindow(hTaskMgr); } CloseHandle(hMutex); return 0; } else if (!hMutex) { return 1; } /* Initialize global variables */ hInst = hInstance; /* Change our priority class to HIGH */ hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId()); SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS); CloseHandle(hProcess); /* Now lets get the SE_DEBUG_NAME privilege * so that we can debug processes */ /* Get a token for this process. */ if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { /* Get the LUID for the debug privilege. */ if (LookupPrivilegeValueW(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid)) { tkp.PrivilegeCount = 1; /* one privilege to set */ tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; /* Get the debug privilege for this process. */ AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); } CloseHandle(hToken); } /* Load our settings from the registry */ LoadSettings(); /* Initialize perf data */ if (!PerfDataInitialize()) { return -1; } /* * Set our shutdown parameters: we want to shutdown the very last, * without displaying any end task dialog if needed. */ SetProcessShutdownParameters(1, SHUTDOWN_NORETRY); DialogBoxW(hInst, (LPCWSTR)IDD_TASKMGR_DIALOG, NULL, TaskManagerWndProc); /* Save our settings to the registry */ SaveSettings(); PerfDataUninitialize(); CloseHandle(hMutex); if (hWindowMenu) DestroyMenu(hWindowMenu); return 0; }