Is there a "Legal" API to exit explorer.exe? - c#

I need to restart Windows Explorer from my custom installer, but the catch is that I can't simply "kill its process" I need it to close "gracefully" so that it saves its settings.
While doing a search I found out that Windows has this option covered. If you open up the Start menu, then Ctrl+Shift+Right-click on an empty space of the start menu, you'll see a context menu that gives you the "Exit Explorer" command that does exactly what I need.
So I was wondering, is there an API to do that from a program?

The question of how to mimic the behaviour of "Exit Explorer" has been asked already.
But this is not what the installer should do, instead the installer should use Restart Manager Functions.
You can use it for various things depending on your needs:
Log out the user and log in again.
Close Explorer.exe and start it again.
Close services and start them again.

Related

Open Explorer.exe and raise event when exe is exited [duplicate]

I have a program that is opening an explorer window to a certain folder but i want to perform an action right after the explorer window is closed, but if I use the following code:
Process proc = Process.Start("explorer.exe", "D:\\");
proc.WaitForExit();
It is opening the explorer window as desired but the WaitForExit command has no effect and it just goes right past it.
Is there a different way of opening the explorer window that will be able to let me know when it is closed by the user?
The problem is explained pretty well at The Old New Thing:
The reason that WaitForSingleObject returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your WaitForSingleObject returns immediately.
He offers a couple solutions you could probably use (with some heavy use of PInvoke), like using something like this.
In the end it might just be easier for you to use some other type of file browser maybe from a C# library somewhere that you have more control over, rather than explorer.
Cannot regenerate the error. Just tried this:
Process.Start("explorer.exe", "D:\\").WaitForExit();
and it blocks the current thread and waits until I close the explorer windows. Make sure that you're not executing the command on another thread than the one you want to block. Also make sure that you set every instance of a window to start a new instance of explorer.exe via importing below .reg file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
You'll need to restart your computer for this to take effect.

Disable Libraries window popup when explorer restarted

I have written an application that changes some of the registry settings and then restarts explorer to save them. Everytime explorer is restarted an annoying "Libraries" popup comes up.
I want to disable it somehow though C# code, (probably by tweaking some other registry keys). However, I do NOT want to disable libraries feature, just a popup that comes up when explorer is restarted.
All articles on the web show how to disable libraries completely.
The most likely reason for this to happen is that the Libraries window was open the last time Explorer was shut down naturally, i.e., the last time the user logged off. If you close the window and log off, then log in again, your software will probably work correctly.
Caveat: killing and restarting the explorer process is inherently risky. Resolving this particular issue does not mean that other similar issues won't show up in future.
When restarting explorer I just had to add some randow parameter as it seems to default to Libraries window. so calling "explorer.exe xxx" would cause explorer to ignore default parameters.

Open explorer window and wait for it to close

I have a program that is opening an explorer window to a certain folder but i want to perform an action right after the explorer window is closed, but if I use the following code:
Process proc = Process.Start("explorer.exe", "D:\\");
proc.WaitForExit();
It is opening the explorer window as desired but the WaitForExit command has no effect and it just goes right past it.
Is there a different way of opening the explorer window that will be able to let me know when it is closed by the user?
The problem is explained pretty well at The Old New Thing:
The reason that WaitForSingleObject returns immediately is that Explorer is a single-instance program (well, limited-instance). When you open an Explorer window, the request is handed off to a running copy of Explorer, and the copy of Explorer you launched exits. That's why your WaitForSingleObject returns immediately.
He offers a couple solutions you could probably use (with some heavy use of PInvoke), like using something like this.
In the end it might just be easier for you to use some other type of file browser maybe from a C# library somewhere that you have more control over, rather than explorer.
Cannot regenerate the error. Just tried this:
Process.Start("explorer.exe", "D:\\").WaitForExit();
and it blocks the current thread and waits until I close the explorer windows. Make sure that you're not executing the command on another thread than the one you want to block. Also make sure that you set every instance of a window to start a new instance of explorer.exe via importing below .reg file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer]
"DesktopProcess"=dword:00000001
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
[HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\BrowseNewProcess]
"BrowseNewProcess"="Yes"
You'll need to restart your computer for this to take effect.

Attaching Visual Studio debugger to Windows service -- "attach" greyed out

I am trying to attach to a windows service using Visual Studio 2010 → Debug → Attach to process command. When I scroll through the list of processes my Windows service is greyed out and the attach button is also greyed out.
I have tried changing the service account to local service, my account, etc., but it remains greyed out. Is there a way to troubleshoot this?
I usually have the same issue and I take care of it by adding a boolean to my configuration that triggers a debug launch. You can launch a Visual Studio debugger instance that attaches to your Windows service process by calling this:
System.Diagnostics.Debugger.Launch();
What's nice is that you can call it wherever you wish in your code.
In Visual Studio 2010, on the 'Attach to Process' dialog, check the two check boxes at the bottom that say 'Show processes from all users' and 'Show processes in all sessions'.
On doing that I found that I had my process show up twice, one running as System and one running under my local Windows login. I am not sure why (any input on this would be great!), but I was able to attach to the not-grayed-out one that showed up and was also able to step through my code this way.
The second one was the servicename.vshost.exe process as Ozz pointed out in the comments.
If you still do not see it, make sure your service is still running. Sometimes you might have a failing service that started and automatically stopped and therefore doesn't show up in the attach to options.
Not only did I have to select show processes from all users and the show processes in all sessions check boxes; I also had to click the Select button and then choose the Managed code.
Make sure you run Visual Studio as an administrator. It will require administrator rights to attach to a running service.
After installing the Windows service, start it. Then in Visual Studio open the Attach to process window (Alt + Ctrl + P), find your process and select it and click Attach Button in the bottom of the window.
You may not have permissions to attach to the service. Do you have admin privileges on the desktop? Many corporations do not allow this by default, supposedly to assist in securing their environment.
I solved my problem by changing the way the setup was generated, from RELEASE to DEBUG. And then, I installed the service and attached as usual.
I ran into the same issue today with none of the previous solutions working. As it turns out, it is not possible to attach to a process if you have a ProcDump session monitoring that process.
Use menu Tools → Attach to Process. Simply check Show Processes from all users at the bottom left. Sort by Process and try looking for your Windows service.

Automatically close windows explorer

i am trying to write a program that close explorer then runs another program.
i am getting a problem when trying to close explorer using the following code:
foreach (Process p in Process.GetProcesses())
if (p.MainModule.ModuleName.Contains("explorer"))
p.Kill();
can somebody please let me know why it is doing this and provide a solution
CHEERS
p.s. this is not a malicous program, it is going to run a game that doesn't work properly when explorer is in the background
The problem is that you can have multiple versions of Explorer running at any one point in time... and you usually need at least one of them. The shell that hosts the Start Menu is actually an instance of Explorer. So if you close all instances of Explorer, you'll also be shutting down the main shell, which is not what you want to do.
However, the fastest way to do get all instances of Explorer and kill them is:
foreach (Process p in Process.GetProcessesByName("explorer"))
{
p.Kill();
}
There is a simple undocumented way to exit explorer cleanly, see also question Gracefully Exit Explorer (Programmatically). It is intended for developers working on shell extensions.
The procedure is different for Windows XP and Windows 7:
Windows XP:
Open the shutdown dialog (Start > Shutdown), then cancel the dialog pressing CTRL-SHIFT-ALT-ESC (or hold down CTRL-SHIFT-ALT and press the Button with the mouse).
Windows 7:
Open the Start menu and then hold CTRL-SHIFT while right-klicking into the empty area of the start menu, see screenshot. A context menu appears, where the second entry is 'Exit Explorer' (without CTRL-SHIFT the context menu has only one entry)
p.s. this is not a malicous program, it is going to run a game that doesn't work properly when explorer is in the background
Explorer is a critical Windows component. You should debug why you have problems when Explorer is running, and fix those.
Killing Explorer will cause severe problems for your users.

Categories