退出所有 Explorer 进程而不终止它们?

退出所有 Explorer 进程而不终止它们?

最近我设法弄到了一张朋友扔掉的旧星际争霸 CD,我一直很喜欢玩星际争霸,所以我把它安装在我的 Windows 7 PC 上,用最新的补丁更新它并尝试运行它。它出现了一些问题并崩溃了几次,但我找到了一个解决方案,只需关闭所有资源管理器进程,运行星际争霸并再次运行资源管理器即可。所以我写了一个小的批处理文件:

@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

而且这有效。

但是我不喜欢这个批处理文件的地方在于,它会终止所有 Explorer 进程,而不是正常关闭它们(包括基本 Explorer 进程)。是否可以正常关闭 Explorer(例如按住 Shift 键并右键单击开始菜单,然后单击“退出 Explorer”)而不终止批处理文件中的进程?或者,如果我终止所有 Explorer 进程,这真的无关紧要吗?

编辑: 我并不是想获取正在运行的 Explorer 进程列表,但我想知道是否有可能正常退出 Explorer 进程,而不必使用 taskkill 突然终止/结束该进程。

答案1

你看!跨站点重复:)

我已经制作并上传尝试停止并启动 Explorer 的程序。它并不总是有效,这取决于 Explorer 的状态,但通常有效。:)

以下是(简短的)源代码,用以下语言编写

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);
}

希望有帮助!

答案2

尝试tasklist /FI "IMAGENAME eq explorer.exe"

请注意,如果您想查看系统或其他用户运行的任务,则可能需要运行此提升的权限。

相关内容