Forcing program to close

Posted on 2001-08-29
Last Modified: 2011-10-03
How can I close the realtime monitor for an AntiVirus program?  I am currently using the SendKeysA API command, but it looks like I have to force the program to close.  How can I do this?
Question by:bond007021200
  • 3
  • 2
  • 2
  • +2

Expert Comment

ID: 6437974
First you have to get it's handle, then you can kill it.

I will post an example of how to show the enumerated processes and kiil them.

Expert Comment

ID: 6437978
Here is a form with 2 command buttons and a listview

Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   5070
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6705
   LinkTopic       =   "Form1"
   ScaleHeight     =   5070
   ScaleWidth      =   6705
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdKill
      Caption         =   "&Kill Process"
      Height          =   360
      Left            =   3390
      TabIndex        =   2
      Top             =   4665
      Width           =   1470
   Begin MSComctlLib.ListView lvProcess
      Height          =   4515
      Left            =   30
      TabIndex        =   1
      Top             =   75
      Width           =   6630
      _ExtentX        =   11695
      _ExtentY        =   7964
      View            =   3
      LabelWrap       =   -1  'True
      HideSelection   =   -1  'True
      Checkboxes      =   -1  'True
      FullRowSelect   =   -1  'True
      _Version        =   393217
      ForeColor       =   -2147483640
      BackColor       =   -2147483643
      BorderStyle     =   1
      Appearance      =   1
      NumItems        =   1
      BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628}
         Text            =   "Process"
         Object.Width           =   8819
   Begin VB.CommandButton cmdRefresh
      Caption         =   "&Refresh"
      Height          =   360
      Left            =   1515
      TabIndex        =   0
      Top             =   4665
      Width           =   1470
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub cmdKill_Click()
  Dim lRetVal As Long
  Dim hWnd As Long
  Dim i As Integer
  For i = 1 To lvProcess.ListItems.Count
    hWnd = Mid(lvProcess.ListItems.Item(i).Key, 2)
    If lvProcess.ListItems.Item(i).Checked = True Then
      lRetVal = SendMessageByNum(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0&)
      lRetVal = SendMessage(hWnd, WM_CLOSE, ByVal CLng(0), ByVal CLng(0))
      lRetVal = CloseHandle(hWnd)
      lRetVal = DestroyWindow(hWnd)
      If IsWindow(hWnd) <> 0 Then
        MsgBox "Process " & lvProcess.ListItems.Item(i).Text & " could not be terminated."
      End If
    End If
  Next i
  cmdRefresh = True
End Sub

Private Sub cmdRefresh_Click()
  EnumWindows AddressOf WndEnumProc, 0&
End Sub

Private Sub Form_Load()
  cmdRefresh = True
End Sub

Accepted Solution

Dave_Greene earned 40 total points
ID: 6437985
Here is a module


Attribute VB_Name = "Module1"
Option Explicit

Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Any, ByVal lParam As Any) As Long
Declare Function EnumChildWindows Lib "user32" (ByVal hwndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Declare Function DestroyWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long
Public Declare Function SendMessageByNum Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Declare Function IsWindow Lib "user32.dll" (ByVal hwnd As Long) As Long

Public Const WM_CLOSE = &H10
Public Const WM_SYSCOMMAND = &H112
Public Const SC_CLOSE = &HF060&

Function WndEnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long

   Dim szTitle As String
   Dim sRes As String
   Dim bRet As Long
   szTitle = String(512, 0)
   bRet = GetWindowText(hwnd, szTitle, 512)
   sRes = Trim(szTitle)
   If sRes <> String(512, 0) Then
      If IsWindow(hwnd) = 1 Then
        If IsWindowVisible(hwnd) = 1 Then
          Form1.lvProcess.ListItems.Add , "K" & CStr(hwnd), sRes
        End If
      End If
   End If
   WndEnumProc = 1
End Function

Add them to your project and it should help you find the process you need to terminate.


Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 51

Expert Comment

by:Ryan Chong
ID: 6438925
Hi bond007,

Firstly, Obtaining a List of Running Processes:

Then, select the exe to close:
Example: c:\Program Files\Microsoft Office\Winword.exe"

Use the code below as: GetProcesses "c:\Program Files\Microsoft Office\Winword.exe"


Option Explicit

Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle

As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long,

ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule

As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule

As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As

Long) As Long
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)

Public Sub GetProcesses(strProcess As String)
 Dim lProcCount     As Long
 Dim lProcPoss      As Long
 Dim lNum           As Long
 Dim ProcessIDs()   As Long
 Dim cbNeeded2      As Long
 Dim NumElements2   As Long
 Dim arrModules(1 To 200) As Long
 Dim lRet           As Long
 Dim sModName       As String
 Dim hwndProcess    As Long
 Dim iCounter       As Long
 Const SIZE = 500
 'need to get the array containing the process id's for each process object
 lProcCount = 8
 lProcPoss = 96
 Do While lProcCount <= lProcPoss
     lProcCount = lProcCount * 2
     ReDim ProcessIDs(lProcCount / 4) As Long
     lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
 lNum = lProcPoss / 4
 For iCounter = 1 To lNum
     'Get a handle to the Process
     hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessIDs(iCounter))
     'Got a Process handle
     If hwndProcess <> 0 Then
         'Get an array of the module handles for the specified
         lRet = EnumProcessModules(hwndProcess, arrModules(1), 200, cbNeeded2)
         'If the Module Array is retrieved, Get the ModuleFileName
         If lRet <> 0 Then
             sModName = Space(255)
             lRet = GetModuleFileNameExA(hwndProcess, arrModules(1), sModName, SIZE)
             'Form1.lstProcesses.AddItem ProcessIDs(iCounter) & vbTab & Left(sModName, lRet)
             If UCase(Left(sModName, lRet)) = UCase(strProcess) Then
                 TerminateProcess hwndProcess, 0&
             End If
         End If
     End If
     'Close the handle
     lRet = CloseHandle(hwndProcess)
End Sub

'Hope will help.

Author Comment

ID: 6440717
I can get the windows handle of the program, but whenever I use any of the code to close it, it doesn't close.  Might this be because it is a real-time virus scanning program (called InnoculateIT)?

For some reason, it doesn't want to close whenever I tell it to do so.
LVL 51

Expert Comment

by:Ryan Chong
ID: 6443244
Hi bond007,

I heard InnoculateIT before, it is a free antivirus software, isn't it.

BTW, if you don't want it to start up, just go to its setting to disable it when windows start up.

Or edit the windows startup files (if you using win98), go to Run, type msconfig..


Author Comment

ID: 6443378
I have to let it run (work regulations), but I have to shut it off at a certain time so it can update our database correctly.
LVL 49

Expert Comment

ID: 7205269
Hi bond007@av,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Dave_Greene's comment(s) as an answer.

bond007@av, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7758368
Finalized as proposed


Community Support Moderator
Experts Exchange

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 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