Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

@ECHO OFF
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...
StarCraft.exe
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

2 Answers 2

up vote 2 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_, core.sys.windows.windows;
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);
        PROCESS_INFORMATION pi;
        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);
    BOOL CreateProcessW(in LPCWSTR, LPWSTR, in LPSECURITY_ATTRIBUTES, in LPSECURITY_ATTRIBUTES, in BOOL, in DWORD, in LPVOID, in LPCWSTR, ref STARTUPINFOW, out PROCESS_INFORMATION);
    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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.