Close vb6.exe using vb

I want to be able to close out vb6.exe by identifing it from the Task Manager by Name and ID.  Windows API Prefered.  Kinda new to Windows API and Im having trouble closing out vb6.exe.

What I am tring to do is before I open up VB6.exe for my automation test I want to be able to check to see if it is already opened, if so close out VB6.exe before I open up a new instance.
Here is what I have Now. This will find an Instance of VB6.exe, but I dont know how to close/terminate it.

Thanks,
Anthony
========================================
rivate Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Private Declare Function ProcessFirst Lib "kernel32" _
Alias "Process32First" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Function ProcessNext Lib "kernel32" _
Alias "Process32Next" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Sub CloseHandle Lib "kernel32" _
(ByVal hPass As Long)

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

Private Sub Command1_Click()
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim r As Long
Dim strString As String
Dim strApp As String

strApp = "vb6.exe"
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)

If hSnapShot = 0 Then
    Exit Sub
End If

uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapShot, uProcess)

List1.Clear

Do While r = 1
    strString = uProcess.szExeFile
    List1.AddItem strString
    strString = Mid(strString, 1, 7)
    Debug.Print strString
    If strString = strApp Then
        MsgBox "Kill app"
       
    End If
   
    r = ProcessNext(hSnapShot, uProcess)
Loop

Call CloseHandle(hSnapShot)

End Sub

LVL 1
toys032498Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adityauCommented:
Try this code.

Private Type SECURITY_ATTRIBUTES
    nLength              As Long
    lpSecurityDescriptor As Long
    bInheritHandle       As Boolean
End Type

' Constants for Registry top-level keys
Const HKEY_LOCAL_MACHINE = &H80000002

Const ERROR_MORE_DATA = 234
Const REG_SZ = 1
Const KEY_QUERY_VALUE = &H1

Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
        Alias "RegOpenKeyExA" _
        (ByVal hKey As Long, ByVal lpSubKey As String, _
        ByVal ulOptions As Long, ByVal samDesired As Long, _
        phkResult As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
        Alias "RegQueryValueExA" _
        (ByVal hKey As Long, ByVal lpszValueName As String, _
        ByVal lpdwReserved As Long, lpdwType As Long, _
        lpData As Any, lpcbData As Long) As Long
Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Private Declare Function ProcessFirst Lib "kernel32" _
Alias "Process32First" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Function ProcessNext Lib "kernel32" _
Alias "Process32Next" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Sub CloseHandle Lib "kernel32" _
(ByVal hPass As Long)

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Private Sub Command1_Click()
    Dim hSnapShot As Long
    Dim uProcess As PROCESSENTRY32
    Dim r As Long
    Dim strString As String
    Dim strApp As String
    Dim lResult As Long
    Dim lHandle As Long
    Dim sValueName As String
    Dim sValue As String
    Dim lSize As Long
   
    sValueName = "ProductDir"
    lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\DevStudio\5.0\Products\Microsoft Visual Basic", 0, KEY_QUERY_VALUE, lHandle)
    lResult = RegQueryValueEx(lHandle, sValueName, 0, REG_SZ, sValue, lSize)
    If lResult = ERROR_MORE_DATA Then
        sValue = Space(lSize)
        lResult = RegQueryValueEx(lHandle, sValueName, 0, REG_SZ, ByVal sValue, lSize)
    End If
    sValue = UCase(Left(sValue, Len(sValue) - 1))
    sValue = sValue & "*"
      hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)

      If hSnapShot = 0 Then
          Exit Sub
      End If

      uProcess.dwSize = Len(uProcess)
      r = ProcessFirst(hSnapShot, uProcess)

      List1.Clear

      Do While r = 1
          strString = uProcess.szExeFile
          List1.AddItem strString
          strString = Trim(strString)
          Debug.Print strString
          If strString Like sValue Then
              MsgBox "Kill app"
          End If
           
          r = ProcessNext(hSnapShot, uProcess)
      Loop
      Call CloseHandle(hSnapShot)

End Sub
0
adityauCommented:
I am sorry. As, I tried it with vb5, I used 5.0. Please fiundout in which key the vb6 path is stored from the registry and use that.
0
toys032498Author Commented:
Thanks for the idea adityau, I will leave the question open for further investigation.

Points +50
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

toys032498Author Commented:
Edited text of question.
0
toys032498Author Commented:
Edited text of question.
0
toys032498Author Commented:
Adjusted points to 400
0
KhollADCommented:
I hope the code below is what you want :

(I make the changes from your code)


Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long

Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16



Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260

Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type

Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Private Declare Function ProcessFirst Lib "kernel32" _
Alias "Process32First" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Function ProcessNext Lib "kernel32" _
Alias "Process32Next" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Private Declare Sub CloseHandle Lib "kernel32" _
(ByVal hPass As Long)

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

Private Sub Command1_Click()
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim r As Long
Dim strString As String
Dim strApp As String
Dim hProcess As Long
Dim lRet As Long
Dim ExCode As Long

strApp = "vb6.exe"
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)

If hSnapShot = 0 Then
    Exit Sub
End If

uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapShot, uProcess)

List1.Clear

Do While r = 1
    strString = uProcess.szExeFile
    List1.AddItem strString

    If InStr(1, UCase(strString), UCase(strApp)) <> 0 Then
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, uProcess.th32ProcessID)
        lRet = GetExitCodeProcess(hProcess, ExCode)

        List1.List(List1.NewIndex) = "Killed --> " & List1.List(List1.NewIndex)
        lRet = TerminateProcess(hProcess, ExCode)
         
    End If
     
    r = ProcessNext(hSnapShot, uProcess)
Loop

Call CloseHandle(hSnapShot)

End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
toys032498Author Commented:
Works Great on Win9x machines, but fails on NT4 and Does not Terminate Vb6 on NT5.

NT4 Does not Reconize:  
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Any Ideas?

I think I have a fix for NT5, There is some documentation that shows that Windows NT/2000: The handle must have PROCESS_TERMINATE access.

Thanks for all you Help.  
Up points to 400!  
0
KhollADCommented:
Hi Toys,

 Thanks a lot for the points.

 The code below is a NT version of the previous code :

 (if you don't have psapi.dll in your NT, copy this file from your Visual Studio CD : \UNSUPPRT\WSVIEW\WINNT\ to c:\winnt\system32\)



Option Explicit

Private Declare Function CloseHandle Lib "Kernel32.dll" _
   (ByVal Handle As Long) As Long

Private Declare Function OpenProcess Lib "Kernel32.dll" _
  (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
      ByVal dwProcId As Long) As Long

Private Declare Function EnumProcesses Lib "psapi.dll" _
   (ByRef lpidProcess As Long, ByVal cb As Long, _
      ByRef cbNeeded As Long) As Long

Private Declare Function GetModuleFileNameExA Lib "psapi.dll" _
   (ByVal hProcess As Long, ByVal hModule As Long, _
      ByVal ModuleName As String, ByVal nSize As Long) As Long

Private Declare Function EnumProcessModules Lib "psapi.dll" _
   (ByVal hProcess As Long, ByRef lphModule As Long, _
      ByVal cb As Long, ByRef cbNeeded As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

Private Const PROCESS_TERMINATE = &H1 'enables terminate process in NT
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const MAX_PATH = 260
         

Private Sub Command1_Click()

   Dim hProcess As Long
   Dim cb As Long
   Dim cbNeeded As Long
   Dim NumElements As Long
   Dim ProcessIDs() As Long
   Dim cbNeeded2 As Long
   Dim NumElements2 As Long
   Dim Modules(1 To 200) As Long
   Dim lRet As Long
   Dim ModuleName As String
   Dim nSize As Long
   Dim i As Long
   Dim ExCode As Long
   Dim strApp As String

   strApp = "vb6.exe"
   
   
   List1.Clear
   
   'Get the array containing the process id's for each process object
   cb = 8
   cbNeeded = 96
   Do While cb <= cbNeeded
      cb = cb * 2
      ReDim ProcessIDs(cb / 4) As Long
      lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
   Loop
   NumElements = cbNeeded / 4

   For i = 1 To NumElements
      'Get a handle to the Process
      hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
         Or PROCESS_VM_READ Or PROCESS_TERMINATE, 0, ProcessIDs(i))
      'Got a Process handle
      If hProcess <> 0 Then
          'Get an array of the module handles for the specified
          'process
          lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                                       cbNeeded2)
          'If the Module Array is retrieved, Get the ModuleFileName
          If lRet <> 0 Then
             ModuleName = Space(MAX_PATH)
             nSize = 500
             lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                             ModuleName, nSize)
             List1.AddItem ModuleName
             
             If InStr(1, UCase(Trim(ModuleName)), UCase(Trim(strApp))) <> 0 Then
             
                               
                lRet = GetExitCodeProcess(hProcess, ExCode)
                lRet = TerminateProcess(hProcess, ExCode)
                List1.List(List1.NewIndex) = "Killed --> " & List1.List(List1.NewIndex)
             End If
             
          End If
      End If
    'Close the handle to the process
   lRet = CloseHandle(hProcess)
   Next

End Sub


Any doubts, send a comment.
Kholl.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.