Solved

Killing an external app called with ShellExecute

Posted on 2003-11-12
7
6,673 Views
Last Modified: 2007-12-19
Hi all;

Ok, big points for a quick response...

I opened an SSH tunnel from within VB with the following:

ShellExecute Me.hwnd, "Open", "C:/plink.exe", "-ssh -l sshtunnel -pw 12345 -L 3306:127.0.0.1:3306 -batch www.myhost.com", "c:\", 0

I would not like to be able to kill it when it's dead. Any solutions?
0
Comment
Question by:Squeebee
7 Comments
 

Expert Comment

by:MichIT
ID: 9735028
instead of shell execute, you want to use a system.diagnostic.process

Here is an example of opening explorer letting it go for 30000 mil, and closing it (use it to refresh cache of an asp.net site after a bulk upload of data)

        Dim IE_Process As New System.Diagnostics.Process
        IE_Process = Process.Start("iexplore", "http://localhost/freightfinder/Cache_Renew.aspx")
        System.Threading.Thread.Sleep("30000")
        IE_Process.CloseMainWindow()

or if you dont want to actually close the process, but just wait for it to finish (use this to run dts script and wait for them to execute):
       
        Dim DTS_Process As New System.Diagnostics.Process
        DTS_Process = Process.Start("C:\Program Files\Microsoft SQL Server\80\Tools\Binn\dtsrun.exe", "/E /S127.0.0.1 /NLISTING_Import")
        DTS_Process.WaitForExit()
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 9735291
If you use Shell you could use this function:

http://www.freevbcode.com/ShowCode.Asp?ID=670
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9735337
Are you using VB6 or .NET?

If VB6, I can show you how to the use the CreateProcess and TerminateProcess API's to accomplish this as well.

Idle_Mind
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 17

Author Comment

by:Squeebee
ID: 9735580
I use VB6. I'm not sure I can use shell because of the arguments. Anyway, I am open to VB6 suggestions.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9735716
Create a new project and add a two command buttons and timer to the main form.  Also add a module.

Paste the code below into the areas as they are labeled.  The first button will launch notepad.  At least it does on my system.  You can change the cmd string to whatever it is you want.  The second button will kill the launched application.  The program will also automatically detect if the application has closed within 100 milliseconds and react accordingly.

Run the app and try using the kill button and/or closing the launched app to see what happens.

' Code for the main form....
Private Sub Form_Load()
    Timer1.Enabled = False
    Timer1.Interval = 100
    Command1.Caption = "Start"
    Command2.Caption = "Kill"
    Command2.Enabled = False
End Sub

Private Sub Command1_Click()
    Dim cmd As String
    cmd = "c:\windows\notepad.exe"
    'cmd = "C:/plink.exe -ssh -l sshtunnel -pw 12345 -L 3306:127.0.0.1:3306 -batch www.myhost.com"
    If RunProcess(cmd, SW_NORMAL) Then
        Command1.Enabled = False
        Timer1.Enabled = True
        Command2.Enabled = True
    Else
        MsgBox "Unable to start command: " & cmd
    End If
End Sub

Private Sub Command2_Click()
    lngReply = TerminateProcess(lngProcess, 0&)
    resetApp
End Sub

Private Sub Timer1_Timer()
    lngReply = WaitForSingleObject(lngProcess, 0)
    If lngReply <> 258 Then ' 258 means the process is still running
        resetApp
    End If
End Sub

Private Sub resetApp()
    Timer1.Enabled = False
    lngReply = CloseHandle(lngProcess)
    lngReply = CloseHandle(lngThread)
    Command1.Enabled = True
    Command2.Enabled = False
End Sub


' Code for the module...
Option Explicit

Private Const STARTF_USESHOWWINDOW = &H1
Public Enum enSW
    SW_HIDE = 0
    SW_NORMAL = 1
    SW_MAXIMIZE = 3
    SW_MINIMIZE = 6
End Enum

Global lngProcess As Long
Global lngThread As Long
Global lngProcessID As Long
Global lngThreadID As Long
Global lngReply As Long

Public Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Byte
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
End Type

Public Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type

Public Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Declare Function GetExitCodeProcess Lib "kernel32" _
      (ByVal hProcess As Long, lpExitCode As Long) As Long

Public Declare Function CreateProcess Lib "kernel32" Alias _
    "CreateProcessA" (ByVal lpApplicationName As String, ByVal _
    lpCommandLine As String, lpProcessAttributes As Any, _
    lpThreadAttributes As Any, ByVal bInheritHandles As Long, ByVal _
    dwCreationFlags As Any, lpEnvironment As Any, ByVal _
    lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, _
    lpProcessInformation As PROCESS_INFORMATION) As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As _
    Long) As Long

Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess _
    As Long, ByVal uExitCode As Long) As Long
   
Global Const NORMAL_PRIORITY_CLASS = &H20&
Global pInfo As PROCESS_INFORMATION
Global sInfo As STARTUPINFO
Global sNull As String

Public Function RunProcess(sCmd As String, start_size As enSW) As Boolean
    'Use the CreateProcess API to start the job. This will give us
    'Process,Thread,ProcessID and ThreadID handles
    sInfo.cb = Len(sInfo)
    sInfo.dwFlags = STARTF_USESHOWWINDOW
    sInfo.wShowWindow = start_size
    lngReply = CreateProcess(sNull, sCmd, ByVal 0&, ByVal 0&, 1&, _
        NORMAL_PRIORITY_CLASS, ByVal 0&, sNull, sInfo, pInfo)
   
    If lngReply = 0 Then
        RunProcess = False
    Else
        lngProcessID = pInfo.dwProcessId
        lngThreadID = pInfo.dwThreadId
        lngProcess = pInfo.hProcess
        lngThread = pInfo.hThread
       
        'Check every 100 milliseconds if the job is finished
        'Any job started with CreateProcess must call CloseHandle for both the
        'Process and Thread handles. See Timer1 on frmMain
       
        RunProcess = True
    End If
End Function


Regards,

Idle_Mind
0
 
LVL 17

Author Comment

by:Squeebee
ID: 9735769
Thanks, I will try that when I get home and get back to you.
0
 
LVL 1

Accepted Solution

by:
atp_expert earned 500 total points
ID: 9747713
Try to invioke the executable using shellexecuteex, and terminate the process using Terminateprocess.
Cos, the sheellexecuteex gives the processid, which can used to kill the process
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now