Ending WinNT Processes With VBA or VB6 - HELP!!!

BigDWAdkins

New Member
Joined
Jul 18, 2002
Messages
13
Does anyone know how to check to see if a WinNT process is running? (Not a "Task" - But a "Process" under the "Processes" tab of Windows NT Task Manager) And if a certain process IS running to "End" or "Terminate" that process? This solution can be in VBA or VB6. (Preferrably in VB6 so I can compile it to an .exe file) Thanks.

P.S. The name ("Image Name") of the process I want to end is "MacExp.exe"

I guess you would have the code find the program ID of the process? Not sure. I know this is a basic thing but I can't figure it out.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi BigDWAdkins, here is a sample code which get ProcessID.
Put this code on Userform1 with CommandButton1 and ListBox1.

<PRE>
<FONT color=red>Option Explicit</FONT>



Private Declare <FONT color=red>Function </FONT>CreateToolhelp32Snapshot Lib "kernel32.dll" ( _

<FONT color=red>ByVal</FONT> dwFlags <FONT color=red>As</FONT><FONT color=red> Long</FONT>, _

<FONT color=red>ByVal</FONT> th32ProcessID <FONT color=red>As</FONT><FONT color=red> Long</FONT>) <FONT color=red>As</FONT><FONT color=red> Long</FONT>



Private Declare <FONT color=red>Function </FONT>Process32First Lib "kernel32.dll" ( _

<FONT color=red>ByVal</FONT> hSnapshot <FONT color=red>As</FONT><FONT color=red> Long</FONT>, _

<FONT color=red>ByRef</FONT> lppe <FONT color=red>As</FONT> PROCESSENTRY32) <FONT color=red>As</FONT><FONT color=red> Long</FONT>



Private Declare <FONT color=red>Function </FONT>Process32<FONT color=red>Next </FONT>Lib "kernel32.dll" ( _

<FONT color=red>ByVal</FONT> hSnapshot <FONT color=red>As</FONT><FONT color=red> Long</FONT>, _

<FONT color=red>ByRef</FONT> lppe <FONT color=red>As</FONT> PROCESSENTRY32) <FONT color=red>As</FONT><FONT color=red> Long</FONT>



Private Declare <FONT color=red>Function </FONT>CloseHandle Lib "kernel32.dll" ( _

<FONT color=red>ByVal</FONT> hObject <FONT color=red>As</FONT><FONT color=red> Long</FONT>) <FONT color=red>As</FONT><FONT color=red> Long</FONT>



Private <FONT color=red>Const </FONT>MAX_PATH = 260

Private<FONT color=red> Type</FONT> PROCESSENTRY32

dwSize <FONT color=red>As</FONT><FONT color=red> Long</FONT>

cntUsage <FONT color=red>As</FONT><FONT color=red> Long</FONT>

th32ProcessID <FONT color=red>As</FONT><FONT color=red> Long</FONT>

th32DefaultHeapID <FONT color=red>As</FONT><FONT color=red> Long</FONT>

th32ModuleID <FONT color=red>As</FONT><FONT color=red> Long</FONT>

cntThreads <FONT color=red>As</FONT><FONT color=red> Long</FONT>

th32ParentProcessID <FONT color=red>As</FONT><FONT color=red> Long</FONT>

pcPriClassBase <FONT color=red>As</FONT><FONT color=red> Long</FONT>

dwFlags <FONT color=red>As</FONT><FONT color=red> Long</FONT>

szExeFile <FONT color=red>As</FONT><FONT color=red> String</FONT> * MAX_PATH

End<FONT color=red> Type</FONT>



Private <FONT color=red>Const </FONT>TH32CS_SNAPPROCESS = &H2

Private <FONT color=red>Const </FONT>TH32CS_SNAPTHREAD = &H4

Private <FONT color=red>Const </FONT>TH32CS_SNAPMODULE = &H8



<FONT color=red>Private <FONT color=red>Sub </FONT></FONT>CommandButton1_Click()

<FONT color=red>Dim </FONT>hSnapshot <FONT color=red>As</FONT><FONT color=red> Long</FONT>

<FONT color=red>Dim </FONT>lppe <FONT color=red>As</FONT> PROCESSENTRY32

<FONT color=red>Dim </FONT>rc <FONT color=red>As</FONT><FONT color=red> Long</FONT>

hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

<FONT color=red>Debug.Print</FONT> hSnapshot



lppe.dwSize = Len(lppe)

rc = Process32First(hSnapshot, lppe)

ListBox1.Clear

Do

<FONT color=red>If </FONT>rc <> 1<FONT color=red> Then </FONT><FONT color=red>Exit Do</FONT>

ListBox1.AddItem lppe.th32ProcessID & ":" & lppe.szExeFile

rc = Process32Next(hSnapshot, lppe)

<FONT color=red>Loop</FONT>

<FONT color=red>Call</FONT> CloseHandle(hSnapshot)



<FONT color=red>End Sub</FONT>




</PRE>
 
Upvote 0
Hi DW.
See Sub Main at the bottom for usage...
Tom
<pre>
Option Explicit
Const MAX_PATH& = 260

Declare Function TerminateProcess _
Lib "kernel32" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long
Declare Function OpenProcess Lib _
"kernel32" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, _
ByVal dwAppProcessId As Long) As Long
Declare Function ProcessFirst _
Lib "kernel32" Alias "Process32First" _
(ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext _
Lib "kernel32" Alias "Process32Next" _
(ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot _
Lib "kernel32" Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, _
lProcessID As Long) As Long
Declare Function CloseHandle _
Lib "kernel32" (ByVal hObject As Long) As Long

Private Type LUID
lowpart As Long
highpart As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LuidUDT As LUID
Attributes As Long
End Type

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function GetVersion _
Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess _
Lib "kernel32" () As Long
Private Declare Function OpenProcessToken _
Lib "advapi32" (ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue _
Lib "advapi32" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges _
Lib "advapi32" (ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Long, _
PreviousState As Any, _
ReturnLength As Any) As Long

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
'---------------------------------------
Public Function KillA(myName As String) As Boolean
Const TH32CS_SNAPPROCESS As Long = 2&
Const PROCESS_ALL_ACCESS = 0
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
On Local Error GoTo Finish
appCount = 0

uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillA = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
If KillProcess(uProcess.th32ProcessID, 0) Then
'For debug.... Remove this
'MsgBox "Instance no. " & appCount & " of " & szExename & " was terminated!"
End If

End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Exit Function
Finish:
'MsgBox "Error!"
End Function

'Terminate any application and return an exit code to Windows.
Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
Dim hToken As Long
Dim hProcess As Long
Dim tp As TOKEN_PRIVILEGES


If GetVersion() >= 0 Then

If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
GoTo CleanUp
End If

If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
GoTo CleanUp
End If

tp.PrivilegeCount = 1
tp.Attributes = SE_PRIVILEGE_ENABLED

If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
GoTo CleanUp
End If
End If

hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
If hProcess Then

KillProcess = (TerminateProcess(hProcess, exitCode) <> 0)
' close the process handle
CloseHandle hProcess
End If

If GetVersion() >= 0 Then
' under NT restore original privileges
tp.Attributes = 0
AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&

CleanUp:
If hToken Then CloseHandle hToken
End If

End Function

Sub Main()
KillA "MacExp.exe"
End Sub
</pre>
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,309
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top