Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Const TH32CS_SNAPPROCESS = &H2
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
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
szExePath As String * 260
End Type
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Function FindProcess(ByVal sExeName As String) As Long
On Error GoTo Error_Handler
Dim hSnapShot As Long
Dim i As Long
Dim atProcEntries() As PROCESSENTRY32
If hSnapShot <> -1 Then
i = 0
ReDim atProcEntries(i)
atProcEntries(i).dwSize = LenB(atProcEntries(i))
If Process32First(hSnapShot, atProcEntries(i)) <> 0 Then
Do
If InStr(UCase(atProcEntries(i).szExePath), UCase(sExeName)) <> 0 Then
FindProcess = atProcEntries(i).th32ProcessID
Exit Do
End If
i = i + 1
ReDim Preserve atProcEntries(i)
atProcEntries(i).dwSize = LenB(atProcEntries(i))
If Process32Next(hSnapShot, atProcEntries(i)) = 0 Then
ReDim Preserve atProcEntries(i - 1)
Exit Do
End If
Loop
End If
CloseHandle hSnapShot
End If
Exit Function
Error_Handler:
If hSnapShot <> -1 Then CloseHandle (hSnapShot)
End Function
Private Sub Form_Load()
Dim hProc As Long
Do While True
hProc = OpenProcess(PROCESS_ALL_ACCESS, 0, FindProcess("notepad.exe"))