Home > Articles > Operating Systems, Server > Microsoft Windows Server

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss

Sorry, this author hasn't posted any blogs.

Adding Some Extras

The WMIProcCheck.Process component also provides the ability to start and stop a service using its StartProc and StopProc methods. Both methods accept the index into the processes array in order to indicate which process to stop. Rather than use ExecQuery and WQL to obtain a reference to the SWbemObject object, these methods use the alternative Get method of the SWbemServices object. The Get method is passed the equivalent of the WHERE clause, which contains the class name and returns the reference. In the following code snippet the variable ServiceName is assumed to hold the name of an NT Service to query for.

Dim ServiceObject As SWbemObject
Dim ServiceName As String

Set ServiceObject = objServices.Get("Win32_Service='" & ServiceName & "'")
ServiceObject.StartService

After the reference is obtained, you can once again call a method on the class(such as StartService) dynamically through automation. Note that for this component, the StartProc and StopProc methods simply return as False if the process is not an NT service.

The custom component's second feature is that it can return the information about the processes in an XML string as well as in an array. This is obviously beneficial for returning information through an ASP page that is to be displayed in MSIE. The GetXML method of the component uses the MSXML parser to create a simple XML document by looping through the array and creating elements with the appropriate attributes for each process as shown in the Listing below. The resulting XML for the notepad and SQL Server processes (discussed previously) follows:

<ProcessData>
     <Process DisplayName="notepad" ProcName="notepad.exe" 
        Running="-1" IsService="0" PathMode="C:\WINNT\System32\notepad.exe"
        Account="SSOSA\Administrator" ServerName="ssosa"/>   
     <Process DisplayName="SQL Server" ProcName="MSSQLServer" 
       Running="-1" IsService="-1" PathMode="Auto" Account="LocalSystem"
       ServerName="ssosa"/>
</ProcessData>

Get the XML. The GetXML public function uses the MSXML parser to create an XML document from the internal array.

Public Function GetXML() As String

' Return the data as XML
Dim xmlDoc As DOMDocument
Dim xmlElem As IXMLDOMElement
Dim xmlSubElem As IXMLDOMElement
Dim i As Long

If IsArray(mProcesses) Then

    Set xmlDoc = New DOMDocument

    ' Create the top level element
    Set xmlElem = xmlDoc.createElement("ProcessData")

    ' Loop through the processes and create the sub elements
    For i = 0 To UBound(mProcesses, 2)
        Set xmlSubElem = xmlDoc.createElement("Process")
        xmlSubElem.setAttribute "DisplayName", mProcesses(0, i)
        xmlSubElem.setAttribute "ProcName", mProcesses(1, i)
        xmlSubElem.setAttribute "Running", mProcesses(2, i)
        xmlSubElem.setAttribute "IsService", mProcesses(3, i)
        xmlSubElem.setAttribute "PathMode", mProcesses(4, i)
        xmlSubElem.setAttribute "Account", mProcesses(5, i)
        xmlSubElem.setAttribute "ServerName", mProcesses(6, i)
        xmlElem.appendChild xmlSubElem
        Set xmlSubElem = Nothing
    Next i
    
    ' Append the high level node
    xmlDoc.appendChild xmlElem
    GetXML = xmlDoc.xml
    Set xmlDoc = Nothing
    
Else
    Set xmlDoc = Nothing
    Err.Raise vbObjectError + 5002, "GetXML", "No processes to enumerate"
End If
End Function

In this case, both notepad and SQL Server were found running. Keep in mind that the DMTF is adding XML support to the specification, so look for Microsoft to add intrinsic XML support to the WMI interfaces shortly.

Although this article is not an exhaustive discussion of WMI, this small example is intended to help you understand you can use WMI to your advantage.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

So Far So Good
By John Traenkenschuh on February 2, 2010 No Comments

So far, Win 7 is making a thoroughbred of what has been a plough mule laptop

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

See All Related Blogs

There are currently no related titles. Please check back later.

Informit Network