c 使用CreateProcess启动程序

有时候需要在程序中启动第三方程序,执行某个命令行之类的事情,也可以做个简单的启动程序启动浏览器之类的传入一些参数,下面来看个简单的例子:

创建一个命令行程序,启动程序的时候可能会遇到一些权限问题,在项目的属性里面可以设置启动的权限, 使用管理员权限

image

下面是个启动浏览器的简单示例

#include "stdafx.h"
#include 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中使用Windows API函数CreateProcess启动msu文件的步骤如下: 1. 在Qt项目中包含Windows.h头文件。 ```c++ #include <Windows.h> ``` 2. 定义CreateProcess函数需要的参数,包括待启动的msu文件路径和命令行参数。 ```c++ LPTSTR applicationName = L"C:\\Windows\\System32\\wusa.exe"; // msu文件启动程序路径 LPTSTR commandLine = L"C:\\temp\\update.msu /quiet /norestart"; // msu文件路径和命令行参数 LPSECURITY_ATTRIBUTES processAttributes = NULL; LPSECURITY_ATTRIBUTES threadAttributes = NULL; BOOL inheritHandles = FALSE; DWORD creationFlags = 0; LPVOID environment = NULL; LPTSTR currentDirectory = NULL; LPSTARTUPINFO startupInfo = new STARTUPINFO; LPPROCESS_INFORMATION processInformation = new PROCESS_INFORMATION; ZeroMemory(startupInfo, sizeof(STARTUPINFO)); ZeroMemory(processInformation, sizeof(PROCESS_INFORMATION)); startupInfo->cb = sizeof(STARTUPINFO); ``` 3. 调用CreateProcess函数启动msu文件。 ```c++ BOOL success = CreateProcess(applicationName, commandLine, processAttributes, threadAttributes, inheritHandles, creationFlags, environment, currentDirectory, startupInfo, processInformation); if (success) { qDebug() << "Process started"; } else { qDebug() << "Process failed to start"; } ``` 完整代码如下: ```c++ #include <QCoreApplication> #include <QDebug> #include <Windows.h> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); // Define CreateProcess parameters LPTSTR applicationName = L"C:\\Windows\\System32\\wusa.exe"; // msu file launch program path LPTSTR commandLine = L"C:\\temp\\update.msu /quiet /norestart"; // msu file path and command line parameters LPSECURITY_ATTRIBUTES processAttributes = NULL; LPSECURITY_ATTRIBUTES threadAttributes = NULL; BOOL inheritHandles = FALSE; DWORD creationFlags = 0; LPVOID environment = NULL; LPTSTR currentDirectory = NULL; LPSTARTUPINFO startupInfo = new STARTUPINFO; LPPROCESS_INFORMATION processInformation = new PROCESS_INFORMATION; ZeroMemory(startupInfo, sizeof(STARTUPINFO)); ZeroMemory(processInformation, sizeof(PROCESS_INFORMATION)); startupInfo->cb = sizeof(STARTUPINFO); // Start process BOOL success = CreateProcess(applicationName, commandLine, processAttributes, threadAttributes, inheritHandles, creationFlags, environment, currentDirectory, startupInfo, processInformation); if (success) { qDebug() << "Process started"; } else { qDebug() << "Process failed to start"; } // Cleanup delete startupInfo; CloseHandle(processInformation->hProcess); CloseHandle(processInformation->hThread); delete processInformation; return a.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值