简体   繁体   English

如何通过C ++启动explorer.exe?

[英]How can I start explorer.exe via C++?

I'm trying to programmatically start explorer.exe but I'm not having any luck. 我正在尝试以编程方式启动explorer.exe,但我没有运气。

This is my code: 这是我的代码:

cout << pName << "died, lets restart it." << endl;
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(startupInfo);

PROCESS_INFORMATION processInformation;

if(CreateProcess(pName, NULL, NULL, NULL, false, NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInformation) == 0){
    cout << "Error starting " << pName << ": " << GetLastError() << endl;
}

and pName is explorer.exe 和pName是explorer.exe

Can someone tell me what I'm doing wrong? 有人能告诉我我做错了什么吗? I get the error code '2' which is ERROR_FILE_NOT_FOUND 我得到错误代码'2',即ERROR_FILE_NOT_FOUND

The first parameter is the application name; 第一个参数是应用程序名称; the second is the command line. 第二个是命令行。 Try specifying "explorer.exe" as the second parameter. 尝试将“explorer.exe”指定为第二个参数。

See this MSDN article : 请参阅此MSDN文章

lpApplicationName [in, optional] lpApplicationName [in,optional]

The name of the module to be executed. 要执行的模块的名称。 This module can be a Windows-based application. 该模块可以是基于Windows的应用程序。 It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer. 如果本地计算机上有适当的子系统,它可以是其他类型的模块(例如,MS-DOS或OS / 2)。

The string can specify the full path and file name of the module to execute or it can specify a partial name. 该字符串可以指定要执行的模块的完整路径和文件名,也可以指定部分名称。 In the case of a partial name, the function uses the current drive and current directory to complete the specification. 对于部分名称,该函数使用当前驱动器和当前目录来完成规范。 The function will not use the search path . 该功能不会使用搜索路径 This parameter must include the file name extension; 此参数必须包含文件扩展名; no default extension is assumed. 没有默认的扩展名。

You probably should give " ShellExecuteEx " a try. 您可能应该尝试“ ShellExecuteEx ”。 This function lets you specify a file or folder and a verb that describes what to do with it. 此功能允许您指定文件文件夹以及描述如何处理它的动词 If you use " explore " as the verb , it will open Windows Explorer with the given folder . 如果您使用“ explore ”作为动词 ,它将打开带有给定文件夹的 Windows资源管理器。

It's surprisingly hard to find relevant information on how to reliably restart windows explorer. 很难找到有关如何可靠地重启Windows资源管理器的相关信息。 On 64-bit Windows 7/8, the ShellExecute method does not work properly and leads to things such as file copying and icon overlays being completely broken. 在64位Windows 7/8上,ShellExecute方法无法正常工作,导致文件复制和图标叠加等操作完全中断。

The most reliable way seems to use stdlib.h system call: 最可靠的方法似乎是使用stdlib.h系统调用:

system("start explorer");

If you are trying to shutdown and restart explorer, you might want to programmatically disable AutoRestartShell registry key, which prevents you from controlling when explorer is restarted. 如果您尝试关闭并重新启动资源管理器,则可能需要以编程方式禁用AutoRestartShell注册表项,这会阻止您重新启动资源管理器进行控制。

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

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