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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.


SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

LVL 52

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 52

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

710 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