Re: Detecting a running process.
- From: "Steve Easton" <admin@xxxxxxxxxxxxx>
- Date: Mon, 2 May 2005 21:40:39 -0400
Hi Randy.
Thanks for the response and feedback.
I need a little time to "absorb" all of this new information..
;-)
--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
"Randy Birch" <rgb_removethis@xxxxxxxx> wrote in message
news:uvRuyU3TFHA.1148@xxxxxxxxxxxxxxxxxxxxxxx
> Hi Steve ...
>
> For NT you have to use the EnumProcesses APIs and friends. Since this is an
> NT4 or later API method, you'll need code from
> http://vbnet.mvps.org/code/helpers/iswinversion.htm to determine the OS
> version and branch accordingly.
>
> Private Const PROCESS_QUERY_INFORMATION As Long = 1024
> Private Const PROCESS_VM_READ As Long = 16
> Private Const MAX_PATH As Long = 260
>
> 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 Sub Command1_Click()
>
> Dim cb As Long
> Dim cbNeeded As Long
> Dim nProcesses As Long
> Dim cnt As Long
> Dim hModule As Long
> Dim sModule As String
> Dim nSuccess As Long
> Dim dwProcessID As Long
>
> 'arbitrary array size to start ...
> 'as long as cbNeeded is more than cb,
> 'increase the array size and call again
> cb = 128
> Do
> cb = cb * 2
> ReDim dwProcID(1 To (cb \ 4)) As Long
> Call EnumProcesses(dwProcID(1), cb, cbNeeded)
> Loop While cbNeeded >= cb
>
> nProcesses = cbNeeded \ 4
>
> If nProcesses > 0 Then
>
> For cnt = 1 To nProcesses
>
> dwProcessID = OpenProcess(PROCESS_QUERY_INFORMATION _
> Or PROCESS_VM_READ, _
> 0, _
> dwProcID(cnt))
>
> 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
> List1.AddItem dwProcID(cnt) & vbTab & Left$(sModule,
> nSuccess)
> End If
>
> End If
>
> Call CloseHandle(dwProcessID)
>
> End If
>
> Next
>
> End If
>
> End Sub
>
>
> --
>
> Randy Birch
> MS MVP Visual Basic
> http://vbnet.mvps.org/
> ----------------------------------------------------------------------------
> Read. Decide. Sign the petition to Microsoft.
> http://classicvb.org/petition/
> ----------------------------------------------------------------------------
>
>
>
> "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
> :
> :
>
.
- Follow-Ups:
- Re: Detecting a running process.
- From: Randy Birch
- Re: Detecting a running process.
- References:
- Detecting a running process.
- From: Steve Easton
- Re: Detecting a running process.
- From: Randy Birch
- Detecting a running process.
- Prev by Date: Re: Detecting a running process.
- Next by Date: Re: Detecting a running process.
- Previous by thread: Re: Detecting a running process.
- Next by thread: Re: Detecting a running process.
- Index(es):
Relevant Pages
|