Month: <span>September 2009</span>

After reading this post at StackOverflow, I realized that I was probably wrong to think it was OK to directly access the registry keys that Windows uses to keep track of installed services. Though the names and locations of the keys probably won’t change any time soon, it’s always better to use a higher level of abstraction when dealing with Windows internals. In this case, WMI fits the bill very nicely.

I had not written any WMI-related code up til now, simply because I’ve always been able to find other methods to get the information I need. But after reading that post and knowing of a particular program of mine that does access the registry directly to determine the start mode for a service, I decided to re-write that code to use WMI via the System.Management namespace in the .NET framework.

I came across a couple of different C# samples on the web and was able to modify them to fit my needs. Below is a new class I created for service management that will hopefully grow over time.

Imports System.Management

'''
''' Used to manage Windows services.
''' 
Public Class ServiceManagement
    'Enumeration of service startup modes
    Public Enum ServiceStartMode
        Boot = 0
        System = 1
        Automatic = 2
        Manual = 3
        Disabled = 4
    End Enum

    'Return values for service-related WMI routines
    Public Enum ReturnValue
        Success = 0
        NotSupported = 1
        AccessDenied = 2
        DependentServicesRunning = 3
        InvalidServiceControl = 4
        ServiceCannotAcceptControl = 5
        ServiceNotActive = 6
        ServiceRequestTimeout = 7
        UnknownFailure = 8
        PathNotFound = 9
        ServiceAlreadyRunning = 10
        ServiceDatabaseLocked = 11
        ServiceDependencyDeleted = 12
        ServiceDependencyFailure = 13
        ServiceDisabled = 14
        ServiceLogonFailure = 15
        ServiceMarkedForDeletion = 16
        ServiceNoThread = 17
        StatusCircularDependency = 18
        StatusDuplicateName = 19
        StatusInvalidName = 20
        StatusInvalidParameter = 21
        StatusInvalidServiceAccount = 22
        StatusServiceExists = 23
        ServiceAlreadyPaused = 24
        ServiceNotFound = 25
    End Enum

    '''
    ''' Gets the current start mode for the given service.
    ''' 
    '''Name of an installed service.
    ''' Numerical code indicating the service's start mode.
    Public Shared Function GetStartMode(ByVal serviceName As String) As ServiceStartMode
        Dim service As ManagementObject = Nothing
        Dim servicePath As String
        Dim startMode As String

        GetStartMode = ServiceStartMode.Disabled

        Try
            servicePath = String.Format("Win32_Service.Name=""{0}""", serviceName)
            service = New ManagementObject(servicePath)
            startMode = service.GetPropertyValue("StartMode").ToString

            'We can't return the exact property value because StartMode will
            'show as 'Auto' rather than 'Automatic'. Since the ChangeStartMode 
            'WMI method will need to have 'Automatic' passed when setting the 
            'service to auto-start, our ServiceStartMode enum uses 'Automatic'
            Select Case startMode
                Case "Boot"
                    Return ServiceStartMode.Boot
                Case "System"
                    Return ServiceStartMode.System
                Case "Auto"
                    Return ServiceStartMode.Automatic
                Case "Manual"
                    Return ServiceStartMode.Manual
                Case "Disabled"
                    Return ServiceStartMode.Disabled
            End Select
        Catch
            Throw
        Finally
            If service IsNot Nothing Then
                service.Dispose()
            End If
        End Try
    End Function

    '''
    ''' Sets the start mode for the given service.
    ''' 
    '''Name of an installed service.
    '''Numerical code representing the new start mode.
    ''' Error code indicating the result of the change operation.
    Public Shared Function SetStartMode(ByVal serviceName As String, ByVal startMode As ServiceStartMode) 
                                        As ReturnValue
        Dim service As ManagementObject = Nothing
        Dim inParams As ManagementBaseObject = Nothing
        Dim outParams As ManagementBaseObject = Nothing
        Dim servicePath As String

        SetStartMode = ReturnValue.UnknownFailure

        Try
            servicePath = String.Format("Win32_Service.Name=""{0}""", serviceName)
            service = New ManagementObject(servicePath)
            inParams = service.GetMethodParameters("ChangeStartMode")
            inParams("StartMode") = startMode.ToString()
            outParams = service.InvokeMethod("ChangeStartMode", inParams, Nothing)

            Return DirectCast([Enum].Parse(GetType(ReturnValue), 
                                           outParams("ReturnValue").ToString()), ReturnValue)
        Catch
            Throw
        Finally
            If inParams IsNot Nothing Then
                inParams.Dispose()
            End If

            If outParams IsNot Nothing Then
                outParams.Dispose()
            End If

            If service IsNot Nothing Then
                service.Dispose()
            End If
        End Try
    End Function
End Class

My thanks go out to Mitchel Sellers for informing me that DotNetNuke has a bulk install feature. I had a need to modify an existing Windows service so it could silently update some DNN modules. I was able to make it work in this same service a couple years ago by modifying the DNN 4.3 source code to allow the main DotNetNuke.dll library to function outside of a web application (no easy feat, that). From there I could use the PaInstaller class to load a module. But then it wouldn’t work against 4.9.0 portals.

When I tried to do the same thing with the 4.9.0 source, it failed miserably. It just wouldn’t create an instance of SqlDataProvider, no matter how much I coaxed it. The better way was to have my service download the new module files (zips) to the install/module folder under the portal’s root, create an HttpWebRequest to http://mysite/install/install.aspx?mode=installresources, then parse the response to check for success.

The response parsing was easier than I thought it would be. I did the bulk update first using a web browser so I could see what I was dealing with. The HTML itself was nice in that it put non-blank space elements between each module name and result, so I could use Split() to break everything out. I ignored the first two elements of the resultant array since it contained all the unimportant stuff before the list of modules, then simply looped over the array and verified the word Success was in each even-numbered element, like so:

Dim aResults() As String

aResults = Split(response, "&nbsp;")

If aResults.Length > 0 Then
    For i As Integer = 2 To aResults.Length - 1
        If i Mod 2 = 0 Then
            If Not aResults(i).Contains("Success") Then
                Return False
            End If
        End If
    Next i

    Return True
End If

I’m not sure why I had never heard of this feature over the course of developing several custom DNN modules, even though I’ve spent untold hours sifting through the forums at dotnetnuke.com and the Internet at large for detailed information on the portal’s architecture. In any case, that was the one new thing I learned that day.