Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Processes

Status
Not open for further replies.

jmlady

Programmer
Nov 20, 2003
19
0
0
US
Hey, I have a question reguarding system processes..

My goal, is that I want to terminate program specific processes, (for example, notepad.exe or other process)...

All of the TerminateProcess script examples I look at are for Terminating ALL processes, but I only want to terminate 1 or 2 specific processes (And I only know the .exe name for both of them) if they exits...

How do I go about doing something like that, using the example of 'notepad.exe' and 'wordpad.exe'?
 
You can use the code posted in thread222-834688 to obtain the process identifier of any running process by its name.

After obtaining the process Id, you can use the OpenProcess function to obtain a handle to that process and terminate it using TerminateProcess function.

Here the complete code is being posted again because the code in the above-mentioned thread has a very small bug.
___
[tt]
Option Explicit
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessId As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Const PROCESS_TERMINATE = (&H1)
Const TH32CS_SNAPPROCESS = 2
Const MAX_PATH = 260
Private 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
Function GetProcessId(Process As String) As Long
Dim hSnapShot As Long, pe32 As PROCESSENTRY32
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, ByVal 0)
pe32.dwSize = Len(pe32)
ProcessFirst hSnapShot, pe32
Do
If InStr(1, pe32.szExeFile, Process & vbNullChar, vbTextCompare) = 1 Then
GetProcessId = pe32.th32ProcessID
Exit Do
End If
Loop While ProcessNext(hSnapShot, pe32)
CloseHandle hSnapShot
End Function

Private Sub Form_Load()
Dim ProcessId As Long, hProcess As Long
'Obtain the process id
ProcessId = GetProcessId("wordpad.exe")
'Obtain process handle
hProcess = OpenProcess(PROCESS_TERMINATE, 0, ProcessId)
'Terminate the process
TerminateProcess hProcess, 0
'Close the process handle
CloseHandle hProcess

Unload Me
End Sub[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top