Start and Terminate an External Application

Hi there, I'm after a way of starting and terminating an external application using Visual Basic 6.  i don't need to do anything fancy like wait until the application has closed or anything like that, simply start the application and then close it.  However, when I close it I'd like to terminate the application completely, e.g. like with word if you've changed the document when you click the X it asks you if you wish to save the changes, the application I'm closing works similar, it basically asks you if you're sure you wish to close it, i'd like to bypass this when closing the application from VB and just terminate it.

Any help is greatly  appreciated, cheers.
LVL 4
ShelfieldCollegeAsked:
Who is Participating?
 
ShauliConnect With a Mentor Commented:
Locate two command buttons on your form, and copy this code:

'to start an application:
Private Sub Command1_Click()
Shell "yourpath&file.exe", vbMaximizedFocus
End Sub

'to terminate:
Public Function EndProcess(ByVal sbFilename As String) As String
    Dim Process As Variant
    EndProcess = "Can't terminate process :("
    For Each Process In GetObject("winmgmts:").ExecQuery("select * from Win32_Process")
        If LCase(Process.Name) = LCase(sbFilename) Then
            Process.Terminate
            EndProcess = Process.Name & " Terminated once and for all!"
        End If
    Next
End Function

Private Sub Command2_Click()
    MsgBox EndProcess("applicationnameonly.exe")   'just the application name, without the path
End Sub

S

0
 
Shiju SasidharanAssoc Project ManagerCommented:
'Code to Start an application
'=======================================
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_MAXIMIZE = 3
Private Const SW_NORMAL = 1
Private Const SW_HIDE = 0
Private Sub Command4_Click()
    StartApplication ("C:\winnt\notepad.exe")
End Sub

Private Sub StartApplication(ByVal ApplicationPath As String)

    Call ShellExecute(hWnd, "open", ApplicationPath, "", "", SW_MAXIMIZE)

End Sub
'=======================================

'Code to End an aplication

'==================================

http://www.vb-helper.com/howto_kill_application.html

'================================

;-)
Shiju
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Shiju SasidharanAssoc Project ManagerCommented:
0
 
ShelfieldCollegeAuthor Commented:
Thanks, your's seems to be the only one I could get working, I'm sure the others worked fine it's probably something I was doing wrong.  On a plus not your's will terminate every instance of that program which works out handy for a different projects I'm using so thanks again

Cheers

/Matt
0
 
ShauliCommented:
Glad it works for you.

S
0
All Courses

From novice to tech pro — start learning today.