Thread: How to check if own created process is still running

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    40

    How to check if own created process is still running

    I have this class which runs another program, and when the instance of that class gets deleted, I let it kill the program that it opened. Now it may have been the case where the user has closes this new program himself, in this case, the object should detect that and destroy itself. There is a method in this class which allows me to check every ~50ms. The question is, how do I check if the program I opened is still running or not?
    I tried to ask for the threadid and check if it returned a 0, but it keeps returning the "correct" threadid even when the program has been closed. Tried the same with processid.

    Code:
    void AppMenu::Run(const tstring& exe, const tstring& param)
    {
    	STARTUPINFO si;
    	PROCESS_INFORMATION pi;
    	ZeroMemory( &si, sizeof(si) );
    	si.cb = sizeof(si);
    	ZeroMemory( &pi, sizeof(pi) );
    	tstring runcmd = exe;
    	if (!param.empty())
    		runcmd += _T(" \"") + param + _T("\"");
    	tstring startpath = runcmd.substr(0,runcmd.find_last_of(_T("\\/")) + 1);
    	TCHAR *tmp;
    	size_t bufsize = runcmd.length() + 1; // +1 to include the \0
    	tmp = new TCHAR[bufsize];
    	_tcscpy_s(tmp,bufsize,runcmd.c_str());
    	if (!CreateProcess( NULL,   // No module name (use command line)
    		tmp ,       // Command line
    		NULL,           // Process handle not inheritable
    		NULL,           // Thread handle not inheritable
    		FALSE,          // Set handle inheritance to FALSE
    		0,              // No creation flags
    		NULL,           // Use parent's environment block
    		startpath.c_str(), // Use given starting directory 
    		&si,            // Pointer to STARTUPINFO structure
    		&pi )          // Pointer to PROCESS_INFORMATION structure
    		)
    	{
    		delete[] tmp;
    		return;
    	}
    	delete[] tmp;
    	m_ThreadID = pi.dwThreadId;
    	m_ProcessID = pi.dwProcessId;
    	m_hThread = pi.hThread;
    	m_hProcess = pi.hProcess;
    }
    As you can see I can use the handles and id's for checking if it exists, but now I need to know what function to use.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    if(GetExitCodeProcess(hProcess,&dwExitCode)){
        if(dwExitCode==STILL_ACTIVE) {
            //still running
        }
        else {
            //not running anymore
        }
    }
    else{
        //query failed, handle probably doesn't have the PROCESS_QUERY_INFORMATION access
    }
    Last edited by maxorator; 01-24-2009 at 11:37 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    WaitForSingleObject on the process handle will tell you if it's still running.
    Code:
    DWORD status = WaitForSingleObject(m_hProcess, 10);
    if(status == WAIT_OBJECT_0)
    {
    // process has exited
    }
    else if(status == WAIT_TIMEOUT)
    {
    // process still running
    }
    else
    {
    // error
    }

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    40
    thanks, works perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  4. Almost finished with Round Robin algorithm
    By Shinobi-wan in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2004, 03:00 PM
  5. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM