Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Will this "kill process" code work on win2k/XP?
Hi, found this on the web and it works fine on win98 - will it work on
win2k/xp?

Thanks,
Mark

Public Sub KillProcess(NameProcess As String)
Const PROCESS_ALL_ACCESS = 0
Const TH32CS_SNAPPROCESS As Long = 2&
Dim uProcess  As PROCESSENTRY32
Dim RProcessFound As Long
Dim hSnapshot As Long
Dim SzExename As String
Dim ExitCode As Long
Dim MyProcess As Long
Dim AppKill As Boolean
Dim AppCount As Integer
Dim i As Integer
Dim WinDirEnv As String

If NameProcess <> "" Then
AppCount = 0

uProcess.dwSize = Len(uProcess)
hSnapshot =  CreateToolhelpSnapshot(TH32CS_SNAPPROCES
S, 0&)
RProcessFound = ProcessFirst(hSnapshot, uProcess)

Do
i = InStr(1, uProcess.szexeFile, Chr(0))
SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
WinDirEnv = Environ("Windir") + "\"
WinDirEnv = LCase$(WinDirEnv)

If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess)
Then
AppCount = AppCount + 1
MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False,
uProcess.th32ProcessID)
AppKill = TerminateProcess(MyProcess, ExitCode)
Call CloseHandle(MyProcess)
End If
RProcessFound = ProcessNext(hSnapshot, uProcess)
Loop While RProcessFound

Call CloseHandle(hSnapshot)
End If

End Sub


--
 ________________________________________
_________________
Mark Durrenberger, PMP
Principal, Oak Associates, Inc, www.oakinc.com
"Advancing the Theory and Practice of Project Management"
 ________________________________________
________________

The nicest thing about NOT planning is that failure
comes as a complete surprise and is not preceded by
a period of worry and depression.

- Sir John Harvey-Jones



Report this thread to moderator Post Follow-up to this message
Old Post
Mark Durrenberger
09-28-04 08:55 PM


Re: Will this "kill process" code work on win2k/XP?
On Tue, 28 Sep 2004 14:10:26 -0400, "Mark Durrenberger"
<durrenm@yahoo.com> wrote:

>Hi, found this on the web and it works fine on win98 - will it work on
>win2k/xp?
>
>Thanks,
>Mark
>
>Public Sub KillProcess(NameProcess As String)
>Const PROCESS_ALL_ACCESS = 0
>Const TH32CS_SNAPPROCESS As Long = 2&
>Dim uProcess  As PROCESSENTRY32
>Dim RProcessFound As Long
>Dim hSnapshot As Long
>Dim SzExename As String
>Dim ExitCode As Long
>Dim MyProcess As Long
>Dim AppKill As Boolean
>Dim AppCount As Integer
>Dim i As Integer
>Dim WinDirEnv As String
>
>       If NameProcess <> "" Then
>          AppCount = 0
>
>          uProcess.dwSize = Len(uProcess)
>          hSnapshot =  CreateToolhelpSnapshot(TH32CS_SNAPPROCES
S, 0&)
>          RProcessFound = ProcessFirst(hSnapshot, uProcess)
>
>          Do
>            i = InStr(1, uProcess.szexeFile, Chr(0))
>            SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
>            WinDirEnv = Environ("Windir") + "\"
>            WinDirEnv = LCase$(WinDirEnv)
>
>            If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess)
>Then
>               AppCount = AppCount + 1
>               MyProcess = OpenProcess(PROCESS_ALL_ACCESS, False,
>uProcess.th32ProcessID)
>               AppKill = TerminateProcess(MyProcess, ExitCode)
>               Call CloseHandle(MyProcess)
>            End If
>            RProcessFound = ProcessNext(hSnapshot, uProcess)
>          Loop While RProcessFound
>
>          Call CloseHandle(hSnapshot)
>       End If
>
>End Sub

Assuming your declares are correct (NT/XP is less forgiving in that
regard), you may need to change PROCESS_ALL_ACCESS to
PROCESS_TERMINATE if this thing isn't running under an admin account.
(General ROT: use the lowest permission that works <g> )

Also I'd strongly advise against using VB booleans with Api calls.
They usually work, but will occassionally bite you.

Lastly, IMO it's worth mentioning that this is ~not~ the preferred
method of closing applications. The recommended approach is to start
by posing WM_CLOSE to an app's main window (allowing it to clean up
after itself), and escalating to TerminateProcess only as a last
resort.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)

Report this thread to moderator Post Follow-up to this message
Old Post
Tom Esh
09-29-04 01:55 AM


Re: Will this "kill process" code work on win2k/XP?
Ok now off to find out how to do "WM_Close" .... Yes I want it to be a
"pretty" shutdown....

Thanks,
Mark

--
 ________________________________________
_________________
Mark Durrenberger, PMP
Principal, Oak Associates, Inc, www.oakinc.com
"Advancing the Theory and Practice of Project Management"
 ________________________________________
________________

The nicest thing about NOT planning is that failure
comes as a complete surprise and is not preceded by
a period of worry and depression.

- Sir John Harvey-Jones
"Tom Esh" <tjeshGibberish@earthlink.net> wrote in message
 news:20hjl0534qeb6ascjagmjfkq4e2e9igdaf@
4ax.com...
> On Tue, 28 Sep 2004 14:10:26 -0400, "Mark Durrenberger"
> <durrenm@yahoo.com> wrote:
> 
>
> Assuming your declares are correct (NT/XP is less forgiving in that
> regard), you may need to change PROCESS_ALL_ACCESS to
> PROCESS_TERMINATE if this thing isn't running under an admin account.
> (General ROT: use the lowest permission that works <g> )
>
> Also I'd strongly advise against using VB booleans with Api calls.
> They usually work, but will occassionally bite you.
>
> Lastly, IMO it's worth mentioning that this is ~not~ the preferred
> method of closing applications. The recommended approach is to start
> by posing WM_CLOSE to an app's main window (allowing it to clean up
> after itself), and escalating to TerminateProcess only as a last
> resort.
>
>
> -Tom
> MVP - Visual Basic
> (please post replies to the newsgroup)



Report this thread to moderator Post Follow-up to this message
Old Post
Mark Durrenberger
09-29-04 01:55 PM


Re: Will this "kill process" code work on win2k/XP?
On Wed, 29 Sep 2004 08:08:27 -0400, "Mark Durrenberger"
<durrenm@yahoo.com> wrote:

>Ok now off to find out how to do "WM_Close" .... Yes I want it to be a
>"pretty" shutdown....

Karl Peterson has an example on his site:
http://www.mvps.org/vb/index2.html

"TaskList.zip", I think.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)

Report this thread to moderator Post Follow-up to this message
Old Post
Tom Esh
09-29-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Visual Basic archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:33 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.