home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Softwarová Záchrana 3
/
Softwarova-zachrana-3.bin
/
pserv.cpl
/
pserv-2.4.exe
/
source
/
cservice.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-01-05
|
30KB
|
909 lines
#include "stdafx.h"
#include "resource.h"
#include "cservice.h"
#include "WriteOptimizedFile.h"
#include "2kwrapper.h"
#include "cconfiguration.h"
#include "MemoryBuffer.h"
#include "stringresources.h"
CService::CService(CServiceList* owner)
: m_pOwner( owner ),
m_hTempSCM( 0 ),
m_hTempService( 0 ),
m_dwChangeFlags( 0 ),
m_dwErrorControl( 0 ),
m_bRestartPending( FALSE ),
m_dwServiceType( (owner->m_dwEnumType == SERVICE_WIN32) ? SERVICE_WIN32_OWN_PROCESS : SERVICE_KERNEL_DRIVER ),
m_dwStartType( SERVICE_DEMAND_START )
{
ZeroMemory( &m_ServiceStatus, sizeof(m_ServiceStatus) );
}
COLORREF CService::GetTextColor()
{
if(IsRunning())
{
return RGB(0, 0, 200);
}
else if(IsDisabled())
{
return RGB(128,128,128);
}
return RGB(0,0,0);
}
CString CService::GetDisplayString(DWORD dwIndex)
{
switch(dwIndex)
{
case 0: return m_strDisplayName;
case 1: return m_strServiceName;
case 2: return GetStatusAsString();
case 3: return GetStartAsString();
case 4: return GetTypeAsString();
case 5: return m_strBinaryPathName;
case 6: return GetPidAsString();
case 7: return m_strServiceStartName;
case 8: return m_strLoadOrderGroup;
case 9: return m_strDescription;
}
return CString();
}
CString CService::GetInfoTip()
{
return m_strDescription;
}
CService::~CService()
{
FinishStateType();
}
BOOL CService::RefreshServiceStatus()
{
MemoryBuffer mb;
if( lpfnQueryServiceStatusEx )
{
LPSERVICE_STATUS_PROCESS ssp = mb.QueryServiceStatusEx(m_hTempService);
if( ssp )
{
m_ServiceStatus = *ssp;
return TRUE;
}
return FALSE;
}
return QueryServiceStatus( m_hTempService, (LPSERVICE_STATUS) &m_ServiceStatus );
}
BOOL CService::ApplyChanges()
{
// no changes -> success always
if(!m_dwChangeFlags )
return TRUE;
BOOL bSuccess = FALSE;
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, SERVICE_CHANGE_CONFIG|SERVICE_QUERY_STATUS );
if( m_hTempService )
{
LPCTSTR lpszStartName = NULL, lpszStartPassword = NULL;
if( m_dwChangeFlags & CHANGED_LOGON_SYSTEM )
{
lpszStartName = TEXT(".\\LOCALSYSTEM");
lpszStartPassword = TEXT("");
}
else if( m_dwChangeFlags & CHANGED_LOGON_USER )
{
lpszStartName = m_strServiceStartName;
lpszStartPassword = m_strServiceStartPassword;
}
bSuccess = ChangeServiceConfig(
m_hTempService,
(m_dwChangeFlags & CHANGED_ALLOW_UI) ? m_dwServiceType : SERVICE_NO_CHANGE,
(m_dwChangeFlags & CHANGED_START_TYPE) ? m_dwStartType : SERVICE_NO_CHANGE,
(m_dwChangeFlags & CHANGED_ERROR_CONTROL) ? m_dwErrorControl : SERVICE_NO_CHANGE,
(m_dwChangeFlags & CHANGED_BINARYPATH) ? (LPCTSTR) m_strBinaryPathName : NULL,
NULL,
NULL,
NULL,
lpszStartName,
lpszStartPassword,
(m_dwChangeFlags & CHANGED_DISPLAYNAME) ? (LPCTSTR) m_strDisplayName : NULL );
if( bSuccess && (m_dwChangeFlags & CHANGED_DESCRIPTION) && (lpfnChangeServiceConfig2 != NULL))
{
SERVICE_DESCRIPTION sd;
sd.lpDescription = (LPTSTR) (LPCTSTR) m_strDescription;
bSuccess = lpfnChangeServiceConfig2( m_hTempService, SERVICE_CONFIG_DESCRIPTION, &sd );
}
DWORD dwLastError = GetLastError();
RefreshServiceStatus();
CloseServiceHandle( m_hTempService );
m_hTempService = 0;
SetLastError(dwLastError);
}
DWORD dwLastError = GetLastError();
CloseServiceHandle(m_hTempSCM);
m_hTempSCM = 0;
SetLastError(dwLastError);
}
return bSuccess;
}
BOOL CService::Delete()
{
BOOL bSuccess = FALSE;
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, STANDARD_RIGHTS_REQUIRED );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, STANDARD_RIGHTS_REQUIRED );
if( m_hTempService )
{
bSuccess = DeleteService(m_hTempService);
DWORD dwLastError = GetLastError();
CloseServiceHandle( m_hTempService );
m_hTempService = 0;
SetLastError(dwLastError);
}
DWORD dwLastError = GetLastError();
CloseServiceHandle(m_hTempSCM);
m_hTempSCM = 0;
SetLastError(dwLastError);
}
return bSuccess;
}
BOOL CService::Install()
{
BOOL bSuccess = FALSE;
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CREATE_SERVICE );
if( m_hTempSCM )
{
LPCTSTR lpszStartName = NULL, lpszStartPassword = NULL;
if( !IsEmptyString( m_strServiceStartName ) )
{
lpszStartName = m_strServiceStartName;
lpszStartPassword = m_strServiceStartPassword;
}
m_hTempService = CreateService(m_hTempSCM,
m_strServiceName,
m_strDisplayName,
SERVICE_QUERY_CONFIG|SERVICE_QUERY_STATUS|SERVICE_CHANGE_CONFIG,
m_dwServiceType,
m_dwStartType,
m_dwErrorControl,
m_strBinaryPathName,
NULL,
NULL,
NULL,
lpszStartName,
lpszStartPassword );
if( m_hTempService )
{
bSuccess = TRUE;
if( !IsEmptyString(m_strDescription))
{
SERVICE_DESCRIPTION sd;
sd.lpDescription = (LPTSTR) (LPCTSTR) m_strDescription;
bSuccess = lpfnChangeServiceConfig2( m_hTempService, SERVICE_CONFIG_DESCRIPTION, &sd );
}
DWORD dwLastError = GetLastError();
RefreshServiceStatus();
CloseServiceHandle( m_hTempService );
m_hTempService = 0;
SetLastError(dwLastError);
}
DWORD dwLastError = GetLastError();
CloseServiceHandle(m_hTempSCM);
m_hTempSCM = 0;
SetLastError(dwLastError);
}
return bSuccess;
}
BOOL CService::StartAtBoot()
{
if( m_dwStartType != SERVICE_BOOT_START )
{
m_dwStartType = SERVICE_BOOT_START;
m_dwChangeFlags = CHANGED_START_TYPE;
return ApplyChanges();
}
return TRUE;
}
BOOL CService::StartAtSystem()
{
if( m_dwStartType != SERVICE_SYSTEM_START )
{
m_dwStartType = SERVICE_SYSTEM_START;
m_dwChangeFlags = CHANGED_START_TYPE;
return ApplyChanges();
}
return TRUE;
}
BOOL CService::Enable()
{
if( m_dwStartType != SERVICE_DEMAND_START )
{
m_dwStartType = SERVICE_DEMAND_START;
m_dwChangeFlags = CHANGED_START_TYPE;
return ApplyChanges();
}
return TRUE;
}
BOOL CService::Disable()
{
if( m_dwStartType != SERVICE_DISABLED )
{
m_dwStartType = SERVICE_DISABLED;
m_dwChangeFlags = CHANGED_START_TYPE;
return ApplyChanges();
}
return TRUE;
}
BOOL CService::StartAutomatically()
{
if( m_dwStartType != SERVICE_AUTO_START )
{
m_dwStartType = SERVICE_AUTO_START;
m_dwChangeFlags = CHANGED_START_TYPE;
return ApplyChanges();
}
return TRUE;
}
SERVICE_STATE_TYPE CService::BeginStart(LPDWORD lpdwWaitHint)
{
FinishStateType();
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, SERVICE_START|SERVICE_QUERY_STATUS );
if( m_hTempService )
{
if( StartService( m_hTempService, 0, NULL ) )
{
if( RefreshServiceStatus() )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_RUNNING )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_START_PENDING )
{
*lpdwWaitHint = m_ServiceStatus.dwWaitHint;
return SERVICE_STATE_PENDING;
}
}
}
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::BeginStop(LPDWORD lpdwWaitHint)
{
FinishStateType();
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, SERVICE_STOP|SERVICE_QUERY_STATUS );
if( m_hTempService )
{
if( ControlService( m_hTempService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS) &m_ServiceStatus) )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_STOPPED )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
*lpdwWaitHint = m_ServiceStatus.dwWaitHint;
return SERVICE_STATE_PENDING;
}
}
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::BeginRestart(LPDWORD lpdwWaitHint)
{
FinishStateType();
m_bRestartPending = TRUE;
m_bRestartHasBeenStopped = FALSE;
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, SERVICE_STOP|SERVICE_QUERY_STATUS );
if( m_hTempService )
{
if( ControlService( m_hTempService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS) &m_ServiceStatus) )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_STOPPED )
{
// ok, stopped, now restart
m_bRestartHasBeenStopped = TRUE;
return BeginStart(lpdwWaitHint);
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
*lpdwWaitHint = m_ServiceStatus.dwWaitHint + (10 * 1000);
return SERVICE_STATE_PENDING;
}
}
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::BeginPause(LPDWORD lpdwWaitHint)
{
FinishStateType();
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, SERVICE_PAUSE_CONTINUE|SERVICE_QUERY_STATUS );
if( m_hTempService )
{
if( ControlService( m_hTempService, SERVICE_CONTROL_PAUSE, (LPSERVICE_STATUS) &m_ServiceStatus) )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_PAUSED )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_PAUSE_PENDING )
{
*lpdwWaitHint = m_ServiceStatus.dwWaitHint;
return SERVICE_STATE_PENDING;
}
}
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::BeginContinue(LPDWORD lpdwWaitHint)
{
FinishStateType();
m_hTempSCM = OpenSCManager( m_pOwner->m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT );
if( m_hTempSCM )
{
m_hTempService = OpenService(m_hTempSCM, m_strServiceName, SERVICE_PAUSE_CONTINUE|SERVICE_QUERY_STATUS );
if( m_hTempService )
{
if( ControlService( m_hTempService, SERVICE_CONTROL_CONTINUE, (LPSERVICE_STATUS) &m_ServiceStatus) )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_RUNNING )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_CONTINUE_PENDING )
{
*lpdwWaitHint = m_ServiceStatus.dwWaitHint;
return SERVICE_STATE_PENDING;
}
}
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::IsStarted()
{
if( !m_hTempService || RefreshServiceStatus() )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_RUNNING )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_START_PENDING )
{
return SERVICE_STATE_PENDING;
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::IsRestarted()
{
if( !m_hTempService || RefreshServiceStatus() )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_RUNNING )
{
if( m_bRestartHasBeenStopped )
{
return SERVICE_STATE_REACHED;
}
else
{
// unable to restart, abort
return SERVICE_STATE_FAILED;
}
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_STOPPED )
{
if( m_bRestartHasBeenStopped )
{
// unable to restart, abort
return SERVICE_STATE_FAILED;
}
DWORD dwIgnoreMe;
SERVICE_STATE_TYPE result = BeginStart(&dwIgnoreMe);
if( result == SERVICE_STATE_PENDING )
m_bRestartHasBeenStopped = TRUE;
return result;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_START_PENDING )
{
return SERVICE_STATE_PENDING;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
return SERVICE_STATE_PENDING;
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::IsStopped()
{
if( !m_hTempService || RefreshServiceStatus() )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_STOPPED )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING )
{
return SERVICE_STATE_PENDING;
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::IsContinued()
{
if( !m_hTempService || RefreshServiceStatus() )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_RUNNING )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_CONTINUE_PENDING )
{
return SERVICE_STATE_PENDING;
}
}
return SERVICE_STATE_FAILED;
}
SERVICE_STATE_TYPE CService::IsPaused()
{
if( !m_hTempService || RefreshServiceStatus() )
{
if( m_ServiceStatus.dwCurrentState == SERVICE_PAUSED )
{
return SERVICE_STATE_REACHED;
}
else if( m_ServiceStatus.dwCurrentState == SERVICE_PAUSE_PENDING )
{
return SERVICE_STATE_PENDING;
}
}
return SERVICE_STATE_FAILED;
}
void CService::FinishStateType()
{
if( m_hTempService )
{
CloseServiceHandle(m_hTempService);
m_hTempService = 0;
}
if( m_hTempSCM )
{
CloseServiceHandle(m_hTempSCM);
m_hTempSCM = 0;
}
m_bRestartPending = FALSE;
}
CString CService::GetPidAsString()
{
CString result( _T("") );
if( m_ServiceStatus.dwProcessId )
result.Format( _T("%d"), m_ServiceStatus.dwProcessId );
return result;
}
CString CService::GetStatusAsString()
{
switch(m_ServiceStatus.dwCurrentState)
{
case SERVICE_STOPPED:
return strSERVICE_STATUS_STOPPED;
case SERVICE_START_PENDING:
return strSERVICE_STATUS_START_PENDING;
case SERVICE_STOP_PENDING:
return strSERVICE_STATUS_STOP_PENDING;
case SERVICE_RUNNING:
return strSERVICE_STATUS_RUNNING;
case SERVICE_CONTINUE_PENDING:
return strSERVICE_STATUS_CONTINUE_PENDING;
case SERVICE_PAUSE_PENDING:
return strSERVICE_STATUS_PAUSE_PENDING;
case SERVICE_PAUSED:
return strSERVICE_STATUS_PAUSED;
}
CString result;
result.Format(strSERVICE_STATUS_INVALID, m_ServiceStatus.dwCurrentState );
return result;
}
CString CService::GetPrintStatusAsString()
{
switch(m_ServiceStatus.dwCurrentState)
{
case SERVICE_STOPPED:
return _T("Stopped");
case SERVICE_START_PENDING:
return _T("Start pending");
case SERVICE_STOP_PENDING:
return _T("Stop pending");
case SERVICE_RUNNING:
return _T("Running");
case SERVICE_CONTINUE_PENDING:
return _T("Continue pending");
case SERVICE_PAUSE_PENDING:
return _T("Pause pending");
case SERVICE_PAUSED:
return _T("Paused");
}
CString result;
result.Format(_T("<INVALID: 0x%08lx>"), m_ServiceStatus.dwCurrentState );
return result;
}
CString CService::GetStartAsString()
{
return GetStartAsString(m_dwStartType);
}
CString CService::GetStartAsString(DWORD dwStartType)
{
switch(dwStartType)
{
// WARNING: also used in PXmlReader.cpp !!!
case SERVICE_BOOT_START:
return _T("Boot");
case SERVICE_SYSTEM_START:
return _T("System");
case SERVICE_AUTO_START:
return _T("Automatic");
case SERVICE_DEMAND_START:
return _T("Manual");
case SERVICE_DISABLED:
return _T("Disabled");
}
CString result;
result.Format(_T("<INVALID: 0x%08lx>"), dwStartType );
return result;
}
CString CService::GetTypeAsString()
{
CString result;
if( m_dwServiceType & SERVICE_WIN32_OWN_PROCESS )
result += _T("Process " );
if( m_dwServiceType & SERVICE_WIN32_SHARE_PROCESS )
result += _T("Shared ");
if( m_dwServiceType & SERVICE_KERNEL_DRIVER )
result += _T("Driver ");
if( m_dwServiceType & SERVICE_FILE_SYSTEM_DRIVER )
result += _T("File System ");
if( m_dwServiceType & SERVICE_INTERACTIVE_PROCESS )
result += _T("(Interactive) ");
return result;
}
BOOL CService::CreateFrom(SC_HANDLE hSCM, LPENUM_SERVICE_STATUS_PROCESS lpStatus)
{
m_strServiceName = lpStatus->lpServiceName;
m_strDisplayName = lpStatus->lpDisplayName;
m_ServiceStatus = lpStatus->ServiceStatusProcess;
m_strDescription = (LPCSTR) NULL;
SC_HANDLE hService = OpenService(hSCM, m_strServiceName, SERVICE_QUERY_CONFIG );
if( hService )
{
MemoryBuffer mb;
LPQUERY_SERVICE_CONFIG pServiceConfig = mb.QueryServiceConfig(hService);
if( pServiceConfig )
{
m_strBinaryPathName = pServiceConfig->lpBinaryPathName;
m_dwErrorControl = pServiceConfig->dwErrorControl;
m_dwServiceType = pServiceConfig->dwServiceType;
m_dwStartType = pServiceConfig->dwStartType;
m_strServiceStartName = pServiceConfig->lpServiceStartName;
m_strLoadOrderGroup = pServiceConfig->lpLoadOrderGroup;
m_strDescription = mb.GetServiceDescription(hService);
}
CloseServiceHandle(hService);
}
return TRUE;
}
void CServiceList::SetEnumType( DWORD dwEnumType )
{
m_dwEnumType = dwEnumType;
RefreshTitleString();
}
static GENERICCOMPAREFN SortMethods[10] = {
GENERICCOMPAREFN(&CServiceList::SortByDisplayName),
GENERICCOMPAREFN(&CServiceList::SortByServiceName),
GENERICCOMPAREFN(&CServiceList::SortByStatus),
GENERICCOMPAREFN(&CServiceList::SortByStart),
GENERICCOMPAREFN(&CServiceList::SortByType),
GENERICCOMPAREFN(&CServiceList::SortByBinaryPathName),
GENERICCOMPAREFN(&CServiceList::SortByPid),
GENERICCOMPAREFN(&CServiceList::SortByUser),
GENERICCOMPAREFN(&CServiceList::SortByLoadOrderGroup),
GENERICCOMPAREFN(&CServiceList::SortByDescription),
};
CServiceList::CServiceList()
: CListViewEntries( SortMethods, _T("Services") ),
m_dwEnumType( SERVICE_WIN32 )
{
CreateColumns( _T("Service"), _T("Section"), _T("Status"), _T("Start"), _T("Type"), _T("Path"), _T("PID"), _T("User"), _T("Load Order Group"), _T("Description"), NULL );
RefreshTitleString();
// read in m_LoadOrderGroups
HKEY hKey;
LONG lResult = RegCreateKeyEx( HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control\\ServiceGroupOrder"),
0,
_T(""),
0,
KEY_READ,
NULL,
&hKey,
0 );
if( lResult == 0 )
{
DWORD dwType = 0, dwData = 0, dwData2;
RegQueryValueEx(hKey, _T("List"), 0, &dwType, NULL, &dwData );
if( dwData )
{
LPBYTE lpbData = new BYTE[dwData+1];
if( lpbData )
{
dwData2 = dwData;
if( RegQueryValueEx(hKey, _T("List"), 0, &dwType, lpbData, &dwData2 ) == 0 )
{
lpbData[dwData] = 0;
ASSERT( dwType == REG_MULTI_SZ );
LPCTSTR p = (LPCTSTR) lpbData;
while( *p )
{
m_LoadOrderGroups.Add( p );
p += (lstrlen(p)+1)*sizeof(TCHAR);
}
}
delete lpbData;
}
}
RegCloseKey(hKey);
}
}
CServiceList::~CServiceList()
{
}
void CServiceList::ConnectTo( LPCTSTR lpszMachine )
{
m_strServiceMachine = lpszMachine;
RefreshTitleString();
}
void CServiceList::RefreshTitleString()
{
LPCTSTR lpszTag = GetTagString();
LPCTSTR lpszMachine = m_strServiceMachine;
if( !lpszMachine || !*lpszMachine )
{
m_strTitle.Format( TEXT("pserv ") CURRENT_VERSION TEXT(": %s on local machine"), lpszTag );
}
else
{
m_strTitle.Format( TEXT("pserv ") CURRENT_VERSION TEXT(": %s on \\\\%s"), lpszTag, (LPCTSTR)m_strServiceMachine );
}
}
int CServiceList::SortByDisplayName(const CService** ps1, const CService** ps2)
{
return (*ps1)->m_strDisplayName.CompareNoCase((*ps2)->m_strDisplayName);
}
int CServiceList::SortByServiceName(const CService** ps1, const CService** ps2)
{
return (*ps1)->m_strServiceName.CompareNoCase((*ps2)->m_strServiceName);
}
int CServiceList::SortByStatus(const CService** ps1, const CService** ps2)
{
return LONG((*ps1)->m_ServiceStatus.dwCurrentState)-LONG((*ps2)->m_ServiceStatus.dwCurrentState);
}
int CServiceList::SortByStart(const CService** ps1, const CService** ps2)
{
return LONG((*ps1)->m_dwStartType)-LONG((*ps2)->m_dwStartType);
}
int CServiceList::SortByType(const CService** ps1, const CService** ps2)
{
return LONG((*ps1)->m_dwServiceType)-LONG((*ps2)->m_dwServiceType);
}
int CServiceList::SortByBinaryPathName(const CService** ps1, const CService** ps2)
{
return (*ps1)->m_strBinaryPathName.CompareNoCase((*ps2)->m_strBinaryPathName);
}
int CServiceList::SortByUser(const CService** ps1, const CService** ps2)
{
return (*ps1)->m_strServiceStartName.CompareNoCase((*ps2)->m_strServiceStartName);
}
int CServiceList::SortByLoadOrderGroup(const CService** ps1, const CService** ps2)
{
return (*ps1)->m_strLoadOrderGroup.CompareNoCase((*ps2)->m_strLoadOrderGroup);
}
int CServiceList::SortByPid(const CService** ps1, const CService** ps2)
{
return LONG((*ps1)->m_ServiceStatus.dwProcessId)-LONG((*ps2)->m_ServiceStatus.dwProcessId);
}
int CServiceList::SortByDescription(const CService** ps1, const CService** ps2)
{
return (*ps1)->m_strDescription.CompareNoCase((*ps2)->m_strDescription);
}
BOOL CServiceList::AddServiceFromStatus(SC_HANDLE hSCM, LPENUM_SERVICE_STATUS_PROCESS lpStatus)
{
CService* instance = new CService(this);
if( instance != NULL )
{
if( instance->CreateFrom( hSCM, lpStatus ) )
{
m_Entries.Add(instance);
return TRUE;
}
else delete instance;
}
return FALSE;
}
void CServiceList::ExportXmlToFile( CFile* pFile )
{
LPCTSTR lpszTag = GetTagString();
PrintToFile(pFile, _T("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\r\n")
_T("<%s>\r\n"), lpszTag );
for(int n = 0, nmax = m_Entries.GetSize(); n < nmax; n++ )
{
CService* p = (CService*)m_Entries.GetAt(n);
PrintToFile(pFile, _T("\t<service id=\"%s\">\r\n"), (LPCTSTR) XmlEscape(p->m_strServiceName) );
PrintToFile(pFile, _T("\t\t<name>%s</name>\r\n"),(LPCTSTR) XmlEscape(p->m_strDisplayName) );
PrintToFile(pFile, _T("\t\t<status>%s</status>\r\n"), (LPCTSTR) p->GetStatusAsString() );
PrintToFile(pFile, _T("\t\t<start>%s</start>\r\n"), (LPCTSTR) p->GetStartAsString() );
PrintToFile(pFile, _T("\t\t<type>%s</type>\r\n"), (LPCTSTR) p->GetTypeAsString() );
PrintToFile(pFile, _T("\t\t<path>%s</path>\r\n"), (LPCTSTR) p->m_strBinaryPathName );
PrintToFile(pFile, _T("\t\t<pid>%s</pid>\r\n"), (LPCTSTR) p->GetPidAsString() );
PrintToFile(pFile, _T("\t\t<description>%s</description>\r\n"), (LPCTSTR) XmlEscape(p->m_strDescription) );
PrintToFile(pFile, _T("\t</service>\r\n") );
}
PrintToFile(pFile, _T("</%s>"), lpszTag);
}
BOOL CServiceList::Refresh()
{
BOOL bSuccess = FALSE;
DeleteObjects( m_Entries );
SC_HANDLE hSCM = OpenSCManager( m_strServiceMachine, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ENUMERATE_SERVICE );
if( hSCM )
{
DWORD dwBytesNeeded, dwServicesReturned, dwResumeHandle = 0;
if( lpfnEnumServicesStatusEx )
{
VERIFY( !lpfnEnumServicesStatusEx( hSCM, SC_ENUM_PROCESS_INFO, m_dwEnumType, SERVICE_STATE_ALL,
NULL, 0, &dwBytesNeeded, &dwServicesReturned, &dwResumeHandle, NULL ) );
ASSERT( GetLastError() == ERROR_MORE_DATA );
LPBYTE bBuffer = new BYTE[dwBytesNeeded];
if( bBuffer )
{
if( lpfnEnumServicesStatusEx( hSCM, SC_ENUM_PROCESS_INFO, m_dwEnumType, SERVICE_STATE_ALL,
bBuffer, dwBytesNeeded, &dwBytesNeeded, &dwServicesReturned, &dwResumeHandle, NULL ) )
{
LPENUM_SERVICE_STATUS_PROCESS lpStatus = (LPENUM_SERVICE_STATUS_PROCESS)bBuffer;
for( DWORD dwIndex = 0; dwIndex < dwServicesReturned; dwIndex++ )
{
bSuccess = AddServiceFromStatus( hSCM, &(lpStatus[dwIndex]));
}
}
delete bBuffer;
}
}
else
{
VERIFY( !EnumServicesStatus( hSCM, m_dwEnumType, SERVICE_STATE_ALL, 0, 0, &dwBytesNeeded, &dwServicesReturned, &dwResumeHandle ) );
ASSERT( GetLastError() == ERROR_MORE_DATA );
LPBYTE bBuffer = new BYTE[dwBytesNeeded];
if( bBuffer )
{
if( EnumServicesStatus( hSCM, m_dwEnumType, SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)bBuffer, dwBytesNeeded, &dwBytesNeeded, &dwServicesReturned, &dwResumeHandle ) )
{
LPENUM_SERVICE_STATUS lpStatus = (LPENUM_SERVICE_STATUS)bBuffer;
for( DWORD dwIndex = 0; dwIndex < dwServicesReturned; dwIndex++ )
{
LPENUM_SERVICE_STATUS s = &(lpStatus[dwIndex]);
ENUM_SERVICE_STATUS_PROCESS esp;
CopyMemory( &esp, s, sizeof(ENUM_SERVICE_STATUS) );
esp.ServiceStatusProcess.dwProcessId = 0;
esp.ServiceStatusProcess.dwServiceFlags = 0;
bSuccess = AddServiceFromStatus( hSCM, &esp);
}
}
delete bBuffer;
}
}
Sort();
CloseServiceHandle(hSCM);
}
if( !bSuccess )
DisplayErrorMessage(_T("Unable to get initial list of %s"), GetTagString() );
return bSuccess;
}
UINT CServiceList::GetContextMenuID()
{
return IDR_CONTEXTMENU;
}