views:

60

answers:

3

I need to kill windows explorer's process (explorer.exe), for that

lets say i use a native NT method TerminateProcess

It works but the problem is that the explorer starts again, may be windows is doing that, anyway. When i kill explorer.exe with windows task manager, it doesn't come back, its stays killed.

I want to do whatever taskmanager is doing through my application.

Edit:
Thanks to @sblom i solved it, a quick tweak in the registry did the trick. Although its a clever hack, apparently taskmnager has a cleaner way of doing that, that said, i've decided to go with @sblom's way for now.

+5  A: 

From Technet:

You can set the registry key HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoRestartShell to 0, and it will no longer auto-restart.

sblom
@sblom Great ! thanks man it works, one little mystery although your workaround works perfectly. taskmanager does not seem to mess with registry settings like this. Now please please dont take it personally i was just wondering. Your answer was a life saver, as i needed it urgently
Vivek Bernard
@sblom just to give other people a chance to solve that taskmanager mystery i leave your anwser as un-accepted for a little while. I'll do that eventually !
Vivek Bernard
+1  A: 

What you probably need to do is instead of using TerminateProcess, post a WM_QUIT message to the explorer windows and main thread. It's a bit involved, but I found this page which has some example code that might help you along:

http://www.replicator.org/node/100

Windows will automatically restart explorer.exe after a TerminateProcess so that it restarts in the case of a crash termination.

Gerald
+1  A: 

The "real" solution. (Complete program. Tested to work on Windows 7.)

using System;
using System.Runtime.InteropServices;

namespace ExplorerZap
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

        static void Main(string[] args)
        {
            int hwnd;
            hwnd = FindWindow("Progman", null);
            PostMessage(hwnd, /*WM_QUIT*/ 0x12, 0, 0);
            return;
        }
    }
}
sblom
@sblom +1 and accepted. Thanks man , much appreciated !
Vivek Bernard