Seeing if a specific ProcessID is running - through MS Access

Pretty much as per the title, I need to find a way to see if a specifc processID is running, from my Access database.

Any ideas?
Andy BrownDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BardobraveCommented:
0
Andy BrownDeveloperAuthor Commented:
Thanks for that, but I have no experience with .vbs.  I will however take a further look (when I have five minutes).

Thanks again.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Process ID or Window handle?
JimD.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Andy BrownDeveloperAuthor Commented:
I got this bit of code from Peter57r, which is what I am basing this new function on:

Hope that helps.
Declare Function GetCurrentProcessId Lib "kernel32" () As Long

Sub getpid()
Dim ProcThis As Long
ProcThis = GetCurrentProcessId
Debug.Print ProcThis
End Sub

Open in new window

0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
What was the declare statement for this:
GetCurrentProcessId
JimD.
 
0
Andy BrownDeveloperAuthor Commented:
Sorry about that.

'Processor ID
    Declare Function GetCurrentProcessId Lib "kernel32" () As Long
0
JezWaltersCommented:
This should do the trick:
Option Explicit
Option Compare Database

Private Declare Function CloseHandle _
                Lib "kernel32.dll" _
                (ByVal hObject As Long) As Long

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long

Const STATUS_PENDING As Long = &H103
Const STILL_ACTIVE As Long = STATUS_PENDING

Private Declare Function GetExitCodeProcess _
                Lib "kernel32.dll" _
                (ByVal hProcess As Long, _
                 ByRef lpExitCode As Long) As Long

Const PROCESS_QUERY_INFORMATION As Long = &H400

Private Declare Function OpenProcess _
                Lib "kernel32.dll" _
                (ByVal dwDesiredAccess As Long, _
                 ByVal bInheritHandle As Long, _
                 ByVal dwProcId As Long) As Long
                
Public Function ProcessRunning(plngProcessId) As Boolean
    Dim lngHandle As Long
    Dim lngExitCode As Long

    lngHandle = OpenProcess(PROCESS_QUERY_INFORMATION, False, plngProcessId)
    GetExitCodeProcess lngHandle, lngExitCode
    ProcessRunning = (lngExitCode = STILL_ACTIVE)
    CloseHandle lngHandle
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JezWaltersCommented:
As you can see, the ProcessRunning() Function returns True if the process with the specified PID is running, and returns False if it isn't.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Here's two functions.  Check simply checks if the process is running and wait will pause until the process exits.
Enjoy.
JimD.

Public Function CheckIfRunning(lngProcID As Long) As Boolean

          Dim lnghProcess As Long

10        lnghProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, lngProcID)

20        If lnghProcess > 0 Then
30            CheckIfRunning = True
40        Else
50            CheckIfRunning = False
60        End If

End Function

Public Sub WaitWhileRunning(lngProcID As Long)

    Dim lngExitCode As Long
    Dim lnghProcess As Long

10  lngExitCode = STILL_ACTIVE
20  lnghProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, lngProcID)

30  If lnghProcess > 0 Then
40      Do While lngExitCode = STILL_ACTIVE
50          Call GetExitCodeProcess(lnghProcess, lngExitCode)
60          DoEvents
70      Loop
80  End If

End Sub

Open in new window

0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Forgot the declares and constants

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) 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 SetProp Lib "user32" Alias "SetPropA" (ByVal hwnd As Long, ByVal lpString As String,
' Used for wait check.
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400
 
JimD
0
JezWaltersCommented:
According to the API (http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx), you need to call CloseHandle() after you've finished with the process handle:
"When you are finished with the handle, be sure to close it using the CloseHandle function."
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<<you need to call CloseHandle() after you've finished with the process handle:>>
Hum,  I've never bothered and never had a problem as a result of that that I'm aware of.  I'll have to check with process explorer if it's actually an issue or not.  VBA may be forcing a close of the handle as a result of cleaning up the variable when the procedure terminates.
Certainly would not hurt to close it.  Always better to be explicit rather then rely on something to occur on it's own.
JimD.
0
JezWaltersCommented:
I see you point, but I tend to play it by the book when it comes to Windows API calls ... you just never know!  :-)
0
Andy BrownDeveloperAuthor Commented:
Thanks everyone - that's some really good information - much appreciated.
0
JezWaltersCommented:
I took my code (very similar to JDettman's) from an application that was looping until a particular process completed.
If you only want to know if the given process is running or not, the code can actually be simplified to just:

Option Explicit
Option Compare Database

Private Declare Function CloseHandle _
                Lib "kernel32.dll" _
                (ByVal hObject As Long) As Long

Const PROCESS_QUERY_INFORMATION As Long = &H400

Private Declare Function OpenProcess _
                Lib "kernel32.dll" _
                (ByVal dwDesiredAccess As Long, _
                 ByVal bInheritHandle As Long, _
                 ByVal dwProcId As Long) As Long
                
Public Function ProcessRunning(plngProcessId) As Boolean
    Dim lngHandle As Long

    lngHandle = OpenProcess(PROCESS_QUERY_INFORMATION, False, plngProcessId)
    ProcessRunning = Not (lngHandle = 0)
    CloseHandle lngHandle
End Function

Open in new window

0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
FYI, the closehandle should be used.  The handle remains open until MSACCESS.EXE is killed.   Procedure termination does *not* close the handle.
JimD.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.