【问题标题】:Executable "C:\Windows\System32\Fodhelper.exe" not found找不到可执行文件“C:\Windows\System32\Fodhelper.exe”
【发布时间】:2021-05-21 13:39:16
【问题描述】:

我正在尝试从 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");

    
}

查看下面程序的输出:

有没有人知道我在这里做错了什么?我找不到与此问题相关的任何信息。据我了解,尝试运行程序的代码是正确的,适用于不同的可执行文件。这也发生在三种不同的方法中。任何帮助将不胜感激。

【问题讨论】:

  • 您是否在 64 位 Windows 上以 32 位模式编译?
  • 我可能是,您能否解释一下这将如何导致此错误?我也会调查一下,如果这是问题,它并没有给我带来任何问题,因此我很感兴趣为什么这是一个问题。
  • phuclv 解释它

标签: c++ redirect wow64


【解决方案1】:

在 WOW64 上运行的 32 位应用程序将放在 file system redirection 下。因此,如果您的应用程序是 32 位应用程序,则路径 "C:\\Windows\\System32\\fodhelper.exe" 将被重定向到不存在的 C:\Windows\SysWOW64\fodhelper.exe。您有一些解决方案:

  • 使用SysNative来访问真正的system32文件夹,这意味着你需要使用system("C:\\Windows\\SysNative\\fodhelper.exe");之类的东西
  • 明确关闭文件系统重定向(一般应避免)
  • 或者更好地将您的 exe 编译为 64 位应用程序。

【讨论】:

  • 完美。该解决方案对我有用。菜鸟失误!很好的解释,带有解释性链接。
  • 暂时禁用 FS 重定向器没有错。使用SysNative正确的主要问题是你不应该硬编码C:\Windows,你应该通过GetWindowsDirectory()SHGetFolderPath(CSIDL_WINDOWS)或@询问Windows实际安装的位置987654330@,然后将SysNative\fodhelper.exe 附加到该路径。或使用ExpandEnvironmentStrings("%windir%\SysNative\fodhelper.exe")。不是每个人都安装在C:\Windows
  • @RemyLebeau 是真的。文件系统重定向的存在正是因为程序员硬编码路径,如 windows、system32...
  • 我编译为 x64 可执行文件并解决了我的问题,但是我可以欣赏代码稳定性,最好从 Win32api 请求 %windir% 而不是假设它驻留在哪里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2021-10-31
相关资源
最近更新 更多