Avatar of Cook09
Cook09Flag for United States of America

asked on 

How to get specific PID of a running application

Experts,
What is the best way to code for finding the PID of a running application (ODS.exe)? I need to find this PID, discreetly, for using in AppActivate pid. A number of examples use the "Find all of the running PIDs." But, I'm interested in finding only one.  Part of the purpose is trapping whether the application is already running.
* vba 7VBAMicrosoft Excel

Avatar of undefined
Last Comment
Cook09
Avatar of Kimputer
Kimputer

Most solutions indeed use the "find all the running PIDs" method, you just add an if statement on the image name.
For my VB.net app, it looks like this:

    Function count_threads()
        Dim count As Integer = 0
        For Each p As Process In Process.GetProcesses()
            If p.ProcessName = "ods" Then
                count = count + 1
            End If
        Next
        If count > 1 Then
            count_threads = True
        Else
            count_threads = False
        End If


    End Function

Open in new window


If you need the PID, adjust code to return  p.Id 
Avatar of Cook09
Cook09
Flag of United States of America image

ASKER

I had a little problem with lines 3 & 4 at one point.  How do I set those?
Avatar of Kimputer
Kimputer

The code is already working, no need to set anything?

If you don't fully understand it, you might just debug it line by line. Basically, it's just as I said, go through all running processes, and check if the name is "ods". Which is what you wished for.
Avatar of Cook09
Cook09
Flag of United States of America image

ASKER

For whatever reason the code keeps throwing me an error...and other code comes back with the wrong PID.  I have found one piece of code that works...but it displays all of the task item details for a running process.  How do I write this to just provide the "ProcessID"?

Public Sub getDetailsByAppName()
    Dim objProcessSet As WbemScripting.SWbemObjectSet
    Dim objProcess As WbemScripting.SWbemObject
    Dim objServices As WbemScripting.SWbemServices
    Dim objLocator As WbemScripting.SWbemLocator
    Dim Recordcount As Long
    Dim Field
 
    'set up wmi for local computer querying
    Set objLocator = New WbemScripting.SWbemLocator
    Set objServices = objLocator.ConnectServer(".") 'local
 
    'Get all the gory details for a name of a running application
    Set objProcessSet = objServices.ExecQuery("SELECT * FROM Win32_Process WHERE name = ""notepad.exe""", , 48)     'change the name to process needing task details
 
    Recordcount = 1
    'Loop through each process returned
    For Each objProcess In objProcessSet
        'Loop through each property/field
        For Each Field In objProcess.Properties_
             Debug.Print Recordcount, Field.Name, Field.Value
          Recordcount = Recordcount + 1
        Next
       
    Next
 
    Set objProcessSet = Nothing
    Set objServices = Nothing
    Set objLocator = Nothing
 
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Cook09
Cook09
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo