StartService (advapi32)
Last changed: -203.26.206.130

.
Summary
The StartService function starts a service.

C# Signature:

[DllImport("advapi32", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool StartService(
                IntPtr        hService,
            int        dwNumServiceArgs,
            string[]        lpServiceArgVectors
            );

VB .NET Signature:

Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As IntPtr, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As String) As Boolean

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

C# Sample Code:

//
// Now, start the service on the remote machine
//

Win32.StartService( hService, 0, null ) ;

VB .NET Sample Code:

' Start Service
' When this button is clicked it will attempt to start a service called Service_Name with SERVICE_ALL_ACCESS privelages
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim hSCManager As IntPtr = New IntPtr(OpenSCManager(Nothing, Nothing, SCMAccess.SC_MANAGER_ALL_ACCESS))
    If (hSCManager.ToInt32 = 0) Then
        MsgBox("The service control manager could not be opened!", MsgBoxStyle.Critical)
        Exit Sub
    End If

    Dim scManager As IntPtr = OpenService(hSCManager, "Service_Name", SCAccess.SERVICE_ALL_ACCESS)
    If (scManager.ToInt32 = 0) Then
        MsgBox("The service could not be opened!", MsgBoxStyle.Critical)
        Exit Sub
    End If

    Dim ret As Boolean = StartService(scManager, 0, Nothing)
    If (Not ret) Then
        MsgBox("The service could not be started!", MsgBoxStyle.Critical)
    End If

    CloseServiceHandle(scManager.ToInt32)

    CloseServiceHandle(hSCManager)
End Sub

Alternative Managed API:

Do you know one? Please contribute it!

Documentation

http://msdn.microsoft.com/library/en-us/dllproc/base/startservice.asp?frame=true