How can I kill and then restart a process in C ++?

I would like to kill and restart explorer.exe from my C ++ application, how would I do this?

0


a source to share


6 answers


Define the main window of the application (for example using FindWindow) and send it WM_QUIT.

Use SendMessageTimeout () to send it; this function allows you to specify how long you are willing to wait for the application to process it. If SendMessageTimeout () returned because it timed out, refer to TerminateProcess ().



Here's a link to the SendMessageTimeout spec: http://msdn.microsoft.com/en-us/library/ms644952(VS.85).aspx

+3


a source


You can use CreateProcess to call explorer.exe and TerminateProcess to kill it. ExitProcess , as stated above, only applies to the current process (i.e. the process from which you are calling ExitProcess).

You can also use OpenProcess to access a process that has already been created by other means.

OpenProcess

Terminateprocess

BOOL WINAPI TerminateProcess
(
  __in  HANDLE hProcess,
  __in  UINT uExitCode
);

      



CreateProcess has the following signature:

BOOL WINAPI CreateProcess(
  __in_opt     LPCTSTR lpApplicationName,
  __inout_opt  LPTSTR lpCommandLine,
  __in_opt     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  __in_opt     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in         BOOL bInheritHandles,
  __in         DWORD dwCreationFlags,
  __in_opt     LPVOID lpEnvironment,
  __in_opt     LPCTSTR lpCurrentDirectory,
  __in         LPSTARTUPINFO lpStartupInfo,
  __out        LPPROCESS_INFORMATION lpProcessInformation
);

      

Notice the last parameter for which you must pass a pointer to the PROCESS_INFORMATION structure. This structure contains a handle, process ID, etc. when CreateProcess returns.

typedef struct _PROCESS_INFORMATION {
  HANDLE hProcess;
  HANDLE hThread;
  DWORD  dwProcessId;
  DWORD  dwThreadId;
}PROCESS_INFORMATION, *LPPROCESS_INFORMATION;

      

If you already have a process created in other ways, process descriptor information, etc. will not be available to you. In this case, you must list the processes and find the one that interests you. This is illustrated here on MSDN. List of all processes

+2


a source


#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>

void LaunchExplorer() {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof( si ) );

    si.cb = sizeof( si );

    CreateProcess( "explorer.exe", NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi );
}

int main( int, char *[] ) {
    PROCESSENTRY32 entry;
    entry.dwFlags = sizeof( PROCESSENTRY32 );

    HANDLE snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );

    if ( Process32First( snapshot, &entry ) == TRUE ) {
        while ( Process32Next( snapshot, &entry ) == TRUE ) {
            if ( stricmp( entry.szExeFile, "explorer.exe" ) == 0 ) {
                HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, FALSE, entry.th32ProcessID );

                TerminateProcess( hProcess, 0 );

                CloseHandle( hProcess );

                break;
            }
        }

        LaunchExplorer();
    }

    CloseHandle( snapshot );

    return 0;
}

      

+1


a source


Since modern Windows systems are POSIX compliant, you can send a KILL signal to external processes. However, note that this will translate into a call to TerminateProcess, so you can just use it.

http://www.mkssoftware.com/docs/man1/kill.1.asp

0


a source


My answer is too late, but just in case for those who need it ...

/////////////////////////////////////////////////////////////////////////////
bool _killExplorer()
{
    CString strCmd1_KillExplorer = _T("taskkill /f /im explorer.exe");

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof si };
    bool bCmd = true;
    TCHAR szCmdTmp[MAX_PATH];
    _tcscpy(szCmdTmp, (LPCTSTR)strCmd1_KillExplorer);
    if (CreateProcess(NULL, (LPTSTR)szCmdTmp, NULL, NULL, NULL, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        DWORD dwCode = 0;
        if (!GetExitCodeProcess(pi.hProcess, &dwCode))
            bCmd = false;
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    }
    else {
        bCmd = false;
    }
    return bCmd;
}   // _killExplorer()


bool _startExplorer()
{
    CString strCmd2_StartExplorer = _T("%systemroot%\\sysnative\\cmd.exe /c start /B explorer.exe");
    TCHAR szCmdTmp[MAX_PATH];
    DWORD dwSize = MAX_PATH;
    ExpandEnvironmentStrings( (LPCTSTR)strCmd2_StartExplorer, szCmdTmp, dwSize );

    PROCESS_INFORMATION pi;
    STARTUPINFO si = { sizeof si };
    bool bCmd = true;
    if (CreateProcessW( NULL, (LPTSTR)szCmdTmp, NULL, NULL, NULL, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
        WaitForSingleObject(pi.hProcess, INFINITE);
        DWORD dwCode = 0;
        if (!GetExitCodeProcess(pi.hProcess, &dwCode))
            bCmd = false;
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    }
    else {
        bCmd = false;
    }
    return bCmd;
}   // _startExplorer()


void RestartExplorer()
{
    if (_killExplorer()) {
        _startExplorer();
    }
}

      

0


a source


Never kill Explorer: it doesn't make any sense! Just use Refreshing Win32 Shell apis.

-1


a source







All Articles