Find a system process by it's name (and check whether or not it is running)

promalley2

New Member
Joined
Nov 16, 2009
Messages
24
Greetings!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
I’m creating a number of routines and functions that work in conjunction with my CAD software’s VB API. In order for the VB API to function properly, it requires two environment variables with specific names and one system process to be running (which is started when a new asynchronous connection and session is established between an application and the CAD software). I’ve been able to successfully check whether the two environment variables exist by calling them by their specific names, but I’m having trouble figuring out how to identify if the unique system process is running. For this discussion, let’s call the name of the unique process “namespc.”<o:p></o:p>
<o:p> </o:p>
In my research prior to posting, I haven’t been able to locate a way to look at the list of system processes running outside of the windows API—which is not something I’m familiar with at all, but I’m open to learn if someone can point me to the right resources. But I’m looking for suggestions as to what ways there are to examine the list of system processes and which one might be considered “the best.”<o:p></o:p>
<o:p> </o:p>
Again, all I’m looking to do here is look at the list of system processes by name and see if “namespc” is there. And if “namespc” is contained in the list of system processes, I’d like to check to see if it’s running (or is it the case where if the process is in the list, then it is considered to be running?).<o:p></o:p>
<o:p> </o:p>
Thanks in advance for taking the time!<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
--PRO<o:p></o:p>
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
PRO

The only way I can think of doing something like this is to use some sort of API.

When I search I keep on coming across something called PSAPI (process status API).

I did find an example of enumerating the current processes but it was written in C (or some flavour of C).

It's been a long time since I've used any API so I couldn't tell you if that would be any use to you, or even how to use it.

Here's a link anyway.

PS I'm probably totally misinterpreting your question but, hey worth a try.:)
 

promalley2

New Member
Joined
Nov 16, 2009
Messages
24
Thanks for your reply!


That's a good question: I don't know if CreateObject(Class as String, [ServerName as String]) starts a system process or not. It seems reasonable that it might, but I'm not familiar with how to access the process or through what class I might be calling it. (Basically, I don't know what argument I would put into "Class".) Is the class I'd be calling one from the classes contained within the CAD software API? I could take a look in the documentation....

I'm looking for something similar to the Visual Basic language line below:

Code:
If Not System.Diagnostics.Process.GetProcessesByName("namespc").Length > 0 Then
            errMsg = "Name service is not running on the system."
        End If

Obviously I don't expect there to be an exact match in VBA but I would like to accomplish the same basic thing. I'm fairly newer to VBA as a language, so I'm just not really sure where to start looking in the Excel object model (or perhaps I'll have to add a reference to another object model?) for how to get into the list of system processes from Excel through VBA.

Regards,

PRO
 

promalley2

New Member
Joined
Nov 16, 2009
Messages
24

ADVERTISEMENT

Norie,

I don't think you're misinterpreting my question at all. In fact, I was pretty sure I was going to have to use some sort of windows API calls to get into the system processes. I have to find some way to get into the system processes from Excel VBA--API calls may be the only way, I don't know...that's part of my initial question. (I was hoping there was going to be a straightforward way, but it's looking like there isn't one...?) Thanks for the link to the documentation, I'll check it out--but at a glance it seems to be on the right track. I just wish I had more understanding about the basics of making API calls...

Thanks again
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
I know you can definitely access APIs from Excel VBA, I've definitely done it before and you've probably done something similar yourself.

It's been a long time since I've done anything with APIs and I recall it did seem quite intimidating but once you get the hang of things it's not too bad.

I'll see if I can dig out any old code that used the Win API.

If I recall I just was just mucking about doing things like enumerating/terminating windows etc.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

I had a look around about listing processes and managed to come up with some pretty shaky code.

It appears to work to some extent, for example the no of processes matches what's shown in Task Manager.

One problem is that it doesn't seem to find a name or other information for all processes.

The main reason for that is probably me, I basically picked out code that looked like it might work and cobbled it together.

I'd post it but it really is a bit of a mess and I'd prefer to find out what's going on, especially how the API functions work, what they return etc.

If you search on something like 'PSAPI VB' you should find stuff - that's what I did.:)
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,253
Office Version
  1. 2016
Platform
  1. Windows
You could use the Windows Management Instrumentation Interface(WMI) which easier to use than the Corresponding API functions.

Here is a generic Custom Function that checks if a given Process/Application is currently running :

Code:
Private Function IsProcessRunning(ByVal ProcName As String) As Boolean

    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strList
    
    strComputer = "."
    
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
    
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process")
    
    For Each objProcess In colProcess
        If CBool(InStr(1, objProcess.Name, ProcName, vbTextCompare)) Then
            IsProcessRunning = True
            Exit Function
        End If
    Next

End Function

Sub test()
 
    MsgBox IsProcessRunning("namespc")
    
End Sub
 

promalley2

New Member
Joined
Nov 16, 2009
Messages
24
Apologies for the tardiness of a reply, but as I was looking at some old posts I realized I never officially responded to announce that Jafar came up with the best solution--utilize the WMI interface. Thank you for the suggestion and, of course, the examle code. As some may already know, the WMI is a very extensive interface but it's fairly easy to use in lieu of API style calls. Alot of great information about the WMI is available through Microsoft at http://msdn.microsoft.com/en-us/library/aa394582(VS.85).aspx. You can also download Scriptomatic v2.0 from Microsoft which is an application that generates code using the WMI if you know the namespace you want to work with.

Thanks again!
 

Forum statistics

Threads
1,137,154
Messages
5,679,914
Members
419,862
Latest member
Bluewings666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top