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!
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.
State of the services to be enumerated by EnumServicesStatus
10/7/2010 5:29:50 PM - -208.104.90.130
TODO - a short description
10/7/2010 5:31:58 PM - -208.104.90.130
The ENUM_SERVICE_STATUS structure is used by the EnumDependentServices and EnumServicesStatus functions to return the name of a service in a service control manager database and to return information about that service.
3/16/2007 2:16:14 PM - Jack Hudler-216.198.83.226
The OpenSCManager function establishes a connection to the service control manager on the specified computer and opens the specified service control manager database.
6/25/2007 1:02:22 PM - -74.97.75.158
The EnumServicesStatus function enumerates services in the specified service control manager database. The name and status of each service are provided.
1/27/2012 1:41:10 AM - -74.56.93.131
The OpenService function opens an existing service.
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).