I would like to kill and restart explorer.exe from my C++ application, how would I do this?
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.
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.
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
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
#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;
}