Avatar of Basicfarmer
Basicfarmer
Flag for United States of America asked on

Start and stop one app from another.

Hello Experts, I have two apps that will be running. These apps are finished and working correctly. What I need to do now is start App2 in the Form_Load event of App1. Then in the Form_Unload event of App1 I want to make sure I stop App2. How do I do this?

Thanks...
Visual Basic Classic

Avatar of undefined
Last Comment
Mike Tomlinson

8/22/2022 - Mon
Muhammad Khan

String is not a problem.. you can do it using Shell command

Shell "c:\myApp2.exe"

the problem comes when you want to close it... for that you need to use Windows API...
ASKER CERTIFIED SOLUTION
Muhammad Khan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Basicfarmer

ASKER
Thanks for the quick response and great answer...
Mike Tomlinson

Here is a simple example of how to kill something started with Shell():
Option Explicit
 
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
 
Private Sub Command1_Click()
    Dim taskID As Double
    taskID = Shell("notepad.exe", vbNormalFocus)
 
    ' ... code ...
 
    KillShell taskID
End Sub
 
Private Sub KillShell(ByVal taskID As Double)
    On Error Resume Next
 
    Dim hProcess As Long
    Dim lExitCode As Long
    Dim lRet As Long
 
    If taskID <> 0 Then
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, taskID)
        If hProcess <> 0 Then
            GetExitCodeProcess hProcess, lExitCode
            If lExitCode <> 0 Then
                TerminateProcess hProcess, lExitCode
            End If
        End If
    End If
End Sub

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61