You need to get a list of running processes, and also display the full path to the executable file. When accessing some system processes, I get error # 5 ERROR_ACCESS_DENIED (Access is denied). I tried to establish privileges, but this did not lead to anything. What could be the error?
#include "stdafx.h" #include <iostream> #include <windows.h> #include <tlhelp32.h> #include <psapi.h> using namespace std; BOOL SetPrivilege( HANDLE hToken, // access token handle LPCTSTR lpszPrivilege, // name of privilege to enable/disable BOOL bEnablePrivilege // to enable or disable privilege ) { TOKEN_PRIVILEGES tp; LUID luid; if (!LookupPrivilegeValue( NULL, // lookup privilege on local system lpszPrivilege, // privilege to lookup &luid)) // receives LUID of privilege { return FALSE; } tp.PrivilegeCount = 1; tp.Privileges[0].Luid = luid; if (bEnablePrivilege) tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; else tp.Privileges[0].Attributes = 0; // Enable the privilege or disable all privileges. if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL)) { return FALSE; } if (GetLastError() == ERROR_NOT_ALL_ASSIGNED) { return FALSE; } return TRUE; } int main() { HANDLE hProcess; HANDLE hToken; HANDLE snapshot; TCHAR filename[MAX_PATH]; DWORD charsCarried = MAX_PATH; PROCESSENTRY32 process; int count = 0; snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); process.dwSize = sizeof(PROCESSENTRY32); if (snapshot != INVALID_HANDLE_VALUE) { if (Process32First(snapshot, &process)) { do { count++; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); SetPrivilege(hToken, SE_DEBUG_NAME, TRUE); hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, process.th32ProcessID); if (hProcess != NULL) { GetModuleFileNameEx(hProcess, NULL, filename, MAX_PATH); printf("%d - PID: %d, Name: %ls, CountThread: %d\n", count, process.th32ProcessID, filename, process.cntThreads); } else { printf("Err: %d\n", GetLastError()); } SetPrivilege(hToken, SE_DEBUG_NAME, FALSE); } while (Process32Next(snapshot, &process)); } } CloseHandle(hProcess); CloseHandle(snapshot); return 0; }