在qt中以父进程权限执行bat文件
Execute a bat file with parent process privilege in qt
我的可执行文件的当前工作路径中有一个名为 Store 的目录。在这个目录下,有一个名为init.bat的bat文件。我已将以下代码写入 运行 这个文件,但 CreateProcessW 似乎没有 运行 bat 文件。我应该如何修复此代码?我没有收到任何错误,只是程序不工作,我的 bat 文件不执行。
#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QMessageBox>
#include <QDir>
#include <windows.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool status = FALSE;
QSplashScreen *splash_loader = new QSplashScreen;
splash_loader->setPixmap(QPixmap(":/new/prefix1/images/splash.png"));
splash_loader->show();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
QString path = QDir::toNativeSeparators(qApp->applicationDirPath());
path.append(L"\Store\init.bat");
// Execute requirement batch file
LPWSTR final_path = _wcsdup(path.toStdWString().c_str());
status = CreateProcessW(NULL, final_path, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
if(status && WaitForSingleObject(pi.hProcess, INFINITE))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
splash_loader->close();
}
MainWindow w;
w.show();
return a.exec();
}
在 Qt 中,您将使用 QProcess-API(参见 QProcess::start())。使用 CreateProcess 不是执行此操作的 Qt 方法。
在链接的文档中,您将找到有关通过 Windows 上的 cmd 执行命令的提示以及其他 OS.
上的提示
我的可执行文件的当前工作路径中有一个名为 Store 的目录。在这个目录下,有一个名为init.bat的bat文件。我已将以下代码写入 运行 这个文件,但 CreateProcessW 似乎没有 运行 bat 文件。我应该如何修复此代码?我没有收到任何错误,只是程序不工作,我的 bat 文件不执行。
#include "mainwindow.h"
#include <QApplication>
#include <QSplashScreen>
#include <QMessageBox>
#include <QDir>
#include <windows.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool status = FALSE;
QSplashScreen *splash_loader = new QSplashScreen;
splash_loader->setPixmap(QPixmap(":/new/prefix1/images/splash.png"));
splash_loader->show();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
QString path = QDir::toNativeSeparators(qApp->applicationDirPath());
path.append(L"\Store\init.bat");
// Execute requirement batch file
LPWSTR final_path = _wcsdup(path.toStdWString().c_str());
status = CreateProcessW(NULL, final_path, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
if(status && WaitForSingleObject(pi.hProcess, INFINITE))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
splash_loader->close();
}
MainWindow w;
w.show();
return a.exec();
}
在 Qt 中,您将使用 QProcess-API(参见 QProcess::start())。使用 CreateProcess 不是执行此操作的 Qt 方法。 在链接的文档中,您将找到有关通过 Windows 上的 cmd 执行命令的提示以及其他 OS.
上的提示