C++, How to determine if a Windows Process is running?

前端 未结 13 1053
清酒与你
清酒与你 2020-11-27 15:27

This is concerning Windows XP processes.

I have a process running, let\'s call it Process1. Process1 creates a new process, Process2, and saves its id.

Now,

13条回答
  •  有刺的猬
    2020-11-27 16:22

    I found this today, it is from 2003. It finds a process by name, you don't even need the pid.

    \#include windows.h
    
    \#include tlhelp32.h
    
    \#include iostream.h
    
    int FIND_PROC_BY_NAME(const char *);
    
    int main(int argc, char *argv[])
    
    {
    
    //  Check whether a process is currently running, or not
    
    char szName[100]="notepad.exe";   // Name of process to find
    
    int isRunning;
    
        isRunning=FIND_PROC_BY_NAME(szName);
    
        // Note: isRunning=0 means process not found, =1 means yes, it is found in memor
        return isRunning;
    }
    
    int FIND_PROC_BY_NAME(const char *szToFind)
    
    // Created: 12/29/2000  (RK)
    
    // Last modified: 6/16/2003  (RK)
    
    // Please report any problems or bugs to kochhar@physiology.wisc.edu
    
    // The latest version of this routine can be found at:
    
    //     http://www.neurophys.wisc.edu/ravi/software/killproc/
    
    // Check whether the process "szToFind" is currently running in memory
    
    // This works for Win/95/98/ME and also Win/NT/2000/XP
    
    // The process name is case-insensitive, i.e. "notepad.exe" and "NOTEPAD.EXE"
    
    // will both work (for szToFind)
    
    // Return codes are as follows:
    
    //   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
    
    // Change history:
    
    //  3/10/2002   - Fixed memory leak in some cases (hSnapShot and
    
    //                and hSnapShotm were not being closed sometimes)
    
    //  6/13/2003   - Removed iFound (was not being used, as pointed out
    
    //                by John Emmas)
    
    {
    
        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;
        OSVERSIONINFO osvi;
        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 );
         DWORD (WINAPI *lpfGetModuleBaseName)( HANDLE, HMODULE,
            LPTSTR, DWORD );
    
          // 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
        iLenP=strlen(szToFind);
        if(iLenP<1 || iLenP>MAX_PATH) return 632;
        for(indx=0;indx

提交回复
热议问题