2

I am trying to launch an application from the Windows shell using VBScript. The application runs without errors when run from QtCreator. However, it crashes when run from VBScript and exits with error code 255. Here's the script:

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

Could someone please point out what I am missing?

user2522981
  • 163
  • 3
  • 18
  • Which line causes the crash? If you are not able to step through it with a debugger, add some msgbox statements before and after key commands to find out where it is crashing. That info will be a big help in tracking it down. – Gary_W May 05 '16 at 14:56
  • By the way, I used `'C:\Windows\System32\calc.exe'` for the app (with a shorter sleep time for testing) and it worked ok on 64-bit Windows 7. Note though the the "True" argument on the first call to run() means wait for the app to finish, so there will be nothing to kill later, unless another process started it? Killing by name could be dangerous. I believe there's a way to start a process, then save it's Process ID (PID). After the sleep, kill by the PID to ensure you are killing the process you started, not another. Much safer that way. – Gary_W May 05 '16 at 15:29
  • See this post https://stackoverflow.com/questions/35298200/taskkill-exit-code-255, 255 could mean there's a problem with the path. Turn off `On Error Resume Next` if you have it on. – langstrom May 05 '16 at 15:42

3 Answers3

1

Give a try for this vbscript and tell me the result :

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
'****************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1

Try This way :

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

    rv = objShell.Run(chr(34)&"c:\windows\system32\Mspaint.exe"&chr(34), 1 , False)
    If rv <> 0 Then
        MsgBox "Failed : " & rv
    End If
    WScript.Sleep 2000
    objShell.Run  "taskkill /f /im ""Mspaint.exe"" ",0,False

    Set objShell = Nothing
hollopost
  • 569
  • 9
  • 28
0

I was able to find the error. I set the current directory to the folder that contains the .exe file.This is the modified script:

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
user2522981
  • 163
  • 3
  • 18