Link to home
Start Free TrialLog in
Avatar of MarcGraff
MarcGraffFlag for United States of America

asked on

Close a program in VB6?

Is there a way to close a program in VB6 with only knowing the file name "AL-Admin.exe"?

I have a way to close by caption but not by file name.

Public Sub CloseProgram(ByVal Caption As String)
    Dim Handle As Long
    Handle = FindWindow(vbNullString, Caption)
    If Handle = 0 Then Exit Sub
    SendMessage Handle, WM_CLOSE, 0&, 0&
End Sub

   - Marc
Avatar of bingie
bingie

Avatar of MarcGraff

ASKER

The "Start and Stop Prossess" application can only close an application that it started. I need to close an application that is running.

   - Marc
Marc,

Try this:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10

Private Sub Form_Load()
   Dim lngHwnd As Long
   lngHwnd = FindWindow(vbNullString, "MyApp")
   SendMessage lngHwnd, WM_CLOSE, 0, 0
   End
End Sub
You can use this function to send Alt+F4 to the app thus closing it

Private Sub Closeapp(AppName as string)
AppActivate AppName
SendKeys "%{F4}"
End Sub

In the AppName string, include text from the Title bar of an application
The application being closed is not visible, therefore I do not know the caption.

   - Marc
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hearing...
Thanks, sorry for the wait...

   - Marc
No Problem....Glad I could help.
Comment for Idle_Mind - brilliant, returns the "Killed!" message box no problems - but appears that application to be killed is still running quite happily (?) Excel in this case .... I've copied your code from above and used it "raw" so where have I lost the plot?  Cheers & thanks.  Joce
Haven't tried it with excel...let me see if I can kill it on my system.
I just killed Excel on my system with "excel.exe".  How are you using it?
I've added a new module to a standard VB project and added and run from within the Visual Studio the following sub main - it comes back with the message but the excel app is still running ?

thanks :-)

Public Sub main()

Dim appName As String

appName = "Excel.exe"

        If KillAppByEXEName(appName) Then
            MsgBox "Killed!"
        Else
            MsgBox "Unable to find and/or kill"
        End If

End Sub
Interesting...I'm running Office XP (Excel 2002 10.5815.4219 SP-2) on Win ME.

What are you running?

Have you tried killing something simple like calculator (calc.exe) to determine if the code even works correctly on your system?

Idle_Mind
Hello

I'm running O2K on W2K pro (latest service packs for both) - I've tried and get the same results with the calculator - I really appreciate you help - thanks

Joce
i had the same problem with trying to kill an ActiveX EXE process that was started by Set XXX = New ActiveXEXE.className

it wasn't terminating until i added:

Private Const PROCESS_TERMINATE = (&H1)
in the declarations and changed the line

              hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                    Or PROCESS_VM_READ, 0, ProcessIDs(i))

to

              hProcess = OpenProcess(PROCESS_TERMINATE  or PROCESS_QUERY_INFORMATION _
                    Or PROCESS_VM_READ, 0, ProcessIDs(i))

if doing this with an ActiveX EXE process, make sure to set the object declaration to Nothing after killing the process otherwise you will not be able to instantiate it again.
OK will try, thanks :-)
I wasn't part of the original conversation, but this was really really helpful even now, thanks!
Alan


Posted answer fails under win2k3

anyone have a workaround?

now thats not good news....  thanks for the heads up pith
             hProcess = OpenProcess(PROCESS_TERMINATE  or PROCESS_QUERY_INFORMATION _
                    Or PROCESS_VM_READ, 0, ProcessIDs(i))

Works on XP sp2


Thanks Steve