请问各位大侠怎样停止一个服务(用函数),谢谢
请问各位大侠怎样停止一个服务(用函数),谢谢 问题点数:0、回复次数:3Top
1 楼microran2000(什么时候才能看到星星?)回复于 2003-08-04 20:06:28 得分 0
DWORD StopServiceEntry( SC_HANDLE hSCM, SC_HANDLE hService,
BOOL fStopDependencies, DWORD dwTimeout ) {
SERVICE_STATUS ss;
DWORD dwStartTime = GetTickCount();
// Make sure the service is not already stopped
if ( !QueryServiceStatus( hService, &ss ) )
return GetLastError();
if ( ss.dwCurrentState == SERVICE_STOPPED )
return ERROR_SUCCESS;
// If a stop is pending, just wait for it
while ( ss.dwCurrentState == SERVICE_STOP_PENDING ) {
Sleep( ss.dwWaitHint );
if ( !QueryServiceStatus( hService, &ss ) )
return GetLastError();
if ( ss.dwCurrentState == SERVICE_STOPPED )
return ERROR_SUCCESS;
if ( GetTickCount() - dwStartTime > dwTimeout )
return ERROR_TIMEOUT;
}
// If the service is running, dependencies must be stopped first
if ( fStopDependencies ) {
DWORD i;
DWORD dwBytesNeeded;
DWORD dwCount;
LPENUM_SERVICE_STATUS lpDependencies = NULL;
ENUM_SERVICE_STATUS ess;
SC_HANDLE hDepService;
// Pass a zero-length buffer to get the required buffer size
if ( EnumDependentServices( hService, SERVICE_ACTIVE,
lpDependencies, 0, &dwBytesNeeded, &dwCount ) ) {
// If the Enum call succeeds, then there are no dependent
// services so do nothing
} else {
if ( GetLastError() != ERROR_MORE_DATA )
return GetLastError(); // Unexpected error
// Allocate a buffer for the dependencies
lpDependencies = (LPENUM_SERVICE_STATUS) HeapAlloc(
GetProcessHeap(), HEAP_ZERO_MEMORY, dwBytesNeeded );
if ( !lpDependencies )
return GetLastError();
__try {
// Enumerate the dependencies
if ( !EnumDependentServices( hService, SERVICE_ACTIVE,
lpDependencies, dwBytesNeeded, &dwBytesNeeded,
&dwCount ) )
return GetLastError();
for ( i = 0; i < dwCount; i++ ) {
ess = *(lpDependencies + i);
// Open the service
hDepService = OpenService( hSCM, ess.lpServiceName,
SERVICE_STOP | SERVICE_QUERY_STATUS );
if ( !hDepService )
return GetLastError();
__try {
// Send a stop code
if ( !ControlService( hDepService, SERVICE_CONTROL_STOP,
&ss ) )
return GetLastError();
// Wait for the service to stop
while ( ss.dwCurrentState != SERVICE_STOPPED ) {
Sleep( ss.dwWaitHint );
if ( !QueryServiceStatus( hDepService, &ss ) )
return GetLastError();
if ( ss.dwCurrentState == SERVICE_STOPPED )
break;
if ( GetTickCount() - dwStartTime > dwTimeout )
return ERROR_TIMEOUT;
}
} __finally {
// Always release the service handle
CloseServiceHandle( hDepService );
}
}
} __finally {
// Always free the enumeration buffer
HeapFree( GetProcessHeap(), 0, lpDependencies );
}
}
}
// Send a stop code to the main service
if ( !ControlService( hService, SERVICE_CONTROL_STOP, &ss ) )
return GetLastError();
// Wait for the service to stop
while ( ss.dwCurrentState != SERVICE_STOPPED ) {
Sleep( ss.dwWaitHint );
if ( !QueryServiceStatus( hService, &ss ) )
return GetLastError();
if ( ss.dwCurrentState == SERVICE_STOPPED )
break;
if ( GetTickCount() - dwStartTime > dwTimeout )
return ERROR_TIMEOUT;
}
// Return success
return ERROR_SUCCESS;
}
void StopService(LPCTSTR szServiceName)
{
SC_HANDLE hSCM;
SC_HANDLE hService;
DWORD dwError;
if ( szServiceName ==NULL ) {
return ;
}
__try {
// Open the SCM database
hSCM = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT );
if ( !hSCM ) {
__leave;
}
// Open the specified service
hService = OpenService( hSCM, szServiceName, SERVICE_STOP
| SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS );
if ( !hService ) {
__leave;
}
// Try to stop the service, specifying a 30 second timeout
dwError = StopServiceEntry( hSCM, hService, TRUE, 30000 ) ;
} __finally {
if ( hService )
CloseServiceHandle( hService );
if ( hSCM )
CloseServiceHandle( hSCM );
}
}
DWORD StartService(LPCTSTR szServiceName)
{
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwStatus;
SC_HANDLE schService;
SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT );
if ( !schSCManager ) {
return -1;
}
schService = OpenService(
schSCManager, // SCM database
szServiceName, // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
return -1;
}
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
return -1;
}
// Check the status until the service is no longer start pending.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information structure
{
return -1;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of structure
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
dwStatus = NO_ERROR;
}
else
{
dwStatus = GetLastError();
}
CloseServiceHandle(schService);
return dwStatus;
}
Top
2 楼microran2000(什么时候才能看到星星?)回复于 2003-08-04 20:07:32 得分 0
调用
StopService("Messenger");
startService("Messenger");Top
3 楼qqpeng76(pk)回复于 2003-08-04 20:23:34 得分 0
有 StopService 这个方法吗
MSDN 中查不出呀Top




