Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently I managed to get my hands on a old Starcraft CD that a friend of mine was throwing out and, having always enjoyed playing Starcraft, installed it on my Windows 7 PC, updated it with the latest patches and tried to run it. It had some problems and crashed a few times, but I found a solution that said simply close all explorer processes, run starcraft and run explorer again. So I wrote a little Batch File:

REM RunStartcraft.bat
REM Closes Explorer, runs StarCraft then starts Explorer again
title Run Starcraft
echo Stopping Explorer...
taskkill /f /im explorer.exe
echo Running Starcraft...
echo  Restarting Explorer...
start explorer.exe

and this works.

However what I don't like about this batch file is that it kills all Explorer processes instead of closing them nicely (including the base Explorer process). Is it possible to close Explorer nicely (like shift-right-clicking the start menu and clicking 'Exit Explorer') without killing the processes in a Batch file? Or does it not really matter if I kill all the explorer processes?

EDIT: I'm not trying to get a list of running Explorer processes, but I'm wondering whether it's possible to exit the Explorer process(es) gracefully without having to abruptly Kill/End the Process using taskkill.

share|improve this question
up vote 3 down vote accepted

Hey look! A cross-site duplicate! :D

I've made and uploaded a program that attempts to stop and start Explorer. It doesn't always work, depending on the state of Explorer, but it usually does. :)

Here's the (short) source code, written in D:

import core.stdc.wchar_,;
struct STARTUPINFOW { DWORD cb; LPWSTR lpReserved, lpDesktop, lpTitle; DWORD dwX, dwY, dwXSize, dwYSize, dwXCountChars, dwYCountChars, dwFillAttribute, dwFlags; WORD wShowWindow; WORD cbReserved2; LPBYTE lpReserved2; HANDLE hStdInput, hStdOutput, hStdError; }
struct PROCESS_INFORMATION { HANDLE hProcess; HANDLE hThread; DWORD dwProcessId; DWORD dwThreadId; }
extern(C) static int D15TypeInfo_Struct6__vtblZ = 0; //Don't need this
extern(C) ubyte[1024] D4core3sys7windows7windows16WIN32_FIND_DATAW6__initZ; //Don't need this
pragma(lib, "msvcrt.lib"); pragma(startaddress, mainCRTStartup);
int mainCRTStartup() //NOT  int main(), since we don't need Phobos or the D runtime
    const HWND hWnd = FindWindowW("Shell_TrayWnd", null);
    if (hWnd != null)
        DWORD pid;
        GetWindowThreadProcessId(hWnd, pid);
        HANDLE hProcess = OpenProcess(0x00100000, false, pid);
        wchar pathBuf[32 * 1024] = void;
        auto si = STARTUPINFOW(STARTUPINFOW.sizeof);
        if (hProcess != null && ExpandEnvironmentStringsW(r"%SystemRoot%\explorer.exe", pathBuf.ptr, pathBuf.length) > 0
            && SendMessageW(hWnd, 0x5B4, 0, 0) == 0 && WaitForSingleObject(hProcess, INFINITE) == 0)
                return !CreateProcessW(pathBuf.ptr, null, null, null, false, 0x04000608, null, null, si, pi);
    return -1;
extern (Windows)
    BOOL SendMessageW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    HWND FindWindowW(LPCWSTR lpClassName, LPCWSTR lpWindowName);
    HANDLE OpenProcess(in DWORD dwDesiredAccess, in BOOL bInheritHandle, in DWORD dwProcessId);
    DWORD GetWindowThreadProcessId(HWND hWnd, out DWORD lpdwProcessId);
    DWORD ExpandEnvironmentStringsW(in LPCWSTR lpSrc, LPWSTR lpDst, in DWORD nSize);

Hope that helps!

share|improve this answer
Thanks, I'll give it try now... – Kryten May 12 '11 at 8:46
Thanks very much! Exactly what I'm looking for! – Kryten May 12 '11 at 8:53
@Kryten: Sure! It was exactly what I was looking for too, when I posted that question on SO. :D – Mehrdad May 12 '11 at 8:54

Try tasklist /FI "IMAGENAME eq explorer.exe".

Note that you may need to run this elevated if you want to see tasks run by the System or by other users.

share|improve this answer
Sorry, I'm not trying to get a list of running Explorer processes, but I'm wondering whether it's possible to exit the Explorer process(es) gracefully without having to abruptly Kill/End the Process using taskkill (I'm sure it can't be good to constantly kill the explorer processes whenever I want to play Starcraft)... – Kryten May 12 '11 at 8:36
Ah, I see. In that case, just do taskkill /im explorer.exe. Without the /f that ends the process without forcing it to quit. However, that is not guaranteed to work as the OS will wait for the process to finish handling the quit. .... NM, I just tried that and Windows asks if you want to shut down! – Bacon Bits May 12 '11 at 8:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .