Solved

Check if process is running from VBA

Posted on 2003-12-04
12
468 Views
Last Modified: 2007-12-19
Hi,  I am using the Shell command to run an external application from an MSACCESS app, and need to be able to check if there is an instance of the program running in memory already.  Any ideas, please?

Thanks!

Terran
0
Comment
Question by:earthman100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 39

Expert Comment

by:stevbe
ID: 9883376
you could do a getobject("....")  and see if you get anything

Dim vobj As Object
Set vobj = GetObject("...")
If vobj Is Nothing Then
   'instance not running
End If

I bet there are api calls that coudl do this faster ... have you looked in the VB section?

Steve
0
 

Author Comment

by:earthman100
ID: 9884866
I haven't looked there, perhaps I should delete this post and post it there?  I have had difficulties with trying VB scripts in VBA before.

I will try you get object idea first...
0
 

Author Comment

by:earthman100
ID: 9911150
I get an error:

"File name or class name not found during Automation operation (Error 432)"

when using:

Dim vobj As Object
Set vobj = GetObject("C:\Program Files\UltraVNC\WinVNC.exe")
If vobj Is Nothing Then
   Shell """C:\Program Files\UltraVNC\WinVNC.exe"""
End If

0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 39

Expert Comment

by:stevbe
ID: 9911704
hmmm .... looks like WinVNC is not an automation server so you will need to iterate all open windows on the PC ... here is some code that shows you how to that ...

http://www.mvps.org/access/api/api0007.htm

Steve
0
 

Author Comment

by:earthman100
ID: 10072685
Actually it is not an open window, just a process.  Any ideas?

Terran
0
 
LVL 39

Expert Comment

by:stevbe
ID: 10072920
what happens if you launch (or try) another instance?

Shell ...
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.

varRet = Shell """C:\Program Files\UltraVNC\WinVNC.exe"""

and depending on what varret gives you ???

Steve
0
 

Author Comment

by:earthman100
ID: 10074777
I will try that and get back to you
0
 

Author Comment

by:earthman100
ID: 10133623
I get different numbers each time, plus it runs the application regardless.  

I want to check if the process is running without running it.

Thanks for your suggestions so far, any others?

0
 
LVL 39

Accepted Solution

by:
stevbe earned 50 total points
ID: 10136723
0
 

Author Comment

by:earthman100
ID: 10138063
Thanks Stevbe,

I used the one at:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20684555.html

and altered it as below in a module. When I call the function "getVNC" it returns True if the process is running.  

Thanks for your help.  Much appreciated!

#############################################

Option Explicit
      Public Declare Function Process32First Lib "kernel32" ( _
         ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

      Public Declare Function Process32Next Lib "kernel32" ( _
         ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

      Public Declare Function CloseHandle Lib "Kernel32.dll" _
         (ByVal Handle As Long) As Long

      Public Declare Function OpenProcess Lib "Kernel32.dll" _
        (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
            ByVal dwProcId As Long) As Long

      Public Declare Function EnumProcesses Lib "psapi.dll" _
         (ByRef lpidProcess As Long, ByVal cb As Long, _
            ByRef cbNeeded As Long) As Long

      Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
         (ByVal hProcess As Long, ByVal hModule As Long, _
            ByVal ModuleName As String, ByVal nSize As Long) As Long

      Public Declare Function EnumProcessModules Lib "psapi.dll" _
         (ByVal hProcess As Long, ByRef lphModule As Long, _
            ByVal cb As Long, ByRef cbNeeded As Long) As Long

      Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
         ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

      Public Declare Function GetVersionExA Lib "kernel32" _
         (lpVersionInformation As OSVERSIONINFO) As Integer

      Public Type PROCESSENTRY32
         dwSize As Long
         cntUsage As Long
         th32ProcessID As Long           ' This process
         th32DefaultHeapID As Long
         th32ModuleID As Long            ' Associated exe
         cntThreads As Long
         th32ParentProcessID As Long     ' This process's parent process
         pcPriClassBase As Long          ' Base priority of process threads
         dwFlags As Long
         szExeFile As String * 260       ' MAX_PATH
      End Type

      Public Type OSVERSIONINFO
         dwOSVersionInfoSize As Long
         dwMajorVersion As Long
         dwMinorVersion As Long
         dwBuildNumber As Long
         dwPlatformId As Long           '1 = Windows 95.
                                        '2 = Windows NT

         szCSDVersion As String * 128
      End Type

      Public Const PROCESS_QUERY_INFORMATION = 1024
      Public Const PROCESS_VM_READ = 16
      Public Const MAX_PATH = 260
      Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
      Public Const SYNCHRONIZE = &H100000
      'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
      Public Const PROCESS_ALL_ACCESS = &H1F0FFF
      Public Const TH32CS_SNAPPROCESS = &H2&
      Public Const hNull = 0

      Public Function getVNC()
     
      Select Case getVers()

      Case 1 'Windows 95/98

         Dim f As Long, sname As String
         Dim hSnap As Long, proc As PROCESSENTRY32
         hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
         If hSnap = hNull Then Exit Function
         proc.dwSize = Len(proc)
         ' Iterate through the processes
         f = Process32First(hSnap, proc)
         Do While f
           sname = StrZToStr(proc.szExeFile)
           Debug.Print sname
           f = Process32Next(hSnap, proc)
         Loop

      Case 2 'Windows NT

         Dim cb As Long
         Dim cbNeeded As Long
         Dim NumElements As Long
         Dim ProcessIDs() As Long
         Dim cbNeeded2 As Long
         Dim NumElements2 As Long
         Dim Modules(1 To 200) As Long
         Dim lRet As Long
         Dim ModuleName As String
         Dim nSize As Long
         Dim hProcess As Long
         Dim i As Long
         'Get the array containing the process id's for each process object
         cb = 8
         cbNeeded = 96
         Do While cb <= cbNeeded
            cb = cb * 2
            ReDim ProcessIDs(cb / 4) As Long
            lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
         Loop
         NumElements = cbNeeded / 4

         For i = 1 To NumElements
            'Get a handle to the Process
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
               Or PROCESS_VM_READ, 0, ProcessIDs(i))
            'Got a Process handle
            If hProcess <> 0 Then
                'Get an array of the module handles for the specified
                'process
                lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                                             cbNeeded2)
                'If the Module Array is retrieved, Get the ModuleFileName
                If lRet <> 0 Then
                   ModuleName = Space(MAX_PATH)
                   nSize = 500
                   lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                                   ModuleName, nSize)
                    If InStr(1, Left(ModuleName, lRet), "WinVNC.exe") > 0 Then
                        Debug.Print Left(ModuleName, lRet)
                        getVNC = True
                    End If
                End If
            End If
          'Close the handle to the process
         lRet = CloseHandle(hProcess)
         Next

      End Select
      End Function



      Function StrZToStr(s As String) As String
         StrZToStr = Left$(s, Len(s) - 1)
      End Function

      Public Function getVers() As Long
         Dim osinfo As OSVERSIONINFO
         Dim retvalue As Integer
         osinfo.dwOSVersionInfoSize = 148
         osinfo.szCSDVersion = Space$(128)
         retvalue = GetVersionExA(osinfo)
         getVers = osinfo.dwPlatformId
      End Function

0
 
LVL 39

Expert Comment

by:stevbe
ID: 10147538
you are welcome, glad I could help, good luck with your project.
0
 

Author Comment

by:earthman100
ID: 10149469
Thanks, I appreciate that!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question