c++ - 找不到可执行文件“C:\Windows\System32\Fodhelper.exe”
问题描述
我正在尝试从 C++ 程序中启动上述内置 Windows 可执行文件。首先,我可以确认该程序确实存在,位于路径“C:\Windows\System32\fodhelper.exe”
我尝试了 3 种不同的方法来运行这个程序:
System()
ShellExecuteW()
CreateProcessW()
这些方法都不起作用。我收到的错误是:The system cannot find the file specified.
由于我可以从开始菜单、运行框和 Windows 资源管理器中将这个可执行文件作为我常用的 Windows 帐户启动,我相信我的用户帐户确实具有运行该程序的权限。此外,我没有从我的代码中收到拒绝访问错误。无论如何,我以管理员身份运行 VS,但我仍然遇到同样的问题。
我相信我用来启动该过程的代码是正确的,因为相同的代码将cmd.exe
毫无问题地启动。见下文:
#include <Windows.h>
#include <tchar.h>
#include <iostream>
void CreateProcessMethod(LPCWSTR programPath) {
HRESULT result;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
ZeroMemory(&processInformation, sizeof(processInformation));
result = CreateProcessW(programPath, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInformation);
if (result == 0) {
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
/* Display error */
std::wcout << programPath << " not started: " << buf << std::endl;
}
else {
std::wcout << programPath << " started successfuly" << std::endl;
}
}
void ShellExecuteMethod(LPCWSTR programPath) {
SHELLEXECUTEINFOW shExecInfo = { 0 };
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFOW);
shExecInfo.fMask = SEE_MASK_FLAG_NO_UI;
shExecInfo.hwnd = nullptr;
shExecInfo.lpVerb = L"open";
shExecInfo.lpFile = programPath;
shExecInfo.lpParameters = L"\\C";
shExecInfo.nShow = SW_SHOWNORMAL;
if (ShellExecuteExW(&shExecInfo) == 0)
{
if (GetLastError() != ERROR_CANCELLED) // Operation canceled by the user
{
wchar_t buf[256];
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
/* Display error */
std::wcout << programPath << " not started: " << buf << std::endl;
}
}
else {
std::wcout << programPath << " started successfuly" << std::endl;
}
}
int main(){
CreateProcessMethod(L"C:\\Windows\\System32\\cmd.exe");
CreateProcessMethod(L"C:\\Windows\\System32\\fodhelper.exe");
ShellExecuteMethod(L"C:\\Windows\\System32\\cmd.exe");
ShellExecuteMethod(L"C:\\Windows\\System32\\fodhelper.exe");
}
请参阅以下程序的输出:
有没有人知道我在这里做错了什么?我找不到与此问题相关的任何信息。据我了解,尝试运行程序的代码是正确的,适用于不同的可执行文件。这也发生在三种不同的方法中。任何帮助将不胜感激。
解决方案
在 WOW64 上运行的 32 位应用程序将被置于文件系统重定向之下。因此,如果您的应用程序是 32 位应用程序,则路径"C:\\Windows\\System32\\fodhelper.exe"
将被重定向到C:\Windows\SysWOW64\fodhelper.exe
不存在的路径。你有一些解决方案:
- 用于
SysNative
访问真正的 system32 文件夹,这意味着您需要使用类似的东西system("C:\\Windows\\SysNative\\fodhelper.exe");
- 显式关闭文件系统重定向(一般应避免)
- 或者更好地将您的 exe 编译为 64 位应用程序。
推荐阅读
- ios - 为 iOS 构建,但在为 macOS 构建的目标文件中链接,文件 '.../Release-iphoneos/libRNVectorIcons.a' 用于架构 armv7
- apache-kafka - 如何为 kafka 消费者 java 应用程序编写性能测试?
- r - 基于R中的行ID合并许多数据框
- javascript - 尽管转义括号,但未终止的组
- git - 为什么我收到 Git 的 SSL 不受支持的协议错误?
- python - Python:图像处理创建皱纸效果
- groovy - Groovy 没有在 Jenkinsfile 中输入 If 条件
- javascript - Fontawesome Icon SVG 未在 Firefox 中显示 Heremap Marker
- linux - 执行 sed 命令时执行错误
- c# - C#用问号替换特殊字符