• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

110 Points Kill Running Application

Need working example of killing an application that is running from another application. I saw a nice small little example of this somewhere, didn't keep it for inspection and here i am !
Using visual 5.0 can't afford 6.0
1 Solution
'This VB program can kill any applications running on windows

'put it in the module.bas
Option Explicit
DefInt A-Z

Declare Function Findwindow Lib "User" (ByVal Class&, ByVal Caption&)
Declare Function GetWindow Lib "User" (ByVal hWnd, ByVal wCmd)
Declare Function GetNextWindow Lib "User" (ByVal hWnd, ByVal wCmd)
Declare Function GetWindowText Lib "User" (ByVal hWnd, ByVal Buf$, ByVal lBuf)
Declare Sub ShowWindow Lib "User" (ByVal hWnd, ByVal nCmd)
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Any) As Long

Const WM_CLOSE = &H10

Function GetWinCaption$(hWnd)

    Dim strCaption As String * 256
    Dim NumChars

    NumChars = GetWindowText(hWnd, strCaption, 256)
    GetWinCaption$ = Left$(strCaption, NumChars)

End Function

' This program checks all the application windows' captions, if there is
' a match, it will ask the user to teminate the program or not.
' If there is nothing matches, it will terminate itself.
' This is useful when the user running an application and try to install
' it while it is running.
' Copyright 1997 (c) 2Link Consulting Inc.
' By Raymond Tsang
Sub Main()

    Dim Wnd, lResult As Long, iResult
    Wnd = SearchWindow("Your application name")
    If Wnd <> 0 Then
        iResult = MsgBox("Another copy of *** is running, do you want to terminate it before starting the installation?", 4, "KillApp")
        If iResult = 6 Then
            lResult = SendMessage(Wnd, WM_CLOSE, 0, 0&)
        End If
    End If

End Sub

Function SearchWindow(ByVal strSearch$)

    Dim hWnd

    ' Find the first window's handle
    hWnd = Findwindow(0, 0)
    hWnd = GetWindow(hWnd, GW_HWNDFIRST)
    ' Set all comparison to upper case
    strSearch$ = UCase$(strSearch$)
    ' Start the loop for all windows
    Do While hWnd <> 0
        ' If the searching caption is a sub string of the current window's caption
        If InStr(UCase$(GetWinCaption(hWnd)), strSearch$) Then
            ' Return the window's handle
            SearchWindow = hWnd
            Exit Function
        End If
        ' Get the next window handle
        hWnd = GetNextWindow(hWnd, GW_HWNDNEXT)

End Function

stnicAuthor Commented:
Hey, thanks a lot. Works for me :)
Hi -
This is exactly what I'm looking for...
I'm creating installs using Wise InstallMaster, which will let me make dll function calls...however, I've been unsuccessful at killing the application with a user32.dll call -- so considering running a small VB exe to do it.
One issue - If I create a module using the VB posted by hatem72, i get a debug error on "hWnd = Findwindow(0, 0)" regarding "user".
Looks like there's something obvious I need to define, but not sure what it is.
Can you help?

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now