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}