Solved

Forcing program to close

Posted on 2001-08-29
9
288 Views
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?
0
Comment
Question by:bond007021200
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
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.
0
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6437978
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
 
LVL 8

Accepted Solution

by:
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.

Cheers
Dave

0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6438925
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bond007021200
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.
0
 
LVL 49

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..

regards.
0
 

Author Comment

by:bond007021200
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.
0
 
LVL 49

Expert Comment

by:DanRollins
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
0
 

Expert Comment

by:modulo
ID: 7758368
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now