在qt中以父进程权限执行bat文件
我的可执行文件的当前工作路径中有一个名为 Store 的目录。在这个目录下,有一个bat文件,名为init.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();
}
I have a directory in the current working path of my executable which is called Store. In this directory, there is a bat file which is called init.bat. I have written the following code to run this file, but it seems CreateProcessW doesn't run the bat file. How should I fix this code? I didn't receive any error, the program just doesn't work and my bat file doesn't execute.
#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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Qt 中,您将使用 QProcess-API(请参阅 QProcess::start() )。使用 CreateProcess 不是执行此操作的 Qt 方法。
在链接的文档中,您将找到有关在 Windows 上通过 cmd 执行命令的提示以及其他操作系统的提示。
In Qt, you would use the QProcess-API (see QProcess::start()). Using CreateProcess is not the Qt way to do this.
In the linked documentation, you will find a hint on executing commands via cmd on Windows and hints for other OS.