how to get pid by name in c 2b 2b

Solutions on MaxInterview for how to get pid by name in c 2b 2b by the best coders in the world

showing results for - "how to get pid by name in c 2b 2b"
Laila
27 May 2017
1#include <Windows.h>
2#include <iostream>
3#include <Tlhelp32.h>
4#include <comdef.h>
5
6/*snapshot for all running processes*/
7	HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
8	PROCESSENTRY32 pEntry;
9	/*initializing size - needed for using Process32First*/
10	pEntry.dwSize = sizeof(pEntry);
11	BOOL hRes = Process32First(hSnapShot, &pEntry);
12	/*while first process in pEntry exists*/
13	while (hRes)
14	{
15		/*create const char for string comparison*/
16		_bstr_t b(pEntry.szExeFile);
17		if (strcmp(b, "process name") == 0)
18		{
19			std::cout << (DWORD)pEntry.th32ProcessID;
20			break;
21		}
22		hRes = Process32Next(hSnapShot, &pEntry);
23	}
24}