StudentX
StudentX

Reputation: 2323

How to know if a process is running in Windows in C++, WinAPI?

How do I know in a windows program if a process is running if I only know the exe file name ? The process in question is TeamSpeak3 ts3client_win64.exe for 64 bit and ts3client_win32.exe for 32 bit.

I am using C++

Upvotes: 2

Views: 11113

Answers (3)

Frerich Raabe
Frerich Raabe

Reputation: 94289

Use the CreateToolhelp32Snapshot function to create a snapshot of the current process table, then use the Process32First and Process32Next functions to iterate the snapshot. You can get the name for each executable file by looking at the szExeName field of the PROCESSENTRY32 structure.

See the MSDN example for a sample of how to use these functions.

The advantage of this approach is that, unlike any EnumProcesses-based solution, it doesn't suffer from race conditions: with EnumProcesses it can happen that a process gets destroyed after you finished enumerating the processes but before you got around to opening the process (or reading our the process executable name).

Upvotes: 5

Billy ONeal
Billy ONeal

Reputation: 106530

Windows NT has several APIs for enumerating processes.

  1. EnumProcesses
  2. ToolHelp
  3. NtQuerySystemInformation (discouraged)
  4. WMI's Win32_Process (works remotely)

Upvotes: 2

jamesdlin
jamesdlin

Reputation: 89926

You can use a combination of EnumProcesses, OpenProcess, and GetModuleFileNameEx (or alternatively, QueryFullProcessImageName for Vista or later). MSDN even has an example.

Upvotes: 1

Related Questions