Skip to content

Commit

Permalink
Added stopping of dependent services of Windows Search service.
Browse files Browse the repository at this point in the history
  • Loading branch information
raspopov committed May 16, 2021
1 parent 1db9cfc commit f3e961e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions SearchManager/SearchManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ CString ProgIDFromProtocol(LPCTSTR szProtocol)
return CString();
}

DWORD StopService(LPCTSTR szService, bool& bWasStarted)
DWORD StopService(LPCTSTR szService, bool& bWasStarted, bool bDisable)
{
bWasStarted = false;

Expand All @@ -252,10 +252,10 @@ DWORD StopService(LPCTSTR szService, bool& bWasStarted)
{
if ( SC_HANDLE scm = OpenSCManager( nullptr, nullptr, SC_MANAGER_ALL_ACCESS ) )
{
if ( SC_HANDLE service = OpenService( scm, szService, SERVICE_STOP | SERVICE_QUERY_STATUS | SERVICE_CHANGE_CONFIG ) )
if ( SC_HANDLE service = OpenService( scm, szService, SERVICE_STOP | SERVICE_QUERY_STATUS | ( bDisable ? SERVICE_CHANGE_CONFIG : 0 ) | SERVICE_ENUMERATE_DEPENDENTS ) )
{
if ( ChangeServiceConfig( service, SERVICE_NO_CHANGE, SERVICE_DISABLED, SERVICE_NO_CHANGE,
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr ) )
if ( bDisable == false ||
ChangeServiceConfig( service, SERVICE_NO_CHANGE, SERVICE_DISABLED, SERVICE_NO_CHANGE, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr ) )
{
// Try to stop service for 10 seconds
res = ERROR_TIMEOUT;
Expand All @@ -278,7 +278,31 @@ DWORD StopService(LPCTSTR szService, bool& bWasStarted)
if ( ! ControlService( service, SERVICE_CONTROL_STOP, &status ) )
{
res = GetLastError();

// Check and stop all dependent services
if ( res == ERROR_DEPENDENT_SERVICES_RUNNING )
{
DWORD dwBytesNeeded = 0, dwCount = 0;
if ( ! EnumDependentServices( service, SERVICE_ACTIVE, nullptr, 0, &dwBytesNeeded, &dwCount ) )
{
res = GetLastError();
if ( res == ERROR_MORE_DATA )
{
CAutoVectorPtr< char > buf( new char[ dwBytesNeeded ] );
LPENUM_SERVICE_STATUS lpDependencies = reinterpret_cast< LPENUM_SERVICE_STATUS >( static_cast< char * >( buf ) );
if ( EnumDependentServices( service, SERVICE_ACTIVE, lpDependencies, dwBytesNeeded, &dwBytesNeeded, &dwCount ) )
{
for ( DWORD j = 0; j < dwCount; ++j )
{
bool started = false;
StopService( lpDependencies[ j ].lpServiceName, started, false );
}
}
}
}
}
}

SleepEx( 250 , FALSE );
}
}
Expand Down
2 changes: 1 addition & 1 deletion SearchManager/SearchManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ LSTATUS RegSetValueFull(HKEY hKey, LPCTSTR lpSubKey, LPCTSTR lpValue, DWORD dwTy
CString ProgIDFromProtocol(LPCTSTR szProtocol);

// Disable and stop service
DWORD StopService(LPCTSTR szService, bool& bWasStarted);
DWORD StopService(LPCTSTR szService, bool& bWasStarted, bool bDisable = true);

// Enable and start service
DWORD StartService(LPCTSTR szService);
Expand Down

0 comments on commit f3e961e

Please sign in to comment.