24

The Problem

In one part of a batch file (kind of, see Extra Information) I need to restart Explorer, so I use the, tried-and-tested method of

taskkill /f /im explorer.exe >nul
explorer.exe

Then this happens

  1. explorer.exe is successfully terminated
  2. explorer.exe is started (see Image 2), but only an Explorer window opens, which I am left with indefinitely (see Image 1)

I can then only properly restart Explorer by starting a new task from Task Manager, as, I'm assuming, Win + R is part of Explorer.

Extra Information

Now, I say "kind of" as I'm running the batch file from a self-executing SFX archive, created with WinRAR. So, when executed, the contents of the archive are extracted to %temp% and a user-defined file (usually a boot-strapper and, in this case, my batch file) is run upon successful extraction.

So far, I've deduced

  1. explorer.exe definitely is being fully killed.
  2. The batch file definitely is called and executed correctly, as it runs and everything else in the script works as designed, except for the line that starts explorer.exe
  3. The command to restart Explorer isn't "badly timed", or anything, as I've tried delaying it.
  4. The batch file works perfectly when manually extracted from the archive, so it's not a problem with the compression or extraction processes.
  5. Even with commands like start explorer.exe | cmd.exe Explorer doesn't restart properly, so it's definitely not a problem with the .bat file.

I can confirm that it works on Windows XP and Windows 7 x86 but not Windows 7 x64 (which is my system).

Status

At the moment, I'm suspicious of WinRAR, as I've proved that the code itself works. So, I'm creating the self-executing SFX with different versions of WinRAR. So far, I've tried versions:

  • 4.11 x86
  • 4.11 x64
  • 4.20b3 x86
  • 4.20b3 x64

and had the same results every time.

I submitted a bug report to dev@rarlab.com yesterday and got a reply from Eugene Roshal himself this morning

Hello, SFX module uses ShellExecuteEx to start a setup application. Normally it works well. I do not know why Explorer decides to switch to windowed mode. Now I built a small standalone program

#include <windows.h>    
void main()
{
  SHELLEXECUTEINFO si;
  memset(&si,0,sizeof(si));
  si.cbSize=sizeof(si);
  si.lpFile="test.bat";
  si.nShow=SW_SHOWNORMAL;
  ShellExecuteEx(&si);
}

which runs test.bat with contents as in your sample. This program shows exactly the same behavior as WinRAR SFX, so Explorer is started in window.

and a second email this morning

Sorry, no advice now. I replaced ShellExecuteEx with CreateProcess

#include <windows.h>
void main()
{
  STARTUPINFO si;
  PROCESS_INFORMATION pi;
  memset(&si,0,sizeof(si));
  si.cb=sizeof(si);
  CreateProcess(NULL,"test.bat",NULL,NULL,TRUE,0,NULL,NULL,&si,&pi);
}

but result is the same. I tried to use other SW_ flags like SW_SHOWDEFAULT or SW_RESTORE with ShellExecuteEx also as "open" and "explore" lpVerb, but it does not help. For now I do not understand the logic behind this windowed versus desktop mode.

I realise the outlook is grim but, I hope that's of help to someone..

Proof / Evidence

Link to an SFX archive demonstrating this, if anyone wants it: https://dl.dropbox.com/u/27573003/Social%20Distribution/restart-explorer.exe

image 1

image 2

You may notice here that I'm running the commands inside a VM (as denoted by VMwareTray.exe) but it is not a VM-caused conflict. I've tested the exact same files on my own host system (which is the same OS) and have had the same results.

Update

I'm experiencing similar "works outside of an SFX archive but not from one" problems when using REG ADD in a completely different project. I just don't think SFX archives play nice with batch files.

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
mythofechelon
  • 3,692
  • 11
  • 37
  • 48
  • Works on XP 32. Maybe your exe doesn't have the license to kill? UAC and Internet Zone security settings might be the culprits. – panda-34 Jun 08 '12 at 19:12
  • But it does kill the process. It just doesn't restart it properly. And there aren't any UAC popups and running as admin doesn't make a difference. – mythofechelon Jun 08 '12 at 19:32
  • Wait, if it is actually killing the process, and it's opening an explorer window afterwards, then how is it not restarting explorer? Last I checked, you can't have an explorer window open without explorer running. – JAB Jun 08 '12 at 19:54
  • 1
    I wonder if some part of Win-RAR is running in 32-bit mode? Could you even start explorer64 running from a 32-bit process? I am pretty certain that Windows won't do that. –  Aug 28 '12 at 18:24

15 Answers15

16

I think user1631170 is on to something, "I wonder if some part of Win-RAR is running in 32-bit mode? Could you even start explorer64 running from a 32-bit process? I am pretty certain that Windows won't do that."

When I start explorer.exe from ProcessHacker (32-bit process manager), I get an explorer window.

But I can force it to start the 64-bit explorer with this:

%systemroot%\sysnative\cmd.exe /c start /B explorer.exe

sysnative is a keyword that Windows recognizes to bypass the file system redirection for 32-bit/64-bit (http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx Enjoy!

Doug Benham
  • 3
  • 1
  • 4
10

I had this same problem and found that all the solutions here still didn't work from a batch script.

None of these worked completely:

start explorer.exe
start explorer
explorer.exe
explorer

because they all either opened a window (and didn't show the taskbar again), or the batch script then hung thereafter and couldn't execute any more commands

I found that this line in the batch file did work (after killing explorer.exe):

start "" "%windir%\explorer.exe"

and also allowed other commands to be executed after it in the script

sparrowt
  • 2,641
  • 25
  • 24
7

This works in Windows 7:

taskkill /f /IM explorer.exe
start explorer.exe
exit
Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
  • 2
    Only works if you're in a 32-bit process on a 32-bit system, or 64 on a 64. It fails if you're in a 32-bit process on a 64-bit system. – nerdfever.com Apr 16 '18 at 22:53
3

For restarting explorer.exe, this worked for me.

powershell.exe Stop-Process -processname explorer
Matt
  • 81
  • 3
2

When you run explorer.exe from an 32-bit application in 64-bit windows, the path will be redirected to the SysWOW64 directory which contains the 32-bit explorer.exe.

In XP64 it wasn't not such a big deal. In the taskmanager you can see the 32-bit explorer.exe running but it did start as the shell. In Windows 10 (as I came to this problem, it looks like it is introduced in Windows 7), the 32-bit explorer.exe is a stub which creates a new instance of the 64-bit explorer.exe. It probably passes a path on the commandline here so the 64-bit explorer.exe opens a window instead of starting the shell.

So it is still like before that you can control whether a window or a shell should be started by starting explorer.exe with or without a path as commandline parameter.

Instead, you should force starting the 64-bit explorer.exe from the 32-bit application and all is ok. To do this, one method is using the sysnative directory as mentioned above. But another method is to use Wow64DisableWow64FsRedirection/Wow64RevertWow64FsRedirection.

I did the latter and can confirm it works nicely. For both CreateProcess and ShellExecuteEx API.

Arnoud Mulder
  • 139
  • 1
  • 5
1

Try

%windir%\explorer.exe
start %windir%\explorer.exe
start /d%windir% explorer.exe
Zombo
  • 1
  • 62
  • 391
  • 407
panda-34
  • 4,089
  • 20
  • 25
0

I have seen similar problems before doing this in C#. The process had to be invoked by calling explorer shell rather than explorer window, but I haven't had any problems in batch.

Try using this:

taskkill /im explorer.exe /f
explorer

The difference between the other answers being explorer rather than explorer.exe which has caused problems before for me.

This works on my Win7 x64 PC.

Hope this helps!

Bali C
  • 30,582
  • 35
  • 123
  • 152
  • The batch file works, but, as usual, the compiled .exe doesn't. :| – mythofechelon Jun 11 '12 at 09:41
  • What do you mean? Are you compiling the batch file to an exe using a converter? – Bali C Jun 11 '12 at 09:45
  • I think I did explain in my question, I'm compiling an .exe with WinRAR's in-built SFX function. ... That's a thought, actually. I hadn't considered it may be an issue with WinRAR. I do have the latest stable release, but I'll try a newer version now. – mythofechelon Jun 11 '12 at 09:47
  • Sorry, I didn't read the question properly. If it works outside of WinRAR then it's almost certainly a problem with that, as you said, try the latest version. – Bali C Jun 11 '12 at 09:49
  • Can't believe that possibility didn't occur to me. I do agree that it has to be a problem with the program, but I have no idea what the problem could be. The batch file is actually called and executed correctly. After all, the extracted batch file works (so it's not a problem with compression) and everything else in the script works as designed, except for that one line. Odd.. I'll try it anyway, though, obviously. – mythofechelon Jun 11 '12 at 09:57
  • Yeah, it still doesn't work with the new version. So it's out of my hands, it seems. :( – mythofechelon Jun 11 '12 at 10:07
  • Hmmm, very strange. All I can suggest is try to rebuild the archive with the latest version installed? – Bali C Jun 11 '12 at 10:12
  • Or try and build it with another program, maybe 7-Zip? – Bali C Jun 11 '12 at 10:13
  • Oh, I didn't know 7-zip had that functionality. Tbh, I've never particularly wanted to try 7-zip, as WinRAR has always "just worked", so I've had no reason to try it. This may be considerable cause, though. I'll try that now, thanks. – mythofechelon Jun 11 '12 at 10:16
  • Yeah same, WinRAR has always done the job so never been inclined to use 7-Zip or WinZip but I have seen self extractors built with it before, so it's worth a shot. – Bali C Jun 11 '12 at 10:19
  • 7zip does support SFX archives, but it doesn't seem to support self-executing SFX archives. – mythofechelon Jun 11 '12 at 12:44
0

The other day, I was having a look through some of WinRAR's more advanced options and came across this tab:

enter image description here

As soon as I saw that I suspected it to be part of the problem and solution, as this issue only ever occurs on Windows 7 x64.

As suspected, using the Default64.SFX module instead of the default Default.SFX module entirely fixed the issue. Finally.

mythofechelon
  • 3,692
  • 11
  • 37
  • 48
0

I do not understand why, but it will only for me if using the full path, regardless of your PATH variable.

%windir%\explorer
Zombo
  • 1
  • 62
  • 391
  • 407
0

Have same issue with Visual Studio.

What works for me (Win 7 Pro 64bit):

PPM on Project name select "Properties"

Configuration Properties > Build Events > Pre-Build Event

taskkill /im explorer.exe /f

Configuration Properties > Build Events > Post-Build Event

start "" "C:\Windows\explorer.exe"

But this make other problem (the IDE is frozen after the explorer runs) and now I'm only able to restart the IDE to run build command again...

Paweł Liszka
  • 330
  • 1
  • 2
  • 17
0

Use this (.bat with administrative privileges) in x64 or x86

tasklist /fi "imagename eq explorer*" | find /i "explorer*"
if not errorlevel 1 (taskkill /f /im "explorer*") else (
start %windir%\explorer.exe
0

What worked for me in Windows 7 64 bit was "C:\Windows\expstart.exe" or just expstart.exe

0

Try adding a explorer.exe key to App Paths in the registry.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\explorer.exe

(Default) C:\Windows\explorer.exe

Path C:\Windows

or copy the following to notepad and save it as a .reg file then run it:


Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\explorer.exe] @="C:\Windows\explorer.exe" "Path"="C:\Windows"

0

To see the start panel you need to execute the explorer.exe from the system32 folder

taskkill /im explorer.exe /f

cd %WINDIR%\system32
start %WINDIR%\explorer.exe
book777
  • 13
  • 1
  • 1
-6

Easy Context Menu

Just right-click on the desktop and choose Restart Windows Explorer. Enjoy!

Screenshot of the configuration

marcanuy
  • 23,118
  • 9
  • 64
  • 113
Alex
  • 1