Check if process is running on XP or Windows 7 (single API)

Posted on 2013-10-16
Medium Priority
Last Modified: 2013-10-16
Hi Experts,

I'm looking for a win32 call (for C++ apps) that can check if certain processes are running by name.  I don't want to have to do conditional compilation if possible, based on library that needs to be present for the operating system.  Is there a single API that will work for both XP and Windows 7 to do this?

Question by:thready
LVL 35

Accepted Solution

sarabande earned 2000 total points
ID: 39577120
the api to get the names of running processes is

- EnumProcesses
- OpenProcess
- EnumProcessModules
- GetModulBaseName

the EnumProcesses would call a callback function you provided for each process running.
you would get an ID that could be used by OpenProcess with mode PROCESS_QUERY_INFORMATION. then call EnumProcessModules to get a handle to the  module of the process and GetModulBaseName to get the process name.

see http://msdn.microsoft.com/en-us/library/windows/desktop/ms682623(v=vs.85).aspx for a sample program.


Author Closing Comment

ID: 39577145
Yep, looks good.  Although I haven't check this much, this looks pretty good to load the correct library depending on the operating system:

//   0   = Process was not found
//   1   = Process was found
//   605 = Unable to search for process
//   606 = Unable to identify system type
//   607 = Unsupported OS
//   632 = Process name is invalid
int CProcessLauncher::FindProcByName(string sProcessName)
      BOOL bResult,bResultm;
      DWORD aiPID[1000],iCb=1000,iNumProc,iV2000=0;
      DWORD iCbneeded,i;
      char szName[MAX_PATH],szToFindUpper[MAX_PATH];
      HANDLE hProc,hSnapShot,hSnapShotm;
      HINSTANCE hInstLib;
      int iLen,iLenP,indx;
      HMODULE hMod;
      PROCESSENTRY32 procentry;      
      MODULEENTRY32 modentry;

      // PSAPI Function Pointers.
      BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
      BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *, DWORD, LPDWORD );

      // ToolHelp Function Pointers.
      HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
      BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
      BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
      BOOL (WINAPI *lpfModule32First)(HANDLE,LPMODULEENTRY32) ;
      BOOL (WINAPI *lpfModule32Next)(HANDLE,LPMODULEENTRY32) ;

      // Transfer Process name into "szToFindUpper" and
      // convert it to upper case
      if(iLenP<1 || iLenP>MAX_PATH) return 632;

      // First check what version of Windows we're in
      osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
      if(!bResult)     // Unable to identify system version
            return 606;

      // At Present we only support Win/NT/2000 or Win/9x/ME
      if((osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) &&
            (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS))
            return 607;

            // Win/NT or 2000 or XP

            // Load library and get the procedures explicitly. We do
            // this so that we don't have to worry about modules using
            // this code failing to load under Windows 95, because
            // it can't resolve references to the PSAPI.DLL.
            hInstLib = LoadLibraryA("PSAPI.DLL");
            if(hInstLib == NULL)
                  return 605;

            // Get procedure addresses.
            lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
                  GetProcAddress( hInstLib, "EnumProcesses" ) ;
            lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
                  DWORD, LPDWORD)) GetProcAddress( hInstLib,
                  "EnumProcessModules" ) ;
            lpfGetModuleBaseName =(DWORD (WINAPI *)(HANDLE, HMODULE,
                  LPTSTR, DWORD )) GetProcAddress( hInstLib,
                  "GetModuleBaseNameA" ) ;

            if( lpfEnumProcesses == NULL ||
                  lpfEnumProcessModules == NULL ||
                  lpfGetModuleBaseName == NULL)
                  return 605;

                  // Unable to get process list, EnumProcesses failed
                  return 605;

            // How many processes are there?

            // Get and match the name of each process
                  // Get the (module) name for this process

                  // First, get a handle to the process
                  // Now, get the process name
                        if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod),&iCbneeded) )
                  // Match regardless of lower or upper case
                        // Process found
                        return 1;

            // Win/95 or 98 or ME

            hInstLib = LoadLibraryA("Kernel32.DLL");
            if( hInstLib == NULL )
                  return FALSE ;

            // Get procedure addresses.
            // We are linking to these functions of Kernel32
            // explicitly, because otherwise a module using
            // this code would fail to load under Windows NT,
            // which does not have the Toolhelp32
            // functions in the Kernel 32.
                  (HANDLE(WINAPI *)(DWORD,DWORD))
                  GetProcAddress( hInstLib,
                  "CreateToolhelp32Snapshot" ) ;
                  GetProcAddress( hInstLib, "Process32First" ) ;
                  GetProcAddress( hInstLib, "Process32Next" ) ;
                  (BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
                  GetProcAddress( hInstLib, "Module32First" ) ;
                  (BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
                  GetProcAddress( hInstLib, "Module32Next" ) ;
            if( lpfProcess32Next == NULL ||
                  lpfProcess32First == NULL ||
                  lpfModule32Next == NULL ||
                  lpfModule32First == NULL ||
                  lpfCreateToolhelp32Snapshot == NULL )
                  return 605;

            // The Process32.. and Module32.. routines return names in all uppercase

            // Get a handle to a Toolhelp snapshot of all the systems processes.

            hSnapShot = lpfCreateToolhelp32Snapshot(
                  TH32CS_SNAPPROCESS, 0 ) ;
            if( hSnapShot == INVALID_HANDLE_VALUE )
                  return 605;

            // Get the first process' information.
            procentry.dwSize = sizeof(PROCESSENTRY32);

            // While there are processes, keep looping and checking.
                  // Get a handle to a Toolhelp snapshot of this process.
                  hSnapShotm = lpfCreateToolhelp32Snapshot(
                        TH32CS_SNAPMODULE, procentry.th32ProcessID) ;
                  if( hSnapShotm == INVALID_HANDLE_VALUE )
                        return 605;
                  // Get the module list for this process

                  // While there are modules, keep looping and checking
                              // Process found
                              return 1;
                        {  // Look for next modules for this process

                  //Keep looking
                  procentry.dwSize = sizeof(PROCESSENTRY32);
                  bResult = lpfProcess32Next(hSnapShot,&procentry);
      return 0;


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question