调用API函数FindExecutable和CreateProcess时需要注意的问题。

下面是一个打开指定文件的函数,用到了API函数FindExecutable、CreateProcess和ShellExecute,需要注意的是,通过FindExecutable找到的关联文件字符串strResult在返回时,后面会有很多null(asc=0),不把这些null去掉就无法打开指定的文件,只会打开一个空的文件,所以要写一个专门去掉null的函数,不知道VB里面有没有这样的函数 :)。

Public Function GF_lSpawn(ByVal sFileName As String,  Optional ByVal bWait As Boolean = True) As Long
    Dim strResult       As String * 260
    Dim lResult         As Long
    Dim start           As STARTUPINFO
    Dim proc            As PROCESS_INFORMATION
    Dim rc              As Long
    Dim strCommandLine  As String
   
    On Error GoTo ErrProc
    
    lResult = FindExecutable(sFileName, "", strResult)
   
    If Asc(Left(strResult, 1)) = 0 Then
        MsgBox "没有可以打开" & sFileName & "文件的软件!"
        Exit Function
    End If
   
    If bWait Then
        start.cb = Len(start)
        start.lpReserved = vbNullString
        start.cbReserved2 = 0
        start.lpReserved2 = 0
        strCommandLine = My_Trim(strResult) + " " + My_Trim(sFileName)
        rc = CreateProcess(vbNullString, strCommandLine, ByVal 0, ByVal 0, False, NORMAL_PRIORITY_CLASS, ByVal 0, vbNullString, start, proc)
        
        rc = WaitForSingleObject(proc.hThread, INFINITE)
        
        rc = CloseHandle(Proc.hThread)
    Else
        ShellExecute 0, "open", strFileName, "", "", SW_SHOWNORMAL
    End If
   
    GF_lSpawn = rc
   
    Exit Function
   
ErrProc:
   
    GF_lSpawn = 0
    MsgBox "打开文件出错,错误信息:" + Err.Description
   
End Function

Public Function My_Trim(ByVal strSrc As String) As String
    Dim i           As Integer
    Dim str         As String
    Dim strDesc     As String
   
    str = ""
    strDesc = ""
   
    For i = 1 To Len(strSrc)
        str = Mid(strSrc, i, 1)
        If Asc(str) <> 0 Then
            strDesc = strDesc + str
        End If
    Next i
   
    My_Trim = strDesc
   
End Function

  • 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、付费专栏及课程。

余额充值