How to get process id

Hi,

How to get process id of any running process by its name in windows.
FindWindow() - http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx
GetWindowThreadProcessId() - http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <windows.h>

int main()
{
	std::wstring windowName;
	std::getline(std::wcin, windowName);

	HWND windowHandle = FindWindowW(NULL, windowName.c_str());
	DWORD* processID = new DWORD;
	GetWindowThreadProcessId(windowHandle, processID);

	std::wcout << L"Process ID of " << windowName.c_str() << L" is: " << *processID << std::endl;

	system("PAUSE");
}



Try to use wchar_t when dealing with the Windows API since all the ANSI functions are just wrappers which make copies of the ANSI strings to Unicode strings and recall the function.

Hi,

Thanks for ur reply..
But its not working.
I gave input to program ------------regedit
Ans is........Process ID of regedit is : 3452816845


Actual process id is ::
regedit.exe process id is.............2536.
Don't blame the API for user error.

When you execute regedit.exe, notice that the window title is "Registry Editor". You cannot search for a window titled "regedit" and expect a match if no such window exists.

Hope this helps.
As Duoas said, the functions I provided require the name of the actual window. If you were looking for how to check against "process.exe" then this should help you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <windows.h>
#include <tlhelp32.h>

DWORD FindProcessId(const std::wstring& processName);

int main()
{
	std::wstring processName;

	std::wcout << "Enter the process name: ";
	std::getline(std::wcin, processName);
	
	DWORD processID = FindProcessId(processName);

	if ( processID == 0 )
		std::wcout << "Could not find " << processName.c_str() << std::endl;
	else
		std::wcout << "Process ID is " << processID << std::endl;

	system("PAUSE");
	return 0;
}

DWORD FindProcessId(const std::wstring& processName)
{
	PROCESSENTRY32 processInfo;
	processInfo.dwSize = sizeof(processInfo);

	HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
	if ( processesSnapshot == INVALID_HANDLE_VALUE )
		return 0;

	Process32First(processesSnapshot, &processInfo);
	if ( !processName.compare(processInfo.szExeFile) )
	{
		CloseHandle(processesSnapshot);
		return processInfo.th32ProcessID;
	}

	while ( Process32Next(processesSnapshot, &processInfo) )
	{
		if ( !processName.compare(processInfo.szExeFile) )
		{
			CloseHandle(processesSnapshot);
			return processInfo.th32ProcessID;
		}
	}
	
	CloseHandle(processSnapshot);
	return 0;
}
Last edited on

Hi,

thanks...
Found this while helping someone else, hope it helps you. It's written by someone called Irwin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <psapi.h> 
#define MAX_PROCESSES 1024 

DWORD FindProcess(__in_z LPCTSTR lpcszFileName) 
{ 
  LPDWORD lpdwProcessIds; 
  LPTSTR  lpszBaseName; 
  HANDLE  hProcess; 
  DWORD   i, cdwProcesses, dwProcessId = 0; 

  lpdwProcessIds = (LPDWORD)HeapAlloc(GetProcessHeap(), 0, MAX_PROCESSES*sizeof(DWORD)); 
  if (lpdwProcessIds != NULL) 
  { 
    if (EnumProcesses(lpdwProcessIds, MAX_PROCESSES*sizeof(DWORD), &cdwProcesses)) 
    { 
      lpszBaseName = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, MAX_PATH*sizeof(TCHAR)); 
      if (lpszBaseName != NULL) 
      { 
        cdwProcesses /= sizeof(DWORD); 
        for (i = 0; i < cdwProcesses; i++) 
        { 
          hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, lpdwProcessIds[i]); 
          if (hProcess != NULL) 
          { 
            if (GetModuleBaseName(hProcess, NULL, lpszBaseName, MAX_PATH) > 0) 
            { 
              if (!lstrcmpi(lpszBaseName, lpcszFileName)) 
              { 
                dwProcessId = lpdwProcessIds[i]; 
                CloseHandle(hProcess); 
                break; 
              } 
            } 
            CloseHandle(hProcess); 
          } 
        } 
        HeapFree(GetProcessHeap(), 0, (LPVOID)lpszBaseName); 
      } 
    } 
    HeapFree(GetProcessHeap(), 0, (LPVOID)lpdwProcessIds); 
  } 
  return dwProcessId; 
}
Topic archived. No new replies allowed.