Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageOn 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)
Post Follow-up to this messageOk 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)
Post Follow-up to this messageOn 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)
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.