Microsoft KB Archive/328429

= INFO: IADsServiceOperations Requires Aministrative rights =

Article ID: 328429

Article Last Modified on 2/12/2004

-

APPLIES TO


 * Microsoft Windows 2000 Standard Edition
 * Microsoft Active Directory Service Interfaces 2.5
 * Microsoft Windows XP Professional
 * Microsoft Active Directory Service Interfaces 2.5

-



This article was previously published under Q328429



SUMMARY
IADsServiceOperations requires that the user instantiating the interface have full control over the service being queried.



MORE INFORMATION
The IADsServiceOperations interface assumes that the caller has full access to all the service information. The interface opens the Service Manager database with full read/write/query privileges. If the object is running in a thread or process in a user context that does not have read/write/query privileges, the interface will fail to return the requested information.

One possible work around is to use the Service Manger APIs directly. Below is a simple console application that illustrates how one can determine if a service is running on a given client.

Only processes with Administrator privileges are able to open a database handle that can be used by the CreateService and LockServiceDatabase functions. The returned handle is only valid for the process that called the OpenSCManager function. It can be closed by calling the CloseServiceHandle function.

// servicestatus.cpp : Defines the entry point for the console application. //


 * 1) include &quot;stdafx.h&quot;
 * 2) include 
 * 3) include 

// // Function stubs // void GetServiceStatus( char *lpService, SC_HANDLE hSM );

int main(int argc, char* argv[]) {  SC_HANDLE hSCM; //   // We are expecting at most two arguments at minimum, 1. // If only // one is present, then it is assumed that we are targeting // the local system and the first argument is the service we  // want to get the status for. //   // If two, then the first is the service we want the status of, // the second is the machine to target. //   if( argc == 2 ) {     //       // We have one argument. Open the local SCM //      hSCM = OpenSCManager( NULL, NULL, GENERIC_READ); if( !hSCM ) {        //          // Error occured opening the local SCM, // display an error and exit //         printf(&quot;Error Opening Service Control Manager : %0X\n&quot;, GetLastError); return 1; }     //       // Display The Service Status //      GetServiceStatus( argv[1], hSCM ); CloseServiceHandle( hSCM ); return 0; }  else if( argc == 3 ) {     //       // We have a target system, open the SCM on that system and display // the service status... //      hSCM = OpenSCManager( argv[2], NULL, GENERIC_READ); if( !hSCM ) {        //          // Error occured opening the local SCM, // display an error and exit //         printf(&quot;Error Opening Service Control Manager  on %s : %0X\n&quot;, argv[2], GetLastError); return 1; }     //       // Display the Service Status //      GetServiceStatus( argv[1], hSCM ); CloseServiceHandle( hSCM ); return 0; }  else {     printf(&quot;Wrong number of arguments\nMust have:\n&quot;); printf(&quot;command SERVICE_TO_CHECK [machine]\n&quot;); return 1; }  return 0; }

void GetServiceStatus( char *lpService, SC_HANDLE hSM ) {  //    // lpService must be the name of the service, not the // display name in the Service Manager it is the // actual name for the service. You can get the service // name from by: // 1. Start Service Manager // 2. Right click the desired service, select properties from the menu // 3. On the GENERAL TAB, find the Service Name: moniker, the name to the //   right will be the service name recognized by the SC Manager //   SC_HANDLE hService; hService = OpenService( hSM, lpService, GENERIC_READ); if( hService ) {     //       // We have a handle to the service. // lets query for its status //      SERVICE_STATUS ssStatus; if( QueryServiceStatus( hService, &ssStatus) ) {        char Buffer[30]; sprintf(Buffer,&quot;%s&quot;,&quot;NO SERVICE STATUS DETECTED&quot;); switch( ssStatus.dwCurrentState ) {        case SERVICE_CONTINUE_PENDING: // The service continue is pending. sprintf( Buffer,&quot;%s&quot;, &quot;SERVICE_CONTINUE_PENDING&quot;); break; case SERVICE_PAUSE_PENDING: // The service pause is pending. sprintf( Buffer,&quot;%s&quot;, &quot;SERVICE_PAUSE_PENDING&quot;); break; case SERVICE_PAUSED: // The service is paused. sprintf( Buffer,&quot;%s&quot;,&quot;SERVICE_PAUSED&quot;); break; case SERVICE_RUNNING: // The service is running. sprintf( Buffer,&quot;%s&quot;, &quot;SERVICE_RUNNING&quot;); break; case SERVICE_START_PENDING: // The service is starting. sprintf( Buffer,&quot;%s&quot;,&quot;SERVICE_START_PENDING&quot;); break; case SERVICE_STOP_PENDING: // The service is stopping. sprintf( Buffer,&quot;%s&quot;,&quot;SERVICE_STOP_PENDING&quot;); break; case SERVICE_STOPPED: // The Service is stopped sprintf( Buffer,&quot;%s&quot;,&quot;SERVICE_STOPPED&quot;); break; }        printf(&quot;Service Status: %s\n&quot;, Buffer); }     else {        printf(&quot;Error retrieving service %s status: %0X\n&quot;, lpService, GetLastError); }     CloseServiceHandle( hService ); }  else {     printf(&quot;Error opening service %s : %0d\n&quot;, lpService, GetLastError); } }



Public MSDN Links
IADsServiceOperations

Service API Overview

OpenSCM anager

Service Security and Access Rights

Related Knowledge Base Articles
Q234001 Managing Services Using ADSI and Windows Script Host

VB Service Management Examples

Q189633 HOWTO: Query a Service for Status and Configuration

VC Service Management Examples

Q245230 HOWTO: Programmatically Stop a Service and Its Dependencies

Keywords: kbinfo kbdswadsi2003swept KB328429

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.