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

Forcing program to close

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?
0
bond007021200
Asked:
bond007021200
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Dave_GreeneCommented:
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.
0
 
Dave_GreeneCommented:
Here is a form with 2 command buttons and a listview

VERSION 5.00
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
   End
   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
      EndProperty
   End
   Begin VB.CommandButton cmdRefresh
      Caption         =   "&Refresh"
      Height          =   360
      Left            =   1515
      TabIndex        =   0
      Top             =   4665
      Width           =   1470
   End
End
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()
  lvProcess.ListItems.Clear
  EnumWindows AddressOf WndEnumProc, 0&
End Sub

Private Sub Form_Load()
  cmdRefresh = True
End Sub
0
 
Dave_GreeneCommented:
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.

Cheers
Dave

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Ryan ChongCommented:
Hi bond007,

Firstly, Obtaining a List of Running Processes:

http://www.mvps.org/vbnet/code/system/toolhelpprocesses.htm

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
 Const PROCESS_QUERY_INFORMATION = 1024
 Const PROCESS_VM_READ = 16
 Const PROCESS_ALL_ACCESS = &H1F0FFF
 
 '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)
 Loop
 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
         'process
         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)
 Next
 
End Sub

'Hope will help.
0
 
bond007021200Author Commented:
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.
0
 
Ryan ChongCommented:
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..

regards.
0
 
bond007021200Author Commented:
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.
0
 
DanRollinsCommented:
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
0
 
moduloCommented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now