4

Is there any function in psapi or windows.h to get desired process' is running via only the process name (e.g : "chrome.exe") without getting all processes.

Edit:

If any one requires to get the desired process information via running through the list of all processes I can paste my code here. it works on a xp-machine and compiled with vs 2008.

I have found a solution for my question, too ! But according to the msdn the function runs already through the processes and checks the name without the extension. Shortly it searchs for "chrome" and returns the list of chrome.*

This function has a nice advantage it returns the process in a list, it might be an exe may run with may instances. Disadvantage CLR is required, it runs slower than the psapi functions and it has extra convertion requirements such as String^ to wchar or String (which I have not tested)

icaptan
  • 1,495
  • 1
  • 16
  • 36

3 Answers3

11

the above answer works on win 8. here it is without the wstring stuff and stripping off the path

#include <tlhelp32.h>
DWORD FindProcessId(char* processName)
{
    // strip path

    char* p = strrchr(processName, '\\');
    if(p)
        processName = p+1;

    PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);

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

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

    while ( Process32Next(processesSnapshot, &processInfo) )
    {
        if ( !strcmp(processName, processInfo.szExeFile) )
        {
          CloseHandle(processesSnapshot);
          return processInfo.th32ProcessID;
        }
    }

    CloseHandle(processesSnapshot);
    return 0;
}
steveh
  • 1,352
  • 2
  • 27
  • 41
2
#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>

using namespace std;

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

int main(int argc, char* argv[])
{

  bool bAnyBrowserIsOpen = false;

  if ( FindProcessId(L"chrome.exe") || FindProcessId(L"firefox.exe") || FindProcessId(L"iexplore.exe"))
  {
     bAnyBrowserIsOpen = true;
  }

  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(processesSnapshot);
  return 0;
}
PTT
  • 526
  • 7
  • 27
1

You can use CreateToolhelp32Snapshot as given above. If you need to regularly poll for the process, save the process ID once you've found it running and then check using OpenProcess. This is many times faster, but be aware that the OS recycles PIDs, so you'll have to deal with that.

If you know something about the internals of the process (or even have control over it) there are other options such as checking for:

  • the existence of a named memory object (using CreateFileMapping)
  • the existence of a named mutex (using OpenMutex)

More details are given in the answer to this question: Fast way to check for a specific process running

Community
  • 1
  • 1
AlainD
  • 5,413
  • 6
  • 45
  • 99