![](/img/trans.png)
[英]The terminal process "C:\WINDOWS\System32\cmd.exe" terminated with exit code: 1
[英]Executable “C:\Windows\System32\Fodhelper.exe” not found
我正在嘗試從 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");
}
見下面程序的output:
有沒有人知道我在這里做錯了什么? 我找不到與此問題相關的任何信息。 據我了解,嘗試運行程序的代碼是正確的,適用於不同的可執行文件。 這也發生在三種不同的方法中。 任何幫助將不勝感激。
在 WOW64 上運行的 32 位應用程序將被置於文件系統重定向之下。 因此,如果您的應用程序是 32 位應用程序,則路徑"C:\\Windows\\System32\\fodhelper.exe"
將被重定向到不存在的C:\Windows\SysWOW64\fodhelper.exe
。 你有一些解決方案:
SysNative
訪問真正的 system32 文件夾,這意味着您需要使用類似system("C:\\Windows\\SysNative\\fodhelper.exe");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.