Qt5:使用QProcess :: startDetached调用.bat文件在System32中找不到程序 [英] Qt5: Calling .bat file with QProcess::startDetached doesn't find program in System32

查看:935
本文介绍了Qt5:使用QProcess :: startDetached调用.bat文件在System32中找不到程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有按钮的Qt GUI小型程序,它可以启动.bat文件.

I have a small Qt GUI program with a button, that starts a .bat file.

调用.bat的重要部分是:

The important part to call the .bat is this:

void MainWindow::on_pushButton_clicked()
{
    int r = QMessageBox::warning(this, tr("Shutdown System"),
    tr("Do you want to close the program and the system?"),
    QMessageBox::Yes | QMessageBox::Default,
    QMessageBox::No,
    QMessageBox::Cancel | QMessageBox::Escape);

    if (r == QMessageBox::Yes){
        QProcess::startDetached("cmd /C shutdown_all.bat");
    }
}

我打算致电位于C:\Windows\System32\psshutdown.exe PSShutdown . ,当然还有PATH.

I intend to call PSShutdown, located in C:\Windows\System32\psshutdown.exe, and of course in the PATH.

出于测试目的,我将其放在.bat中:

I put this in the .bat for testing purposes:

cd C:\Windows\System32
psshutdown
PAUSE

当单独调用.bat文件时,似乎可以找到该程序并打印有关它的信息,但是当我使用该按钮调用该程序时,它找不到psshutdown.exe

When the .bat file is called alone, seems to find the program and print info about it, but when I use the button to call the program, it can't find psshutdown.exe

结果捕获:

使用Qt程序调用.bat:

Calling .bat with Qt program:

双击调用.bat(预期结果):

Calling .bat with double click (expected result):

在.bat中添加echo %PATH%时,两个版本中的路径似乎也都可以打印.知道有什么问题吗?

The path also seems to be printed Ok in both versions when adding echo %PATH% to the .bat. Any idea what could be wrong?

这已在两台不同的计算机上以Qt 5.5和5.7(调试和发布模式)进行了测试.

This has been tested with Qt 5.5 and 5.7, debug and release mode, in two different computers.

如果需要,我可以提供完整的源代码,但这只是使用Qt Creator中的模板的带有添加按钮的Qt Widgets Aplication.

I can provide the full source code if needed, but it's just a Qt Widgets Aplication with an added button, using the template in Qt Creator.

推荐答案

psshutdown.exe是Windows上通常不存在的可执行文件.它是来自 Windows Sysinternals 的工具,该工具需要手动下载并复制到任何目录.

psshutdown.exe is an executable which usually does not exist on Windows. It is a tool from Windows Sysinternals which needs to be downloaded and copied manually to any directory.

在64位Windows上,64位应用程序访问%SystemRoot%\System32中的64位应用程序和动态链接库.

On 64-bit Windows 64-bit applications accesses the 64-bit applications and dynamic linked libraries in %SystemRoot%\System32.

但是Windows 文件系统重定向器在访问%SystemRoot%\System32来运行可执行文件或加载DLL时,将在64位Windows上运行的x86应用程序重定向到32位系统目录%SystemRoot%\SysWOW64.

But Windows File System Redirector redirects x86 applications running on 64-bit Windows to the 32-bit system directory %SystemRoot%\SysWOW64 on accessing %SystemRoot%\System32 for running an executable or loading a DLL.

一种解决方案是在批处理文件中使用以下代码:

One solution is using following code in batch file:

if exist %SystemRoot%\System32\psshutdown.exe (
    %SystemRoot%\System32\psshutdown.exe
) else if exist %SystemRoot%\Sysnative\psshutdown.exe (
    %SystemRoot%\Sysnative\psshutdown.exe
) else if %SystemRoot%\SysWOW64\psshutdown.exe (
    %SystemRoot%\SysWOW64\psshutdown.exe
) else (
    echo Error: Can't find psshutdown.exe in system directory of Windows.
    pause
)

此批处理代码独立于将Qt应用程序编译为32位和64位Windows的x64或x86应用程序,并且独立于以前将系统目录psshutdown.exe复制到其中的工作.

This batch code works independent on compiling the Qt application as x64 or x86 application for 32-bit and 64-bit Windows and independent into which system directory psshutdown.exe was copied before.

第一个 IF 条件适用于仅具有System32的32位Windows上的32位应用程序,以及具有64位System32和32- SysWOW64位.

The first IF condition is for 32-bit applications on 32-bit Windows with only System32 and 64-bit applications on 64-bit Windows with 64-bit System32 and 32-bit SysWOW64.

第二个 IF 条件是在64位Windows上用于访问64位System32的32位应用程序.

The second IF condition is for 32-bit applications on 64-bit Windows for accessing 64-bit System32.

第三个 IF 条件是在64位Windows上用于访问32位SysWOW64的64位应用程序.

The third IF condition is for 64-bit applications on 64-bit Windows for accessing 32-bit SysWOW64.

另一种解决方案是使用Windows资源管理器之类的64位文件管理器将文件psshutdown.exe复制到两个目录中

Another solution is copying the file psshutdown.exe with a 64-bit file manager like Windows Explorer to the two directories

%SystemRoot%\System32
%SystemRoot%\SysWOW64

这篇关于Qt5:使用QProcess :: startDetached调用.bat文件在System32中找不到程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆