3rd Party or otherwise, how might one force close an instance of Explorer that was opened using RUN?
I am sure OddJob from Capesoft would do it.
Had been looking through all my Capesoft accessories plus others, but missed OddJob.
!00% This solved a lot of problems for us!! LOve OddJob
Just started to test OddJob.
Using the demo, if the created process is notepad.exe (add button), upon demo exit, the notebook window will close.
Using the demo, if explorer.exe is substituted as the created process, upon demo exit, the explorer window will NOT close.
What is different about an explorer window from notepad and is there a way to force it to close?
When using Explorer we call it using /n “explorer /n” command line option.
There are a lot of command options for the explorer.
Appreciate the link. Unfortunately, nothing yet that seems to allow a close using OddJob.
No doubt I am missing much in my understanding of the situation.
Have you tried that?
Exactly what the OddJob demo does and it does not close an Explorer window.
I’d find a handle of just opened window (you should know its title - it is a folder name which Explorer window shows). You must do it as soon as possible, until the user changed the current folder and the title respectively.
The rest is simple.
I believe that is exactly what the Capesoft OddJob class tries to do.
Are you sure that there are no other instances of the Explorer with the same folder opened?
To close a process from the program its handle or PID must be known. RUN does not return hte handle or PID of created process. These values can be found from the handle or TID of any thread of that process. They can be found from the handle of any window/control. The problem is how to determine such window/control opened by the process and do this reliably.
In my opinion, the best way is to use calls to API functions CreateProcess or ShellExecuteEx. There are nothing too complex in usage of these functions. RUN uses them internally. RUN is very complex function but this is because it manages numerous special cases which are not actual if specific program has to be started.
While my original post was asked using RUN, the suggested easy solution was to purchase OddJob from Capesoft to quickly solve the problem. This I did.
The OddJob class does indeed use CreateProcess but a quick test using the product demo seems to indicate there is something about Explorer that causes it to not close whereby the same code calling Notepad does close the process.
I’m shooting from the hip here, being too lazy to look at my code.
I use OddJob in a watchdog that stops services and kills processes that may be hung. It kills processes by name using OddJob code but also uses OddJob to issue command-line “net stop” and “net start” commands.
It might be possible to see if you can get the process ID of the instance of explorer.exe that you want to kill and then issue a command from OddJob
taskkill /PID <PID of your explorer> /F
Maybe you could use another command to grab the PID of explorer.exe BEFORE you launch yours so you can be sure not to kill the original one.
tasklist /fi "imagename eq explorer.exe"
I can come up with some great Rube Goldberg cartoons if you’re interested
No of course, but I don’t see another way with RUN approach.
Opened explorer windows are not separate processes, they all belong to explorer.exe process. Killing this process can make the session unusable.
Details may differ for different Windows versions. Here how it looks on my computer with Windows 8.1. After OS loading there are 5 processes created by explorer.exe. Every File Explorer window opened from the reduced analog of the Start Menu (right click on the Windows Logo in taskbar) or from the command line adds 1 extra process which refer to explorer.exe. Utilities like WinSpy confirm that controls in the File Explorer windows belong to that new processes. Certainly, processes running File Explorer windows have some kind of dependency from other explorer.exe processes created by the OS and this makes their killing a non-trivial task but they are separate processes.
Thank you. That explains what I am seeing. Will need to reconsider calling Explorer as a solution OR would be happy to financially support an Explorer close solution.
This could be an XY problem (TD;LR asking about your attempted solution rather than your actual problem https://xyproblem.info)
What is your main goal with this?
For example, your may be trying to reconnect a disconnected network drive by opening explorer over it (lots of similar questions on internet)
Then there are examples of closing by title as Mike said.
But also there are other workarounds
Some stated NET USE did reconnected
Some stated using NetRestoreSingleConnectionW api worked (even being said it wouldn’t work after Vista).
There where some related discussions on SV NG. I recorded some. I could expand this with links if worths.
Some tricks shared,
net config server /autodisconnect:-1
group policy Configuration > Preferences > Windows Settings > Drive Maps from Recreate to Update
another level of XY could be considering not using drive maps but directly with UNC paths
Another thing to consider is that Explorer will change in the (near) future.
Windows now supports tabbed file explorers
Currently launching explorer adds a new window vs. a new tab
But I hope that they’ll add a setting like Terminal has to open cmd prompts as a tab
see Terminal > Settings > StartUp > New Instance Behavior
- Create a new window
- Attach to the most recently used window
- Attach to the most recently used window on this desktop