VB 6.0 - how to detect an open program(e g WINWORD)

Asked By chao saborrido on 26-Apr-05 08:43 PM
Hi there!!!!
    Could anybody give me a tip on how can i identify a running program(e.g winword; games) using visual basic? and how to close it safely?  your help would be appreciated a lot. Thanks

Kill process using Visual Basic 6 0 and WMI - Asked By Robbe Morris on 27-Apr-05 10:30 AM

Public Sub KillProcess(byval processName as string)
        On Error GoTo ErrHandler
         Dim oWMI
         Dim ret
         Dim sService
         Dim oWMIServices
         Dim oWMIService
         Dim oServices
         Dim oService
         Dim servicename 
          Set oWMI = GetObject("winmgmts:")
          Set oServices = oWMI.InstancesOf("win32_process")
          For Each oService In oServices
                 servicename = LCase(Trim(CStr(oService.Name) & ""))
                 If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
                    ret = oService.Terminate
                 End If
          Next
          Set oServices = Nothing
          Set oWMI = Nothing
ErrHandler:
  Err.Clear
End Sub
KillProcess "excel"

Wonderful Code! - Krishnadas P replied to Robbe Morris on 12-Jun-08 12:34 AM

Hello there!

While searching code for closing an open application in Visual Basic 6 and I came across your code in this site through Google Search.  I am developing a software for Internet Cafes so needed code for blocking unwanted programs.  This code will be very much helpful for me, I hope so.

There was a spelling error in your program.
serviceame = LCase(Trim(CStr(oService.Name) & ""))

Here Servicename was typed as "serviceame". Due to this error I was
unable to get the result.

Thank you so much for your code and congratulations.

Krishnadas

Great piece of code.. - Amit Choudhary replied to Krishnadas P on 24-Oct-08 09:22 AM

We were in mess of API's close an exe.

Many Thanks for great piece of code..  

piece of code - ramz buen replied to Amit Choudhary on 07-Oct-09 03:00 AM
though i havent tried. thanks too.
Russ replied to Robbe Morris on 06-Jul-10 05:58 PM

I have code that can leave an orphaned ref to Excel, as seen in Task Manager processes.

When next running the code this would generate err = 70 'Permission to Kill file is denied'.

Your code cleans this issue nicely.

Many thanks

Russ

Gagan replied to Robbe Morris on 22-Oct-10 11:27 AM
Thanks...great code snippet!

My goal was to kill an orphan ActiveX EXE process that my main VBprogram started, but it needed to kill any such processes hanging around in case the main app crashed and had to restart. First, I too was trying to use APIs - & using getWindowHandle, TerminateProcess etc. ... it was working for some other processes but not for the one I wanted.

Your code helped a lot! and took care of my problem right away.

Thanks
Gagan
Thanks for the feedback - Robbe Morris replied to Gagan on 22-Oct-10 12:02 PM
Always trying to help my fellow developers :)

There is a .NET version of this in my FAQs if anyone needs it

http://www.eggheadcafe.com/faqlist.aspx?userid=fadce924-7702-4fd3-98b0-c1c1b024680b
pengjoo replied to chao saborrido on 22-Feb-11 07:57 PM
GREAT CODE!!!
had been trying out many way of code writing to close the application / kill the application / kill the process and doesn't work well. thus got permission denied error when the processes is  active. Your code help me a lot and work fine. Thank you so much ! 
travis replied to chao saborrido on 10-Mar-11 09:19 AM
Works great, I have been chasing my tail for hours. Thanks for posting this, you make me look smart.
doss doss replied to Robbe Morris on 28-May-12 12:48 AM
thanks..............