Re: Detecting a running process.
- From: "Harry Bates" <None>
- Date: Tue, 10 May 2005 15:13:00 -0400
Haven't worked with this is a while and I forgot where I got it from but you
should be able to clean it up:
-Harry Bates
Option Explicit
Private Const PROCESS_QUERY_INFORMATION As Long = 1024
Private Const PROCESS_VM_READ As Long = 16
Private Const MAX_PATH As Long = 260
Private Const LB_SETTABSTOPS As Long = &H192
Private Declare Function EnumProcesses Lib "psapi.dll" (lpidProcess As Long,
ByVal cb As Long, cbNeeded As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal
dwProcessID As Long, lphModule As Long, ByVal cb As Long, cbNeeded As Long)
As Long
Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias
"GetModuleFileNameExA" (ByVal dwProcessID As Long, ByVal hModule As Long,
ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal
dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcID As
Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal Handle As
Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long
Public Function GetNTProcessCount(dwProcID() As Long) As Long
Dim cb As Long
Dim cbNeeded As Long
cb = 128
Do
cb = cb * 2
ReDim dwProcID(1 To (cb \ 4)) As Long
Call EnumProcesses(dwProcID(1), cb, cbNeeded)
Loop While cbNeeded >= cb
GetNTProcessCount = cbNeeded \ 4
End Function
Public Function GetNTProcessCountNoComments(dwProcID() As Long) As Long
Dim cb As Long
Dim cbNeeded As Long
cb = 128
Do
cb = cb * 2
ReDim dwProcID(1 To (cb \ 4)) As Long
Call EnumProcesses(dwProcID(1), cb, cbNeeded)
Loop While cbNeeded >= cb
GetNTProcessCountNoComments = cbNeeded \ 4
End Function
Public Function GetNTProcessFileName(ByVal dwProcessID As Long) As String
Dim cnt As Long
Dim hModule As Long
Dim cb As Long
Dim cbNeeded As Long
Dim sModule As String
Dim nSuccess As Long
dwProcessID = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ,
0, dwProcessID)
If dwProcessID <> 0 Then
If EnumProcessModules(dwProcessID, hModule, 4, cbNeeded) <> 0 Then
sModule = Space(MAX_PATH)
nSuccess = GetModuleFileNameEx(dwProcessID, hModule, sModule,
Len(sModule))
If nSuccess > 0 Then
GetNTProcessFileName = Left$(sModule, nSuccess)
End If
End If
Call CloseHandle(dwProcessID)
End If
End Function
"Steve Easton" <admin@xxxxxxxxxxxxx> wrote in message
news:udtE3C2TFHA.2664@xxxxxxxxxxxxxxxxxxxxxxx
> I found a script to detect running process here:
> http://vbnet.mvps.org/index.html?code/system/toolhelpprocesses.htm
>
> However, I modified it to scan the processes and set a string variable to
1 if one certain process
> is running as follows:
>
> Dim hSnapShot As Long
> Dim isrun As String
> Dim uProcess As PROCESSENTRY32
> Dim success As Long
> Const frontp As String = "FRONTPG.EXE"
> hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
> If hSnapShot = -1 Then Exit Sub
> uProcess.dwSize = Len(uProcess)
> success = ProcessFirst(hSnapShot, uProcess)
> If success = 1 Then
> isrun = 0
> Do
> If Left$(uProcess.szExeFile, 11) = frontp Then isrun = isrun +
1
> Loop While ProcessNext(hSnapShot, uProcess)
> End If
> Call CloseHandle(hSnapShot)
>
> At this point if FRONTPG.EXE is running the value isrun = 1
> and I use it to open a message box alerting the user to close FrontPage
before continuing.
>
> Problem is, it won't work in windows NT because CreateToolhelp32Snapshot
isn't available on NT
>
> So, my questions are, Is there a better way to do this and is there a way
that's compatible with NT.
>
> tyia
>
>
> --
> Steve Easton
> Microsoft MVP FrontPage
> 95isalive
> This site is best viewed............
> .......................with a computer
>
>
.
- References:
- Detecting a running process.
- From: Steve Easton
- Detecting a running process.
- Prev by Date: Controlling Windows Explorer with API
- Next by Date: Need urgent help
- Previous by thread: Re: Detecting a running process.
- Next by thread: LogonUser & CreateProcessAsUser
- Index(es):
Relevant Pages
|