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

Professional System Library: Introduction

, 22 Nov 2010 CPOL
A simplified and unified way for accessing most frequently used information about Process, System, and Environment.
ProcessViewer32_bin.zip
SimpleProcessViewer.exe
Interop.ProSysLib.dll
ProSysLib.dll
ProcessViewer64_bin.zip
SimpleProcessViewer.exe
Interop.ProSysLib.dll
ProSysLib.dll
ProcessViewer_bin.zip
PSL64v0.9.dll
Interop.ProSysLib.dll
ProcessViewer.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.3Setup.zip
PSLv0.3Setup.exe
PSLv0.4Setup.zip
PSLv0.4Setup.exe
PSLv0.7Setup.zip
PSLv0.7Setup.exe
SimpleProcessViewer_src.zip
SimpleProcessViewer
Properties
Settings.settings
// 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)

Share

About the Author

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 22 Nov 2010
Article Copyright 2008 by Vitaly Tomilov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid