使用VBScript启动应用程序

时间:2016-05-05 14:18:24

标签: shell vbscript

我正在尝试使用VBScript从Windows shell启动应用程序。从QtCreator运行时,应用程序运行时没有错误。但是,从VBScript运行时它会崩溃,并以错误代码255退出。 这是脚本:

Set objShell = WScript.CreateObject("WScript.Shell")

rv = objShell.Run("path\to\application.exe", 1 , True)
If rv <> 0 Then
    MsgBox "Failed : " & rv
End If
WScript.Sleep 120000
objShell.Run "taskkill /im path\to\application.exe"

Set objShell = Nothing

有人可以指出我错过了什么吗?

4 个答案:

答案 0 :(得分:3)

Shell.Exec可以运行您的应用程序并终止它:

Set objShell = WScript.CreateObject("WScript.Shell")

Set rv = objShell.Exec ("path\to\application.exe")
If rv <> 0 Then
    MsgBox "Failed : " & rv
End If
WScript.Sleep 120000

rv.Terminate

Set objShell = Nothing

答案 1 :(得分:1)

试试这个vbscript并告诉我结果:

Option Explicit
Dim Title,objShell,rv,ProcessPath,ProcessName
Title = "Launching and killing application using Vbcript"
Set objShell = CreateObject("WScript.Shell")
ProcessPath = "C:\Windows\system32\Calc.exe"
rv = objShell.Run(DblQuote(ProcessPath),1,False)
If rv <> 0 Then
    MsgBox "Failed : " & rv
End If
Set objShell = Nothing
WScript.Sleep 12000
ProcessPath = Split(ProcessPath,"\")
ProcessName = ProcessPath(UBound(ProcessPath))
Msgbox "The Process named "& DblQuote(ProcessName) &" is being to be killed now !",_
vbExclamation,Title
Call Kill(ProcessName)
'****************************************************
Sub Kill(ProcessName)
    Dim Ws,Command,Execution
    Set Ws = CreateObject("Wscript.Shell")
    Command = "cmd /c Taskkill /F /IM "& DblQuote(ProcessName) &""
    Execution = Ws.Run(Command,0,True)
    Set Ws = Nothing
End Sub 
'****************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************************

答案 2 :(得分:1)

尝试这种方式:

source /etc/profile.d/rvm.sh

答案 3 :(得分:0)

我能够找到错误。我将当前目录设置为包含.exe文件的文件夹。这是修改后的脚本:

Option Explicit
Dim Title,objShell,rv,ProcessPath,ProcessName
Title = "Launching and killing application using Vbcript"
Set objShell = CreateObject("WScript.Shell")
objShell.CurrentDirectory = "path\to\folder\containing\.exe"
ProcessPath = "path\to\application.exe"
objShell.Run DblQuote(ProcessPath),1,False
If rv <> 0 Then
    MsgBox "Failed : " & rv
End If
Set objShell = Nothing
WScript.Sleep 12000
ProcessPath = Split(ProcessPath,"\")
ProcessName = ProcessPath(UBound(ProcessPath))
Msgbox "The Process named "& DblQuote(ProcessName) &" is being to be killed now !",_
vbExclamation,Title
Call Kill(ProcessName)

Sub Kill(ProcessName)
    Dim Ws,Command,Execution
    Set Ws = CreateObject("Wscript.Shell")
    Command = "cmd /c Taskkill /F /IM "& DblQuote(ProcessName) &""
    Execution = Ws.Run(Command,0,True)
    Set Ws = Nothing
End Sub 

Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function