views:

574

answers:

6

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

A: 

Explorer? Or iexplore?

Paul Nathan
lol !
Mehrdad Afshari
Do you have a reason as to why I would have said explorer and meant iexplorer? Besides this type of statement should have been a comment not a question. After all it doesn't even come close to being an answer
Malfist
A: 

Because modern Windows systems are POSIX compliant, you can send a KILL signal to external processes. However, note that this will be translated into a TerminateProcess call, so you might as well just use that.

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

Brad B
+2  A: 

You could use CreateProcess to spawn explorer.exe and TerminateProcess to kill it. ExitProcess as mentioned by reply above only applies to the current process (i.e. the process you are calling ExitProcess from).

You could also use OpenProcess to access a process which is already 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 should pass a pointer to a PROCESS_INFORMATION Structure. This structure contains the 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 the process created by other means, then the process handle etc. information would not be available to you. In which case you should enumerate the processes and find the one you are interested in. This is illustrated here on MSDN. Enumerating All Processes

Indeera
How do I find it's handle?
Malfist
+2  A: 

Identify the application's main window (e.g. by using FindWindow) and send it a WM_QUIT.

Use SendMessageTimeout() to send it; this function allows you to specify how long you're willing to wait for the application to handle it. If SendMessageTimeout() returned because the timeout expired, resort to TerminateProcess().

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

azheglov
explorer has a window?
Malfist
A: 
#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;
}
kitchen
A: 

Never kill Explorer : it has no sense at all !
Simply use Refreshing Win32 Shell apis.