GetParentProcessId - Get the process ID of the parents process returns the pid of the current process parent process. used to check for illegal launchers (an attacker's launcher code spawned our process, for example) */
| 229 | returns the pid of the current process parent process. used to check for illegal launchers (an attacker's launcher code spawned our process, for example) |
| 230 | */ |
| 231 | DWORD Process::GetParentProcessId() |
| 232 | { |
| 233 | HANDLE hSnapshot = NULL; |
| 234 | PROCESSENTRY32 pe32; |
| 235 | DWORD ppid = 0, pid = GetCurrentProcessId(); |
| 236 | |
| 237 | hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 238 | __try |
| 239 | { |
| 240 | if (hSnapshot == INVALID_HANDLE_VALUE) __leave; |
| 241 | |
| 242 | ZeroMemory(&pe32, sizeof(pe32)); |
| 243 | pe32.dwSize = sizeof(pe32); |
| 244 | if (!Process32First(hSnapshot, &pe32)) __leave; |
| 245 | |
| 246 | do |
| 247 | { |
| 248 | if (pe32.th32ProcessID == pid) |
| 249 | { |
| 250 | ppid = pe32.th32ParentProcessID; |
| 251 | break; |
| 252 | } |
| 253 | } while (Process32Next(hSnapshot, &pe32)); |
| 254 | |
| 255 | } |
| 256 | __finally |
| 257 | { |
| 258 | if (hSnapshot != INVALID_HANDLE_VALUE && hSnapshot != 0) |
| 259 | CloseHandle(hSnapshot); |
| 260 | } |
| 261 | return ppid; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | GetProcessIdByName - Get first pid given a process name. |
nothing calls this directly
no outgoing calls
no test coverage detected