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

SetServiceStatus (Structures)
 
.
Summary
This API allows you top set the status on a Windows Service. This is especially helpful in .NET in the various events you override from the ServiceBase class. For example, in the OnStart event, here is a recommendatation (see below for a custom enumeration with the SERVICE_START_PENDING and other states):

PER 'Programming Server-Side Applications for Microsoft Windows 2000' ISBN: 0-7356-0753-2

        myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
        myServiceStatus.checkPoint = 1;
        myServiceStatus.waitHint = 5000;
        SetServiceStatus(handle, ref myServiceStatus)

PER 'Programming Server-Side Applications for Microsoft Windows 2000' ISBN: 0-7356-0753-2

After your worker thread(s) start and are your service is running, call (before OnStart ends):

        myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
        myServiceStatus.checkPoint = 1;
        myServiceStatus.waitHint = 5000;
        SetServiceStatus(handle, ref myServiceStatus)

        myServiceStatus.currentState = (int)State.SERVICE_RUNNING;
        myServiceStatus.checkPoint = 0;
        myServiceStatus.waitHint = 0;
        SetServiceStatus(handle, ref myServiceStatus);

After your worker thread(s) start and are your service is running, call (before OnStart ends):

For more on writing services in .NET please search Google on: 'SetServiceStatus' and 'ServiceBase'

        myServiceStatus.currentState = (int)State.SERVICE_RUNNING;
        myServiceStatus.checkPoint = 0;
        myServiceStatus.waitHint = 0;
        SetServiceStatus(handle, ref myServiceStatus);

C# Definition:

   [DllImport("advapi32.dll")]
   private static extern bool SetServiceStatus(IntPtr hServiceStatus, ref SERVICE_STATUS lpServiceStatus);

For more on writing services in .NET please search Google on: 'SetServiceStatus' and 'ServiceBase'

C# Definition:

   [StructLayout(LayoutKind.Sequential)]
   public struct ServiceStatus {
       public long dwServiceType;
       public ServiceState dwCurrentState;
       public long dwControlsAccepted;
       public long dwWin32ExitCode;
       public long dwServiceSpecificExitCode;
       public long dwCheckPoint;
       public long dwWaitHint;
   };
   [DllImport("advapi32.dll")]
   private static extern bool SetServiceStatus(IntPtr hServiceStatus, ref ServiceStatus lpServiceStatus);

VB Definition:

Declare Function SetServiceStatus Lib "advapi32.dll" (

    ByVal hServiceStatus As IntPtr,
    ByRef lpServiceStatus As SERVICE_STATUS)
    As Integer

User-Defined Field Types:

VB Definition:

Declare Function SetServiceStatus Lib "advapi32.dll" (

    ByVal hServiceStatus As IntPtr,
    ByRef lpServiceStatus As SERVICE_STATUS)
    As Integer

    public enum State
    {
    SERVICE_STOPPED = 0x00000001,
    SERVICE_START_PENDING = 0x00000002,
    SERVICE_STOP_PENDING = 0x00000003,
    SERVICE_RUNNING = 0x00000004,
    SERVICE_CONTINUE_PENDING = 0x00000005,
    SERVICE_PAUSE_PENDING = 0x00000006,
    SERVICE_PAUSED = 0x00000007,
    }

User-Defined Field Types:

   [StructLayout(LayoutKind.Sequential)]
   public struct SERVICE_STATUS {
       public long serviceType;
       public State currentState;
       public long controlsAccepted;
       public long win32ExitCode;
       public long serviceSpecificExitCode;
       public long checkPoint;
       public long waitHint;
   };
    public enum State
    {
    SERVICE_STOPPED = 0x00000001,
    SERVICE_START_PENDING = 0x00000002,
    SERVICE_STOP_PENDING = 0x00000003,
    SERVICE_RUNNING = 0x00000004,
    SERVICE_CONTINUE_PENDING = 0x00000005,
    SERVICE_PAUSE_PENDING = 0x00000006,
    SERVICE_PAUSED = 0x00000007,
    }

Notes:

1) The Microsoft MSDN Site is incorrect in their example for C#! They do not call the lpServiceStatus argument using the ref modifier.

Notes:

2) To have this work on all platforms (64-bit and 32-bit) you must use the IntPtr. Never use an int/Integer as it does not adapt.

1) The Microsoft MSDN Site is incorrect in their example for C#! They do not call the lpServiceStatus argument using the ref modifier.

2) To have this work on all platforms (64-bit and 32-bit) you must use the IntPtr. Never use an int/Integer as it does not adapt.

Documentation

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

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