Click here to Skip to main content
12,398,537 members (58,978 online)
Click here to Skip to main content

Stats

73.4K views
1.8K downloads
170 bookmarked
Posted

ProSysLib: Dissecting the Process

, 22 Nov 2010 CPOL
Access detailed information about the current process the easiest way.
ProcessInfo.exe
ProSysLib.dll
Interop.ProSysLib.dll
Interop.ProSysLib.dll
ProcessInfo.exe
ProSysLib.dll
PSL64v0.9.dll
Interop.ProSysLib.dll
ProcessInfo.exe
PSL32v0.9.dll
ProSysLib_v0.9
Bin
PSL32v0.9.dll
PSL64v0.9.dll
Help
Samples
C#2008
ProcessInfo
Properties
ProcessViewer
Properties
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.exe
PSLv0.7Setup.exe
#include "StdAfx.h"
#include "ServiceInfo.h"

// Interval in milliseconds for updating values of dynamic properties
// of the interface, such as m_dwProcessID and m_CurrentState:
#define UPDATE_PROPERTY_DELAY	200

CServiceInfo::CServiceInfo(void)
{
	m_sName = _T("");
	m_sDisplayName = _T("");
	m_sDescription = _T("");
	m_sPath = _T("");
	m_sStartName = _T("");
	m_sDependencies = _T("");
	m_CurrentState = ssStopped;
	m_dwServiceType = 0; // Invalid;
	m_dwStartType = -1; // Unknown;
	m_dwProcessID = 0;
	m_dwTickCount = 0;
}

void CServiceInfo::Initialize(SC_HANDLE hService, ENUM_SERVICE_STATUS_PROCESS & ssp, LPBYTE pTempBuffer, DWORD dwBufferSize)
{
	m_sName = ssp.lpServiceName;
	m_sDisplayName = ssp.lpDisplayName;
	m_CurrentState = (PSLServiceState)ssp.ServiceStatusProcess.dwCurrentState;
	m_dwServiceType = ssp.ServiceStatusProcess.dwServiceType;
	m_dwProcessID = ssp.ServiceStatusProcess.dwProcessId;

	DWORD dwBytesNeeded = 0;

	LPQUERY_SERVICE_CONFIG pConfig = (LPQUERY_SERVICE_CONFIG)pTempBuffer;
	if(::QueryServiceConfig(hService, pConfig, dwBufferSize, &dwBytesNeeded))
	{
		tstring s = pConfig->lpBinaryPathName;
		CPSLUtilities::FixSysPath(s);
		m_sPath = s.c_str();

		if(pConfig->lpServiceStartName)
			m_sStartName = pConfig->lpServiceStartName;

		if(pConfig->lpDependencies)
			m_sDependencies = pConfig->lpDependencies;

		m_dwStartType = pConfig->dwStartType;
	}

	LPSERVICE_DESCRIPTION pDesc = (LPSERVICE_DESCRIPTION)pTempBuffer;
	if(::QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, (LPBYTE)pDesc, dwBufferSize, &dwBytesNeeded))
		m_sDescription = pDesc->lpDescription;
}

PSLException CServiceInfo::Update()
{
	if(m_sName.length() < 1)
		return exNone;

	DWORD dwCount = ::GetTickCount();
	if(abs((long)(dwCount - m_dwTickCount)) <  UPDATE_PROPERTY_DELAY)
		return exNone;

	m_dwTickCount = dwCount;

	m_dwProcessID = 0;
	m_CurrentState = ssStopped;

	SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, GENERIC_READ);
	if(!hSCM)
	{
		if(::GetLastError() == ERROR_ACCESS_DENIED)
			return exAccessDenied;
		else
			return exGeneric;
	}

	SC_HANDLE hService = ::OpenService(hSCM, m_sName, GENERIC_READ);
	if(!hService)
	{
		DWORD dwError = ::GetLastError();
		::CloseServiceHandle(hSCM);

		if(dwError == ERROR_ACCESS_DENIED)
			return exAccessDenied;
		else
			return exGeneric;
	}

	PSLException ex = exGeneric;

	DWORD dwBytesNeeded = 0;
	SERVICE_STATUS_PROCESS Status;
	if(::QueryServiceStatusEx(hService, SC_STATUS_PROCESS_INFO, (LPBYTE)&Status, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded))
	{
		m_dwProcessID = Status.dwProcessId;
		m_CurrentState = static_cast<PSLServiceState>(Status.dwCurrentState);
		ex = exNone;
	}

	::CloseServiceHandle(hService);
	::CloseServiceHandle(hSCM);

	return ex;
}

long CServiceInfo::StartService(SAFEARRAY * CmdParams, PSLException & ex)
{
	ex = exNone;
	long ResultCode = 0;
	SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, STANDARD_RIGHTS_EXECUTE);
	if(hSCM)
	{
		SC_HANDLE hService = ::OpenService(hSCM, m_sName, SERVICE_START);
		if(hService)
		{
			TCHAR ** Params = NULL;
			DWORD dwCounter = 0;

			if(CmdParams)
			{
				long nElements;	// Number of elements in the array;
				if(::SafeArrayGetUBound(CmdParams, 1, &nElements) == S_OK)
				{
					nElements ++;
					dwCounter = nElements + 1;

					Params = new TCHAR*[dwCounter];
					int L = m_sName.length();
					Params[0] = new TCHAR[L + 1];
					_tcsncpy_s(Params[0], L + 1, m_sName, L);

					BSTR sName = NULL;
					for(long i = 0;i < nElements;i ++)
						if(::SafeArrayGetElement(CmdParams, &i, &sName) == S_OK)
						{
							_bstr_t tmp(sName);
							L = tmp.length();
							Params[i + 1] = new TCHAR[L + 1];
							_tcsncpy_s(Params[i + 1], L + 1, tmp, L);
							::SysFreeString(sName);
						}
				}
			}

			if(!::StartService(hService, dwCounter, (LPCTSTR*)Params))
			{
				DWORD dwError = ::GetLastError();
				if(dwError == ERROR_ACCESS_DENIED)
					ex = exAccessDenied;
				
				ResultCode = dwError;
			}

			for(DWORD i = 0;i < dwCounter;i ++)
				delete Params[i];
			delete []Params;

			::CloseServiceHandle(hService);
		}
		else
		{
			DWORD dwError = ::GetLastError();
			if(dwError == ERROR_ACCESS_DENIED)
				ex = exAccessDenied;

			ResultCode = dwError;
		}

		::CloseServiceHandle(hSCM);
	}
	else
	{
		DWORD dwError = ::GetLastError();
		if(dwError == ERROR_ACCESS_DENIED)
			ex = exAccessDenied;

		ResultCode = dwError;
	}

	return ResultCode;
}

long CServiceInfo::StopService(PSLException & ex)
{
	ex = exNone;
	long ResultCode = 0;
	SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, STANDARD_RIGHTS_EXECUTE);
	if(hSCM)
	{
		SC_HANDLE hService = ::OpenService(hSCM, m_sName, SERVICE_STOP);
		if(hService)
		{
			SERVICE_STATUS s;
			if(!::ControlService(hService, SERVICE_CONTROL_STOP, &s))
			{
				DWORD dwError = ::GetLastError();
				if(dwError == ERROR_ACCESS_DENIED)
					ex = exAccessDenied;

				ResultCode = dwError;
			}

			::CloseServiceHandle(hService);
		}
		else
		{
			DWORD dwError = ::GetLastError();
			if(dwError == ERROR_ACCESS_DENIED)
				ex = exAccessDenied;

			ResultCode = dwError;
		}

		::CloseServiceHandle(hSCM);
	}
	else
	{
		DWORD dwError = ::GetLastError();
		if(dwError == ERROR_ACCESS_DENIED)
			ex = exAccessDenied;

		ResultCode = dwError;
	}

	return ResultCode;
}

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

You may also be interested in...

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