| 28 | } |
| 29 | |
| 30 | DWORD Elevate::GetProcessIdByName(std::wstring processName) |
| 31 | { |
| 32 | HANDLE hSnapshot; |
| 33 | if ((hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE) |
| 34 | { |
| 35 | throw std::runtime_error("CreateToolhelp32Snapshot failed: " + std::to_string(GetLastError())); |
| 36 | } |
| 37 | |
| 38 | DWORD pid = -1; |
| 39 | PROCESSENTRY32W pe; |
| 40 | ZeroMemory(&pe, sizeof(PROCESSENTRY32W)); |
| 41 | pe.dwSize = sizeof(PROCESSENTRY32W); |
| 42 | if (Process32FirstW(hSnapshot, &pe)) |
| 43 | { |
| 44 | while (Process32NextW(hSnapshot, &pe)) |
| 45 | { |
| 46 | if (pe.szExeFile == processName) |
| 47 | { |
| 48 | pid = pe.th32ProcessID; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | CloseHandle(hSnapshot); |
| 56 | throw std::runtime_error("Process32First failed: " + std::to_string(GetLastError())); |
| 57 | } |
| 58 | |
| 59 | if (pid == -1) |
| 60 | { |
| 61 | CloseHandle(hSnapshot); |
| 62 | throw std::runtime_error("process not found: " + std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(processName)); |
| 63 | } |
| 64 | |
| 65 | CloseHandle(hSnapshot); |
| 66 | return pid; |
| 67 | } |
| 68 | |
| 69 | void Elevate::ImpersonateSystem() |
| 70 | { |
nothing calls this directly
no outgoing calls
no test coverage detected