使用 C++ 执行 CMD 命令

问题描述 投票:0回答:1

我正在尝试使用此命令让我的电脑休眠

system("C:\\Windows\\System32\\psshutdown -d -t 0");

如果我使用 cmd 它工作正常,但是当我在 cpp 中运行它时,我得到了这个

'C:\Windows\System32\psshutdown' is not recognized as an internal or external command, operable program or batch file.
c++ windows cmd 32bit-64bit wow64
1个回答
2
投票

在 64 位 Windows 上运行的 32 位应用程序将置于 文件系统重定向 之下。因此,如果您的应用程序是 32 位应用程序,调用

system("C:\\Windows\\System32\\psshutdown -d -t 0");
将在
psshutdown.exe
中查找
C:\Windows\SysWOW64
并失败。你有一些解决方案:

  • 使用 Sysnative 访问真正的 System32 文件夹:

    system(R"(C:\Windows\Sysnative\psshutdown -d -t 0)");
    
  • 明确关闭文件系统重定向(一般应避免)

  • 或更好将您的应用程序重新编译为 64 位应用程序

© www.soinside.com 2019 - 2024. All rights reserved.