Shut down explorer programmatically
-
I need to shutdown explorer programmatically. I had to do this once in a C++ project and I used the following code: HWND hwndShell = NULL; hwndShell = FindWindow("Progman", NULL); // Locate the Windows shell if(hwndShell != NULL) { PostMessage(hwndShell, WM_QUIT, 0, 0L)// Tell the Windows shell to quit } Any ideas on how to do this in C#? Thanks! Ian
-
I need to shutdown explorer programmatically. I had to do this once in a C++ project and I used the following code: HWND hwndShell = NULL; hwndShell = FindWindow("Progman", NULL); // Locate the Windows shell if(hwndShell != NULL) { PostMessage(hwndShell, WM_QUIT, 0, 0L)// Tell the Windows shell to quit } Any ideas on how to do this in C#? Thanks! Ian
You can do this using the System.Diagnostics namespace. The following code will kill explorer.exe: Process[] RunningProcesses = Process.GetProcessesByName("explorer"); Process proc = new Process(); foreach(Process p in RunningProcesses) { if(p.ProcessName == "explorer") { proc = p; break; } } proc.Kill(); When I tested it however explorer.exe restarted a few seconds after it was killed. This might be due to some program I had running, but I'm not sure.
-
You can do this using the System.Diagnostics namespace. The following code will kill explorer.exe: Process[] RunningProcesses = Process.GetProcessesByName("explorer"); Process proc = new Process(); foreach(Process p in RunningProcesses) { if(p.ProcessName == "explorer") { proc = p; break; } } proc.Kill(); When I tested it however explorer.exe restarted a few seconds after it was killed. This might be due to some program I had running, but I'm not sure.
Thanks alot! I wonder how you kill it for good? I want to control when it starts up again. Anyone know how to do that? Thanks! Ian
-
You can do this using the System.Diagnostics namespace. The following code will kill explorer.exe: Process[] RunningProcesses = Process.GetProcessesByName("explorer"); Process proc = new Process(); foreach(Process p in RunningProcesses) { if(p.ProcessName == "explorer") { proc = p; break; } } proc.Kill(); When I tested it however explorer.exe restarted a few seconds after it was killed. This might be due to some program I had running, but I'm not sure.
tommazzo wrote: When I tested it however explorer.exe restarted a few seconds after it was killed. It's supposed to do this. By default, Explorer automatically restarts itself if it's shutdown without a user session logoff. Sometimes, though, it doesn't restart itself. When this happens, you can still hit Ctrl-Alt-Del, start the Task Manager, click on File/New Task (Run), then type CMD in the prompt, NOT EXPLORER! Instead, type CMD and in the resulting command window, type START EXPLORER. For some reason, if Explorer crashes and you type EXPLORER in the Start/Run box, it won't restart. Though, typing the START EXPLORER command in a CMD window works. Wierd... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Thanks alot! I wonder how you kill it for good? I want to control when it starts up again. Anyone know how to do that? Thanks! Ian
You can't. Explorer automatically restarts itself. But, I seem to, vaguely(!), remember a policy somewhere that controls this. I can't find it and I can't remember where I saw it, though. BTW: Why on earth would you want to do this anyway? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
You can't. Explorer automatically restarts itself. But, I seem to, vaguely(!), remember a policy somewhere that controls this. I can't find it and I can't remember where I saw it, though. BTW: Why on earth would you want to do this anyway? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Why? It's a long story... The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation?
-
Why? It's a long story... The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation?
Ian Bowler wrote: The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation? This is, I believe, because you sent the process the WM_QUIT message, with Process.Kill() you kill it without sending it the WM_QUIT message. In order to keep explorer from restarting you could either run a loop on a new thread, which continously checks if explorer has been restarted and kills it again if neccessary (this isn't really the best idea). Or (and this should be the better idea) you could import the FindWindow and PostMessage methods with DllImport and use them the same way as in C++;
-
Ian Bowler wrote: The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation? This is, I believe, because you sent the process the WM_QUIT message, with Process.Kill() you kill it without sending it the WM_QUIT message. In order to keep explorer from restarting you could either run a loop on a new thread, which continously checks if explorer has been restarted and kills it again if neccessary (this isn't really the best idea). Or (and this should be the better idea) you could import the FindWindow and PostMessage methods with DllImport and use them the same way as in C++;
Thanks! I'll give it a whirl... -Ian
-
Why? It's a long story... The C++ code in the original post shuts down explorer indefinately. Is the limitation a C# limitation?
I've got time. :-D Honestly, if an application killed off Explorer for me, without my permission, I'd want to know where on earth this person was who wrote it, for some very not-so-nice reasons... Please tell me this isn't being deployed into the real world? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
I've got time. :-D Honestly, if an application killed off Explorer for me, without my permission, I'd want to know where on earth this person was who wrote it, for some very not-so-nice reasons... Please tell me this isn't being deployed into the real world? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
I understand your concerns and curiosity. I'm not going to be shutting down explorer all willy-nilly. There's a good reason for it in the context of what I'm working on. For those of you that may wonder how this all turned out, I had to import the User32.dll functions, FindWindow and PostMessage (as tommazzo suggested). The final code looks like this: public class Win32 { public Win32(){} [DllImport("User32.dll")] private static extern IntPtr FindWindow(string ClassName, string WindowName); [DllImport("User32.dll")] private static extern Boolean PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); public bool ShutdownExplorer() { Boolean bSuccess = false; IntPtr Handle = new IntPtr(0); // Get a handle to explorer.exe Handle = FindWindow("Progman", null); // Shutdown explorer.exe if(Handle != IntPtr.Zero) bSuccess = PostMessage(Handle, 0x0012, IntPtr.Zero, Ptr.Zero); return bSuccess; } } Thanks tommazzo for the solution. Thanks Dave for your insights. Regards, Ian