繁体   English   中英

如何以编程方式重新启动 Windows 资源管理器进程

[英]How to programmatically restart windows explorer process

我正在开发 Windows shell 扩展,不幸的是,在更改 DLL 时,我必须重新启动 Windows 资源管理器(因为它将 DLL 保存在内存中)。

我从 Dino Esposito 找到了这个程序,但它对我不起作用。

void SHShellRestart(void)
{
    HWND hwnd;
    hwnd = FindWindow("Progman", NULL );
    PostMessage(hwnd, WM_QUIT, 0, 0 );
    ShellExecute(NULL, NULL, "explorer.exe", NULL, NULL, SW_SHOW );
    return;
}

有没有人可以分享一些东西来做到这一点?

PS我意识到我可以去任务管理器并杀死资源管理器进程,但我只是想以懒惰的方式来做。 此外,这可以实现自动化。

PPS 我使用 .NET 进行开发,但 shell 重新启动功能可能是 C、C++ 或 .NET 语言。 它只是一个小的独立可执行文件。

在解析了一些早期的答案并进行了一些研究之后,我在 C# 中创建了一个完整的小示例。 这将关闭 explorer shell,然后等待它完全关闭并重新启动它。 希望这会有所帮助,这个线程中有很多有趣的信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace RestartExplorer
{
class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    const int WM_USER = 0x0400; //http://msdn.microsoft.com/en-us/library/windows/desktop/ms644931(v=vs.85).aspx

    static void Main(string[] args)
    {
        try
        {
            var ptr = FindWindow("Shell_TrayWnd", null);
            Console.WriteLine("INIT PTR: {0}", ptr.ToInt32());
            PostMessage(ptr, WM_USER + 436, (IntPtr)0, (IntPtr)0);

            do
            {
                ptr = FindWindow("Shell_TrayWnd", null);
                Console.WriteLine("PTR: {0}", ptr.ToInt32());

                if (ptr.ToInt32() == 0)
                {
                    Console.WriteLine("Success. Breaking out of loop.");
                    break;
                }

                Thread.Sleep(1000);
            } while (true);
        }
        catch (Exception ex)
        {
            Console.WriteLine("{0} {1}", ex.Message, ex.StackTrace);
        }
        Console.WriteLine("Restarting the shell.");
        string explorer = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe");
        Process process = new Process();           
        process.StartInfo.FileName = explorer;
        process.StartInfo.UseShellExecute = true;
        process.Start();

        Console.ReadLine();

    }
}
}

我注意到没有人解决将 explorer.exe 作为 shell 启动的问题,而不仅仅是打开资源管理器窗口。 我花了一段时间才弄清楚这一点,结果很简单:

string explorer = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe");
        Process process = new Process();
        process.StartInfo.FileName = explorer;
        process.StartInfo.UseShellExecute = true;
        process.Start();

您必须将 StartInfo.UseshellExecute 设置为 true 才能使其作为 shell 重新启动。

一个万无一失的解决方案:

foreach (Process p in Process.GetProcesses())
{
    // In case we get Access Denied
    try
    {
        if (p.MainModule.FileName.ToLower().EndsWith(":\\windows\\explorer.exe"))
        {
            p.Kill();
            break;
        }
    }
    catch
    { }
}
Process.Start("explorer.exe");

FindWindow 之后使用 GetWindowThreadProcessId,然后是 OpenProcess,然后是 TerminateProcess。

经过一番谷歌搜索后,我想出了以下 C# 解决方案:


using System.Diagnostics;
...
static public void RestartExplorer()
{
    foreach(Process p in Process.GetProcesses())  {
       if(p.MainModule.ModuleName.contains("explorer") == true)
         p.Kill();
    }
    Process.Start("explorer.exe");
}

这适用于我在 Vista 上:

DWORD dwPID;
HANDLE hExp;
HWND hSysTray = ::FindWindow (TEXT("Shell_TrayWnd"), NULL) ;
GetWindowThreadProcessId (hSysTray, &dwPID);
hExp = OpenProcess (PROCESS_TERMINATE, FALSE, dwPID);

if (hExp)
{
   TerminateProcess (hExp, 0);
}
Sleep (2000);
ShellExecute (NULL, NULL, TEXT("explorer.exe"), NULL, NULL, SW_HIDE);

但我找不到任何方法来抑制打开的探索窗口(我试过了,因此是 SW_HIDE)。 在 Vista 上,不带参数运行 explorer.exe 似乎与在早期系统上运行“explorer.exe /e”相同。 你必须自己在 XP 上尝试一下,我这里没有。

注意:使用 TerminateProcess 看起来确实很极端,但是将 WM_CLOSE 发布到资源管理器会引发 Windows 关闭对话框。

public static void RestartExplorer()
{
    using(Process process = new Process())
    {
        //有些系统有tskill.exe可以直接调用tskill explorer命令
        process.StartInfo = new ProcessStartInfo
        {
            FileName = "taskkill.exe",
            Arguments = "-f -im explorer.exe",
            WindowStyle = ProcessWindowStyle.Hidden
        };
        process.Start();
        process.WaitForExit();
        process.StartInfo = new ProcessStartInfo("explorer.exe");
        process.Start();
    }
}

这适用于 Windows 7/8(需要测试,甚至可能适用于 Vista)。

由于有一种关闭 Windows 7 和 8 中包含的资源管理器(progman)的正确方法- 通过在按 Ctrl-Shift 的同时右键单击任务栏(Win8 中的 Shell_TrayWnd 或 Win7 上的 StartMenu),它会在弹出菜单中显示一个隐藏的关闭选项Explorer ,并使用 Spy++ 挖掘它是由消息WM_USER+436触发的。

因此,我进行了测试并执行了以下操作,效果很好。

PostMessage(FindWindow('Shell_TrayWnd'),nil),WM_USER+436,0,0);

它关闭资源管理器,以及所有打开的实例。 要重新启动资源管理器,请使用上面提供的方法。

因此,请在评论中确认这是否适用于您的 windows vista/7/8 或任何其他版本的 32 位/64 位版本。

AC# 解决方案提供了更多确定“正确”资源管理器进程被杀死的可能性。

using System;
using System.Diagnostics;

...............

public static void RestartExplorer()
 {
 const string explorer = "explorer.exe";
 string explorerPath = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), explorer);
 foreach (Process process in Process.GetProcesses())
  {
  // In case we get Access Denied
  try
   {
   if (string.Compare(process.MainModule.FileName, explorerPath, StringComparison.OrdinalIgnoreCase) == 0)
    {
    process.Kill();
    }
   }
  catch
   {
   }
  }
 Process.Start(explorer);
 }

与其尝试重新启动资源管理器,不如快速刷新它,它应该具有类似的效果。

[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

public static void RefreshExplorer()
{
    SHChangeNotify(0x08000000, 0x00000000, IntPtr.Zero, IntPtr.Zero);
}

只需调用 RefreshExplorer(),它就会刷新资源管理器。

SHChangeNotify 接受 eventId 作为第一个参数。 在上面的示例中,它通知文件关联已更改,但您可以针对特定事件执行此操作。 此处的文档中提到了所有 eventId 常量。 您可以在magnumdb中找到它们的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM