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>
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,764
Office Version
365
Platform
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
75,764
Office Version
365
Platform
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
75,764
Office Version
365
Platform
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
7,893
Office Version
2016
Platform
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!
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,503
Messages
5,511,697
Members
408,860
Latest member
RajaRavi

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top