enumdependentservices (advapi32)
Last changed: anfortas.geo@yahoo.com-216.204.61.86

.
Summary
The EnumDependentServices function retrieves the name and status of each service that depends on the specified service; that is, the specified service must be running before the dependent services can run.

C# Signature:

[DllImport("advapi32.dll", EntryPoint="EnumDependentServicesW", ExactSpelling=true, SetLastError=true)]
static extern bool EnumDependentServices( IntPtr hService,
                       SERVICE_STATES dwServiceState,
                       ref ENUM_SERVICE_STATUS lpServices,
                       int cbBufSize,
                       ref int pcbBytesNeeded,
                       ref int lpServicesReturned );

VB Signature:

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

User-Defined Types:

None.

Notes:

None.

See:

SERVICE_STATES, SERVICE_TYPES, ENUM_SERVICE_STATUS, OpenSCManager, EnumServicesStatus, and OpenService

Tips & Tricks:

Please add some!

Sample Code:

IntPtr handle = OpenSCManager( null, null, SCM_ACCESS.SC_MANAGER_ALL_ACCESS );
if ( handle != IntPtr.Zero )
{
     int iBytesNeeded = 0;
     int iServicesReturned = 0;
     int iResumeHandle = 0;

     if ( !EnumServicesStatus( handle,
                 SERVICE_TYPES.SERVICE_WIN32,
                 SERVICE_STATES.SERVICE_STATE_ALL,
                 IntPtr.Zero,
                 0,
                 ref iBytesNeeded,
                 ref iServicesReturned,
                 ref iResumeHandle ) )
     {
     // Don't forget to test for ERROR_MORE_DATA before proceeding (removed for brevity)
     IntPtr buf = Marshal.AllocHGlobal( (int) iBytesNeeded );
     if ( !EnumServicesStatus( handle,
                     SERVICE_TYPES.SERVICE_WIN32,
                     SERVICE_STATES.SERVICE_STATE_ALL,
                     buf,
                     iBytesNeeded,
                     ref iBytesNeeded,
                     ref iServicesReturned,
                     ref iResumeHandle ) )
     {
         Debug.WriteLine("EnumServicesStatus Failed " + Marshal.GetLastWin32Error() );
     }
     else
     {
         ENUM_SERVICE_STATUS serviceStatus;
         int iPtr = buf.ToInt32();
         for ( int i = 0 ; i < (int) iServicesReturned; i++ )
         {
         serviceStatus = (ENUM_SERVICE_STATUS) Marshal.PtrToStructure( new IntPtr(iPtr), typeof(ENUM_SERVICE_STATUS) );
         Debug.WriteLine("Service " + serviceStatus.pServiceName );
         Debug.Indent();
         IntPtr hService = OpenService( handle, serviceStatus.pServiceName, SERVICE_ACCESS.SERVICE_ALL_ACCESS );
         if ( hService != IntPtr.Zero )
         {
             int iDepBytesNeeded = 0;
             int iDepServicesReturned = 0;

             if ( !EnumDependentServices( hService,
                             SERVICE_STATES.SERVICE_STATE_ALL,
                             IntPtr.Zero,
                             0,
                             ref iDepBytesNeeded,
                             ref iDepServicesReturned ) )
             {
             // Don't forget to test for ERROR_MORE_DATA before proceeding (removed for brevity)
                 IntPtr depBuf = Marshal.AllocHGlobal( (int) iDepBytesNeeded );
             if ( !EnumDependentServices( hService,
                             SERVICE_STATES.SERVICE_STATE_ALL,
                             depBuf,
                             iDepBytesNeeded,
                             ref iDepBytesNeeded,
                             ref iDepServicesReturned ) )
             {
                 Debug.WriteLine("EnumDependentServices Failed " + Marshal.GetLastWin32Error() );
             }
             else
             {
                 ENUM_SERVICE_STATUS depService;
                 int iDepPtr = depBuf.ToInt32();
                 for ( int j = 0 ; j < (int) iDepServicesReturned; j++ )
                 {
                 depService = (ENUM_SERVICE_STATUS) Marshal.PtrToStructure( new IntPtr(iDepPtr), typeof(ENUM_SERVICE_STATUS) );
                 Debug.WriteLine("Dep on " + depService.pServiceName );
                 iDepPtr += ENUM_SERVICE_STATUS.SizeOf;
                 }
             }
             }
             CloseServiceHandle( hService );
         }
         else
         {
             Debug.WriteLine("OpenService Failed " + Marshal.GetLastWin32Error() );
         }
         Debug.Unindent();
         iPtr += ENUM_SERVICE_STATUS.SizeOf;
         }
     }
     }
     CloseServiceHandle( handle );
}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation