微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在一个Windowsshell扩展,不幸的是,当对DLL进行更改时,我必须重新启动Windows资源pipe理器(因为它保持在内存中的DLL)。

我从迪诺埃斯波西托发现了这个节目,但它对我不起作用。

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

有人可以分享一些东西吗?

PS我意识到,我可以去任务pipe理器,并杀死探险家的过程,但我只是想这样做的懒惰的方式。 此外,这使自动化。

如何获取调用过程Windows用户访问令牌

C + + QT与C#.NET的Windows开发

WindowsPrincipal.IsInRole考虑到组内的组?

重命名正在运行的可执行文件(exe)文件

我应该在Windows消息框中使用警告图标还是问号图标?

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

托pipeC ++方法命名

命令行窗口编译器(cl.exe)目标

testing“我的文档”文件夹redirect

在Windows上进行蓝牙开发

如何获得一个Windows服务的命令行参数?

一个不错的解决方案:

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

在解析了一些较早的答案并做了一些研究之后,我在C#中创建了一个完整的示例。 这将关闭浏览器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}","explorer.exe"); Process process = new Process(); process.StartInfo.FileName = explorer; process.StartInfo.UseShellExecute = true; process.Start();

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

FindWindow之后使用GetwindowThreadProcessId,然后OpenProcess,然后TerminateProcess。

经过一些Google搜索之后,我想出了以下C#解决方案:

using System.Diagnostics; ... static public void RestartExplorer() { foreach(Process p in Process.GetProcesses()) { if(p.Mainmodulee.moduleeName.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,TEXT("explorer.exe"),SW_HIDE);

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

注意:使用TerminateProcess似乎极端,但是发布一个WM_CLOSE到资源管理器会激发一个windows关机对话框。

这是Windows 7/8(需要测试,甚至可以在Vista上运行)。

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

所以我测试了下面的工作,效果很好。

PostMessage(FindWindow('Shell_TrayWnd'),nil),WM_USER+436,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}",explorer); foreach (Process process in Process.GetProcesses()) { // In case we get Access Denied try { if (string.Compare(process.Mainmodulee.FileName,explorerPath,StringComparison.OrdinalIgnoreCase) == 0) { process.Kill(); } } catch { } } Process.Start(explorer); }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐