vb6windows-serviceswindows-server-2003windows-server-2000

Two different names for Windows services (VB6)


I am having a minor problem automating the starting and stopping of services. When I open services.msc and look at the list of services, they all have names. However, when I run this code:

Dim objService As Object
Dim objSet As Object
IsServiceRunning = False
Set objSet = GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Service")
For Each objService In objSet
    If (UCase(strServiceName) = UCase(objService.Name)) And (UCase(objService.State) = UCase("Running")) Then
        IsServiceRunning = True
    End If
Next

The objService.Name value is not the same as the name in the list. For example, "Computer Browser" is just "browser", "Distributed File System" is "dfs", and "Net Logon" is "netlogon". Is there a way to pull the full, longer name for these services from this objService object? I can workaround this, but for the sake of clarity in the code I'd rather use the same value for determining if the service is running, making a NET START or NET STOP command line call, and logging.


Solution

  • Just use objService.Caption to access "long name" of service.

    I discovered the name of the property like this:

    For Each objService In objSet
        For Each vElem In objService.Properties_
            Debug.Print vElem.Name; "=";
            Debug.Print vElem.Value
        Next
        Exit For
        ...
    Next
    

    Just put objService in watch window to find out Properties_ property. Put vElem in watch window too to find Name and the default property Value (besides IsArray, etc.) of SWbemProperty object.