Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

ProSysLib: Dissecting the Process

, 22 Nov 2010 CPOL
Access detailed information about the current process the easiest way.
ProcessInfo32_bin.zip
ProcessInfo.exe
ProSysLib.dll
Interop.ProSysLib.dll
ProcessInfo64_bin.zip
Interop.ProSysLib.dll
ProcessInfo.exe
ProSysLib.dll
ProcessInfo_bin.zip
PSL64v0.9.dll
Interop.ProSysLib.dll
ProcessInfo.exe
PSL32v0.9.dll
ProSysLib_v0.9.zip
ProSysLib_v0.9
Bin
PSL32v0.9.dll
PSL64v0.9.dll
Help
Samples
C#2008
ProcessInfo
Properties
Settings.settings
ProcessViewer
Properties
Settings.settings
VB6
CPUInfo.exe
CPUInfo.frm
CPUInfo.frx
CPUInfo.vbp
CPUInfo.vbw
VC2008
Deployment
Src
mssccprj.scc
ProSysLib.def
ProSysLib.rgs
ProSysLib.vcproj.vspscc
ProSysLib.vssscc
PSLSystem.rgs
PSLv0.5Setup.zip
PSLv0.5Setup.exe
PSLv0.7Setup.zip
PSLv0.7Setup.exe
#pragma once

#include "ProSysLib_i.h"
#include "PSLExceptions.h"

template<class Container, class Child, class ChildInterface>
class CServiceEnumerator
{
protected:

	void EnumServices(Container * pContainer, long ServiceType)
	{
		SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, GENERIC_READ);
		if(!hSCM)
		{
			if(::GetLastError() == ERROR_ACCESS_DENIED)
				pContainer->SetException(exAccessDenied);
			return;
		}

		DWORD dwBytesNeeded = 0;
		DWORD dwServiceCount = 0;
		DWORD dwResumeHandle = 0;
		BOOL RetVal = ::EnumServicesStatusEx(hSCM, SC_ENUM_PROCESS_INFO, ServiceType, SERVICE_STATE_ALL, NULL, 0, &dwBytesNeeded, &dwServiceCount, &dwResumeHandle, NULL);

		if(RetVal || GetLastError() != ERROR_MORE_DATA)
		{
			::CloseServiceHandle(hSCM);
			return; // This should never happen;
		}

		LPBYTE pStatusBuffer = new BYTE[dwBytesNeeded];
		if(!pStatusBuffer)
		{
			pContainer->SetException(exLowMemory);
			::CloseServiceHandle(hSCM);
			return;
		}

		if(!::EnumServicesStatusEx(hSCM, SC_ENUM_PROCESS_INFO, ServiceType, SERVICE_STATE_ALL, pStatusBuffer, dwBytesNeeded, &dwBytesNeeded, &dwServiceCount, &dwResumeHandle, NULL))
		{
			if(::GetLastError() == ERROR_ACCESS_DENIED)
				pContainer->SetException(exAccessDenied);
			delete []pStatusBuffer;
			::CloseServiceHandle(hSCM);
			return;
		}

		// Allocating 8Kb of memory to get full service description,
		// which is also the supported maximum for a number of Service API-s,
		// like QueryServiceConfig, so it cannot be changed.
		DWORD dwBufferSize = 8192;
		LPBYTE pTempBuffer = new BYTE[dwBufferSize];

		if(!pTempBuffer)
		{
			pContainer->SetException(exLowMemory);
			delete []pStatusBuffer;
			::CloseServiceHandle(hSCM);
			return;
		}

		LPENUM_SERVICE_STATUS_PROCESS pSSP = (LPENUM_SERVICE_STATUS_PROCESS)pStatusBuffer;

		CCritSecLock cs(pContainer->m_csCollection);
		pContainer->m_coll.clear();
		CComObject<Child> * pService = NULL;
		for(DWORD i = 0;i < dwServiceCount;i ++)
		{
			SC_HANDLE hService = ::OpenService(hSCM, pSSP[i].lpServiceName, GENERIC_READ);
			if(!hService)
				continue;

			CComObject<Child>::CreateInstance(&pService);
			pService->Initialize(hService, pSSP[i], pTempBuffer, dwBufferSize);
			pContainer->m_coll.push_back(CComPtr<ChildInterface>(pService));

			::CloseServiceHandle(hService);
		}
		cs.Unlock();

		delete []pTempBuffer;
		delete []pStatusBuffer;
		::CloseServiceHandle(hSCM);
	}

	ChildInterface * FindService(Container * pContainer, BSTR ServiceName)
	{
		_bstr_t sServiceName(ServiceName);
		CCritSecLock cs(pContainer->m_csCollection);	

		for(vector<CAdapt<CComPtr<ChildInterface> > >::iterator i = pContainer->m_coll.begin();i != pContainer->m_coll.end();i ++)
		{
			BSTR sName = NULL;
			if(i->m_T->get_Name(&sName) == S_OK)
			{
				_bstr_t s(sName);
				if(!::_tcsicmp(s, sServiceName)) // Doing case-insensitive comparison;
				{
					ChildInterface * pService = i->m_T;
					pService->AddRef();
					return pService;
				}
			}	
		}

		return NULL;
	}

};

class CServiceInfo
{
public:

	CServiceInfo(void);

protected:

	void Initialize(SC_HANDLE hSCM, ENUM_SERVICE_STATUS_PROCESS & ssp, LPBYTE pTempBuffer, DWORD dwBufferSize);

	long StartService(SAFEARRAY * CmdParams, PSLException & ex);
	long StopService(PSLException & ex);

	PSLException Update();

	_bstr_t m_sName;
	_bstr_t m_sDisplayName;
	_bstr_t m_sDescription;
	_bstr_t m_sPath;
	_bstr_t m_sStartName;
	_bstr_t m_sDependencies;

	PSLServiceState m_CurrentState;
	DWORD m_dwServiceType;
	DWORD m_dwStartType;
	DWORD m_dwProcessID;

private:

	DWORD m_dwTickCount;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vitaly Tomilov
Software Developer (Senior) Sibedge IT
Ireland Ireland
My online CV: cv.vitalytomilov.com

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 22 Nov 2010
Article Copyright 2008 by Vitaly Tomilov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid