Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

StartService (advapi32)
 
.
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

VB Signature:

Declare Function StartService Lib "advapi32.dll" (TODO) As TODO

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

C# Sample Code:

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

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions