I'm working on a windows shell extension, and unfortunately, when making changes to the DLL, I must restart windows explorer (since it keeps the DLL in memory).
I found this program from Dino Esposito, but it doesn't work for me.
void SHShellRestart(void)
{
HWND hwnd;
hwnd = FindWindow("Progman", NULL );
PostMessage(hwnd, WM_QUIT, 0, 0 );
ShellExecute(NULL, NULL, "explorer.exe", NULL, NULL, SW_SHOW );
return;
}
Does any one have something they can share to do this?
P.S. I realize that I can go to task manager and kill the explorer process, but I just want to do it the lazy way. Besides, this enables automation.
P.P.S I am using .NET for the development, but the shell restart functionality could be in C, C++ or a .NET language. It will simply be a small stand-alone executable.
After parsing some of the earlier answers and doing a bit of research, I've created a little complete example in C#. This closes the explorer shell then waits for it to completely shut down and restarts it. Hope this helps, there's a lot of interesting info in this thread.
After FindWindow use GetWindowThreadProcessId, then OpenProcess, then TerminateProcess.
This is for Windows 7/8 (and need testing, maybe even works on Vista).
Since there is a proper way to close Explorer (progman) included in Windows 7 & 8 - by right clicking the taskbar (Shell_TrayWnd in Win8 or StartMenu on Win7) while pressing Ctrl-Shift, it shows in the popup menu a hidden option to close Explorer, and digging it using Spy++ it is triggered by message WM_USER+436.
So I tested and doing the following it works great.
It closes Explorer, with all the opened instances. And to relaunch explorer, use the methods provided above.
So, please confirm in comments if this works on 32bit/64bit editions of your windows vista/7/8 or any other.
This works for me on Vista:
But I can't find any way to suppress the explore window that opens (I tried, hence the SW_HIDE). On Vista, running explorer.exe without parameters seems to be the same as running "explorer.exe /e" on earlier systems. You'll have to try it for yourself on XP, I don't have it here.
Note: Using TerminateProcess does seem extreme, but posting a WM_CLOSE to explorer provokes a windows shutdown dialog.
After some more googling, I came up with the following C# solution:
I noticed no one addressed the issue of starting explorer.exe as the shell, rather than it just opening an explorer window. Took me a while to figure this out, turns out it was something simple:
You have to set the StartInfo.UseshellExecute as true to get it to restart as the shell.