调用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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值