Detecting a Process.

VB.NET:
Private Sub KillProcess(byval ProcessName as string)
        Dim prc() As Process
        Dim ERROR_FILE_NOT_FOUND As Integer = 2
        Dim ERROR_ACCESS_DENIED As Integer = 5
        Try
            prc = System.Diagnostics.Process.GetProcessesByName("ProcessName")
            Dim eprc As IEnumerator = prc.GetEnumerator
            eprc.Reset()

            While eprc.MoveNext
                Dim proc As Process = CType(eprc.Current, Process)
                proc.Kill()
                proc = Nothing
            End While

            eprc = Nothing
            prc = Nothing

        Catch e As Win32Exception
            If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
                MessageBox.Show(e.Message + ". Process not found.")

            Else
                If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
                    ' Note that if your word processor might generate exceptions 
                    ' such as this, which are handled first. 
                    MessageBox.Show(e.Message + ". You do not have permission to kill this process.")
                End If
            End If
        End Try
    End Sub

Call KillProcess("test.exe")
Call KillProcess("msnmsgr.exe") 'MSN Messenger
 
Last edited:
oh shyt, i always forget namespaces..

Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.ComponentModel
 
Hmm, i'm using this code now.
VB.NET:
[size=2]#[/size][size=2][color=#0000ff]Region[/color][/size][size=2] " Disable MSN Messenger "

[/size][size=2][color=#0000ff]Sub[/color][/size][size=2] KillMessenger()

[/size][size=2][color=#0000ff]Call[/color][/size][size=2] KillProcess("msnmsgr.exe") [/size][size=2][color=#008000]'MSN Messenger

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size]
[size=2][color=#0000ff] 
[/color][/size][size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] KillProcess([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] ProcessName [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2])

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] prc() [/size][size=2][color=#0000ff]As[/color][/size][size=2] Process

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ERROR_FILE_NOT_FOUND [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 2

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ERROR_ACCESS_DENIED [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 5

[/size][size=2][color=#0000ff]Try

[/color][/size][size=2]prc = System.Diagnostics.Process.GetProcessesByName("ProcessName")

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] eprc [/size][size=2][color=#0000ff]As[/color][/size][size=2] IEnumerator = prc.GetEnumerator

eprc.Reset()

[/size][size=2][color=#0000ff]While[/color][/size][size=2] eprc.MoveNext

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] proc [/size][size=2][color=#0000ff]As[/color][/size][size=2] Process = [/size][size=2][color=#0000ff]CType[/color][/size][size=2](eprc.Current, Process)

proc.Kill()

proc = [/size][size=2][color=#0000ff]Nothing

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]While

[/color][/size][size=2]eprc = [/size][size=2][color=#0000ff]Nothing

[/color][/size][size=2]prc = [/size][size=2][color=#0000ff]Nothing

[/color][/size][size=2][/size][size=2][color=#0000ff]Catch[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] Win32Exception

[/size][size=2][color=#0000ff]If[/color][/size][size=2] e.NativeErrorCode = ERROR_FILE_NOT_FOUND [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]MessageBox.Show(e.Message + ". Process not found.")

[/size][size=2][color=#0000ff]Else

[/color][/size][size=2][/size][size=2][color=#0000ff]If[/color][/size][size=2] e.NativeErrorCode = ERROR_ACCESS_DENIED [/size][size=2][color=#0000ff]Then

[/color][/size][size=2][/size][size=2][color=#008000]' Note that if your word processor might generate exceptions 

[/color][/size][size=2][/size][size=2][color=#008000]' such as this, which are handled first. 

[/color][/size][size=2]MessageBox.Show(e.Message + ". You do not have permission to kill this process.")

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size][size=2]#[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Region[/color][/size]


I run the sub at the Formload, it pauses to think about it, but then continues with the formload, and doesn't kill the process. I tried it with other processes, and still nothing?
Any ideas? Thanks.
 
hmmm, i dunno that exact code kills msn messenger on my system (i even set it ta kill msn on app startup)
 
Change this line:
VB.NET:
prc = System.Diagnostics.Process.GetProcessesByName("ProcessName")
To this:
VB.NET:
prc = System.Diagnostics.Process.GetProcessesByName(ProcessName)
i.e. remove the quotes around ProcessName.

Hmm, for some reason spaces are being inserted into 'ProcessName' with the code blocks. Should read like this:

prc = System.Diagnostics.Process.GetProcessesByName(ProcessName)

OK, it does it without the code blocks too. Anyway, hopefully you know what I mean.
 
Last edited:
It just doesn't seem to want to work! I tried removing the quotes, still nothing. My computer pauses the first time the code is run, for example if i put it behind a button.click event. but then does nothing. It doesn't even show any of the error messages?

It seems strange that it works fine for you and not at all for me. But oh well. Thanks for your help.
Just out of interest what OS you using? I've tried on XP SP2, and Server 2003 Pro.
 
Ok you know you have this program to run and close MSN or whatever process you choose. Well I was wondering, is this program of yours a service that runs at startup or something like that, or do you have to run it manually?
 
Michaelk,

I have played around with the code that JuggaloBrotha provided, and it does work.

The catch is you have to leave the ".exe" off when passing the process name to the Sub.

Hope this helps!
 
Back
Top