Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / VBScript

ProSysLib: Dissecting the Process

Rate me:
Please Sign up or sign in to vote.
4.84/5 (69 votes)
22 Nov 2010CPOL12 min read 128.2K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLPrivileges.cpp : Implementation of CPSLPrivileges

#include "stdafx.h"
#include "PSLPrivileges.h"


CPSLPrivileges::CPSLPrivileges()
{
}

HRESULT CPSLPrivileges::OnIndexOutOfRange()
{
	return MakeException(exIndexOutOfRange);
}

HRESULT CPSLPrivileges::FinalConstruct()
{
	PSL_BEGIN

	InternalUpdate();
	
	PSL_END
}

void CPSLPrivileges::FinalRelease()
{
}

void CPSLPrivileges::InternalUpdate()
{
	CCritSecLock cs(m_csCollection);

	m_coll.clear(); // Releases interfaces and clears the collection;
					// NOTE: .NET clients catch up interfaces, so takes
					// about 30 seconds before they are garbaged by .NET;

	HANDLE hToken = NULL;
	if(::OpenProcessToken(::GetCurrentProcess(), TOKEN_READ, &hToken))
	{
		DWORD dwLength = 0;
		::GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwLength);
		LPBYTE pBuffer = new BYTE[dwLength];
		if(::GetTokenInformation(hToken, TokenPrivileges, pBuffer, dwLength, &dwLength))
		{
			TOKEN_PRIVILEGES * pPrivileges = (TOKEN_PRIVILEGES*)(LPVOID)pBuffer;
			LUID_AND_ATTRIBUTES * Privileges = pPrivileges->Privileges;
			DWORD dwCount = pPrivileges->PrivilegeCount;
			TCHAR PrivilegeName[64];
			for(DWORD i = 0;i < dwCount;i ++)
			{
				DWORD dwLength = 64;
				if(::LookupPrivilegeName(NULL, &Privileges[i].Luid, PrivilegeName, &dwLength))
				{
					CComObject<CPSLPrivilege> * pPrivilege = NULL;
					if(CComObject<CPSLPrivilege>::CreateInstance(&pPrivilege) == S_OK)
					{
						pPrivilege->Initialize(Privileges[i], PrivilegeName);
						// NOTE: Here the interface is also gets AddRef;
						m_coll.push_back(CComPtr<IPSLPrivilege>(pPrivilege));
					}
				}		
			}			
		}
		delete []pBuffer;
		::CloseHandle(hToken);
	}
}

////////////////////////////////////////////////////////////////////////
// Interface Implementation;
////////////////////////////////////////////////////////////////////////

STDMETHODIMP CPSLPrivileges::Update()
{
	PSL_BEGIN

	InternalUpdate();

	PSL_END
}

STDMETHODIMP CPSLPrivileges::Find(BSTR PrivilegeName, IPSLPrivilege ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;
	_bstr_t sPrivilegeName(PrivilegeName);
	CCritSecLock cs(m_csCollection);

	for(PrivilegesType::iterator i = m_coll.begin();i != m_coll.end();i ++)
	{
		BSTR sName = NULL;
		if(i->m_T->get_Name(&sName) == S_OK)
		{
			_bstr_t s(sName);
			if(!::_tcsicmp(s, sPrivilegeName)) // Doing case-insensitive comparison;
			{
				IPSLPrivilege * pPrivilege = i->m_T;
				pPrivilege->AddRef();
				*ppValue = pPrivilege;
				break;
			}
		}	
	}

	PSL_END
}

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)


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

Comments and Discussions