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

CreateService (advapi32)
 
.
Summary
Creates an NT service object and adds it to the specified service control manager database

C# Signature:

[DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern IntPtr CreateService(
    IntPtr hSCManager,
    string lpServiceName,
    string lpDisplayName,
    uint dwDesiredAccess,
    uint dwServiceType,
    uint dwStartType,
    uint dwErrorControl,
    string lpBinaryPathName,
    string lpLoadOrderGroup,
    out uint lpdwTagId,
    string lpDependencies,
    string lpServiceStartName,
    string lpPassword);

VB Signature:

    <DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function CreateService(ByVal hSCManager As IntPtr, ByVal serviceName As String, _
                ByVal displayName As String, ByVal desiredAccess As Int32, ByVal serviceType As Int32, _
                ByVal startType As Int32, ByVal errorcontrol As Int32, ByVal binaryPathName As String, _
                ByVal loadOrderGroup As String, ByVal TagBY As Int32, ByVal dependencides As String, _
                ByVal serviceStartName As String, ByVal password As String) As IntPtr

User-Defined Types:

None.

C# User-Defined Types:

See
ACCESS_MASK definition

    /// <summary>
    /// Access to the service. Before granting the requested access, the
    /// system checks the access token of the calling process.
    /// </summary>
   [Flags]
    public enum SERVICE_ACCESS : uint
    {
    STANDARD_RIGHTS_REQUIRED = 0xF0000,

    /// <summary>
    /// Required to call the QueryServiceConfig and
    /// QueryServiceConfig2 functions to query the service configuration.
    /// </summary>
    SERVICE_QUERY_CONFIG = 0x00001,

    /// <summary>
    /// Required to call the ChangeServiceConfig or ChangeServiceConfig2 function
    /// to change the service configuration. Because this grants the caller
    /// the right to change the executable file that the system runs,
    /// it should be granted only to administrators.
    /// </summary>
    SERVICE_CHANGE_CONFIG = 0x00002,

    /// <summary>
    /// Required to call the QueryServiceStatusEx function to ask the service
    /// control manager about the status of the service.
    /// </summary>
    SERVICE_QUERY_STATUS = 0x00004,

    /// <summary>
    /// Required to call the EnumDependentServices function to enumerate all
    /// the services dependent on the service.
    /// </summary>
    SERVICE_ENUMERATE_DEPENDENTS = 0x00008,

    /// <summary>
    /// Required to call the StartService function to start the service.
    /// </summary>
    SERVICE_START = 0x00010,

    /// <summary>
    ///     Required to call the ControlService function to stop the service.
    /// </summary>
    SERVICE_STOP = 0x00020,

    /// <summary>
    /// Required to call the ControlService function to pause or continue
    /// the service.
    /// </summary>
    SERVICE_PAUSE_CONTINUE = 0x00040,

    /// <summary>
    /// Required to call the EnumDependentServices function to enumerate all
    /// the services dependent on the service.
    /// </summary>
    SERVICE_INTERROGATE = 0x00080,

    /// <summary>
    /// Required to call the ControlService function to specify a user-defined
    /// control code.
    /// </summary>
    SERVICE_USER_DEFINED_CONTROL = 0x00100,

    /// <summary>
    /// Includes STANDARD_RIGHTS_REQUIRED in addition to all access rights in this table.
    /// </summary>
    SERVICE_ALL_ACCESS = (ACCESS_MASK.STANDARD_RIGHTS_REQUIRED |
        SERVICE_QUERY_CONFIG |
        SERVICE_CHANGE_CONFIG |
        SERVICE_QUERY_STATUS |
        SERVICE_ENUMERATE_DEPENDENTS |
        SERVICE_START |
        SERVICE_STOP |
        SERVICE_PAUSE_CONTINUE |
        SERVICE_INTERROGATE |
        SERVICE_USER_DEFINED_CONTROL),

    /// <summary>
    /// Required to call the QueryServiceObjectSecurity or
    /// SetServiceObjectSecurity function to access the SACL. The proper
    /// way to obtain this access is to enable the SE_SECURITY_NAME
    /// privilege in the caller's current access token, open the handle
    /// for ACCESS_SYSTEM_SECURITY access, and then disable the privilege.
    /// </summary>
    ACCESS_SYSTEM_SECURITY = ACCESS_MASK.ACCESS_SYSTEM_SECURITY,

    /// <summary>
    /// Required to call the DeleteService function to delete the service.
    /// </summary>
    DELETE = ACCESS_MASK.DELETE,

    /// <summary>
    /// Required to call the QueryServiceObjectSecurity function to query
    /// the security descriptor of the service object.
    /// </summary>
    READ_CONTROL = ACCESS_MASK.READ_CONTROL,

    /// <summary>
    /// Required to call the SetServiceObjectSecurity function to modify
    /// the Dacl member of the service object's security descriptor.
    /// </summary>
    WRITE_DAC = ACCESS_MASK.WRITE_DAC,

    /// <summary>
    /// Required to call the SetServiceObjectSecurity function to modify
    /// the Owner and Group members of the service object's security
    /// descriptor.
    /// </summary>
    WRITE_OWNER = ACCESS_MASK.WRITE_OWNER,
    }

    /// <summary>
    /// Service types.
    /// </summary>
    [Flags]
    public enum SERVICE_TYPE : uint
    {
    /// <summary>
    /// Driver service.
    /// </summary>
    SERVICE_KERNEL_DRIVER = 0x00000001,

    /// <summary>
    /// File system driver service.
    /// </summary>
    SERVICE_FILE_SYSTEM_DRIVER = 0x00000002,

    /// <summary>
    /// Service that runs in its own process.
    /// </summary>
    SERVICE_WIN32_OWN_PROCESS = 0x00000010,

    /// <summary>
    /// Service that shares a process with one or more other services.
    /// </summary>
    SERVICE_WIN32_SHARE_PROCESS = 0x00000020,

    /// <summary>
    /// The service can interact with the desktop.
    /// </summary>
    SERVICE_INTERACTIVE_PROCESS = 0x00000100,
    }

    /// <summary>
    /// Service start options
    /// </summary>
    public enum SERVICE_START : uint
    {    
    /// <summary>
    /// A device driver started by the system loader. This value is valid
    /// only for driver services.
    /// </summary>
    SERVICE_BOOT_START = 0x00000000,

    /// <summary>
    /// A device driver started by the IoInitSystem function. This value
    /// is valid only for driver services.
    /// </summary>
    SERVICE_SYSTEM_START = 0x00000001,

    /// <summary>
    /// A service started automatically by the service control manager
    /// during system startup. For more information, see Automatically
    /// Starting Services.
    /// </summary>        
    SERVICE_AUTO_START = 0x00000002,

    /// <summary>
    /// A service started by the service control manager when a process
    /// calls the StartService function. For more information, see
    /// Starting Services on Demand.
    /// </summary>
    SERVICE_DEMAND_START = 0x00000003,

    /// <summary>
    /// A service that cannot be started. Attempts to start the service
    /// result in the error code ERROR_SERVICE_DISABLED.
    /// </summary>
    SERVICE_DISABLED = 0x00000004,      
    }

    /// <summary>
    /// Severity of the error, and action taken, if this service fails
    /// to start.
    /// </summary>
    public enum SERVICE_ERROR
    {      
    /// <summary>
    /// The startup program ignores the error and continues the startup
    /// operation.
    /// </summary>
    SERVICE_ERROR_IGNORE = 0x00000000,

    /// <summary>
    /// The startup program logs the error in the event log but continues
    /// the startup operation.
    /// </summary>
    SERVICE_ERROR_NORMAL = 0x00000001,

    /// <summary>
    /// The startup program logs the error in the event log. If the
    /// last-known-good configuration is being started, the startup
    /// operation continues. Otherwise, the system is restarted with
    /// the last-known-good configuration.
    /// </summary>
    SERVICE_ERROR_SEVERE = 0x00000002,

    /// <summary>
    /// The startup program logs the error in the event log, if possible.
    /// If the last-known-good configuration is being started, the startup
    /// operation fails. Otherwise, the system is restarted with the
    /// last-known good configuration.
    /// </summary>
    SERVICE_ERROR_CRITICAL = 0x00000003,
    }

Notes:

The 4th parameter from the end, TagBY, should - accordig to the API documention, be a ByRef parameter. Unfortunately, the call only works if the parameter is ByVal.

lpDependencies, if not null, must be a series of strings concatenated with the null character as a delimiter, including a trailing one.

Tips & Tricks:

Please add some!

Sample Code:

Dim scHandle As IntPtr = OpenSCManager(Nothing, Nothing, SC_MANAGER_ALL_ACCESS)
    Dim serviceName As String = "AAATestService"
    Dim displayName As String = "AAATestDisplayName"

    If OpenFileDialog1.ShowDialog() <> DialogResult.OK Then
        MsgBox("aborting")
    End If

    Dim pathName As String = Chr(34) & OpenFileDialog1.FileName & Chr(34)

    Dim serviceHandle As IntPtr = CreateService(scHandle, serviceName, displayName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, _
                SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, pathName, Nothing, 0, Nothing, Nothing, Nothing)
    If serviceHandle.Equals(IntPtr.Zero) Then
        MsgBox(Marshal.GetLastWin32Error())
    End If

    CloseServiceHandle(serviceHandle)
    CloseHandle(scHandle)

Alternative Managed API:

System.ServiceProcess.ServiceInstaller - http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemServiceProcessServiceInstallerClassTopic.asp

Documentation

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
Find References
Show Printable Version
Revisions