Start and Terminate an External Application

Posted on 2005-04-15
Last Modified: 2010-05-02
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.
Question by:ShelfieldCollege
    LVL 53

    Expert Comment

    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    '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



    LVL 14

    Expert Comment

    by:Shiju Sasidharan
    LVL 19

    Accepted Solution

    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
                EndProcess = Process.Name & " Terminated once and for all!"
            End If
    End Function

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


    LVL 4

    Author Comment

    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


    LVL 19

    Expert Comment

    Glad it works for you.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    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

    8 Experts available now in Live!

    Get 1:1 Help Now