Click here to Skip to main content
15,891,409 members
Articles / Programming Languages / Visual Basic 6

Professional System Library: Introduction

Rate me:
Please Sign up or sign in to vote.
4.84/5 (93 votes)
22 Nov 2010CPOL14 min read 193.6K   3.4K   232  
A simplified and unified way for accessing most frequently used information about Process, System, and Environment.
// PSLSecurity.cpp : Implementation of CPSLSecurity

#include "stdafx.h"
#include "PSLSecurity.h"
#include <Aclapi.h>

CPSLSecurity::CPSLSecurity()
{
}

HRESULT CPSLSecurity::FinalConstruct()
{
	return S_OK;
}

void CPSLSecurity::FinalRelease()
{
}

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

STDMETHODIMP CPSLSecurity::get_User(IPSLUser ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_User;
	
	PSL_END
}

STDMETHODIMP CPSLSecurity::get_Accounts(IPSLAccounts ** ppValue)
{
	PSL_BEGIN

	SetException(exNotImplemented);
	//*ppValue = m_Accounts;
	
	PSL_END
}

STDMETHODIMP CPSLSecurity::get_Privileges(IPSLPrivileges ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Privileges;
	
	PSL_END
}

STDMETHODIMP CPSLSecurity::GetNamedObjectAccess(PSLNamedType nt, BSTR sObjectName, long * pErrorCode, long * pValue)
{
	PSL_BEGIN

	*pValue = 0; // Mask Value;

	PACL pDacl;
	_bstr_t sObject = sObjectName;
	DWORD dwError = ::GetNamedSecurityInfo((LPTSTR)sObject, (SE_OBJECT_TYPE)nt, DACL_SECURITY_INFORMATION, NULL, NULL, &pDacl, NULL, NULL);
	if(dwError == ERROR_SUCCESS)
	{
		HANDLE hToken = NULL;
		if(::OpenProcessToken(::GetCurrentProcess(), TOKEN_READ, &hToken))
		{
			DWORD dwLength = 0;
			::GetTokenInformation(hToken, TokenUser, NULL, 0, &dwLength);
			LPBYTE pBuffer = new BYTE[dwLength];
			if(::GetTokenInformation(hToken, TokenUser, pBuffer, dwLength, &dwLength))
			{
				TOKEN_USER * pUser = (TOKEN_USER*)(LPVOID)pBuffer;
				TRUSTEE trustee;
				::BuildTrusteeWithSid(&trustee, pUser->User.Sid);
				ACCESS_MASK mask;
				dwError = ::GetEffectiveRightsFromAcl(pDacl, &trustee, &mask);
				if(dwError == ERROR_SUCCESS)
					*pValue = mask;
			}
			else
				dwError = ::GetLastError();

			delete []pBuffer;
			::CloseHandle(hToken);
		}
		else
			dwError = ::GetLastError();
	}

	if(pErrorCode)
		*pErrorCode = dwError;

	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