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
// PSLCurrentProcess.cpp : Implementation of CPSLCurrentProcess

#include "stdafx.h"
#include "PSLCurrentProcess.h"
#include "ProSysModule.h"
#include <ATLComTime.h>
#include <Psapi.h>
#include "SystemInfoAccessor.h"

CPSLCurrentProcess::CPSLCurrentProcess()
{
	m_dwProcessID = ::GetCurrentProcessId();

	FILETIME create, exit, kernel, user;
	if(::GetProcessTimes(::GetCurrentProcess(), &create, &exit, &kernel, &user))
		m_Created = COleDateTime(create);
	else
		m_Created = NULL;

	m_sFilePath = _T("");
	m_sFileDir = _T("");
	m_sFileName = _T("");

	tstring path;
	path.resize(MAX_FILE_PATH);
	if(::GetModuleFileName(NULL, (LPTSTR)path.c_str(), MAX_FILE_PATH) > 0)
		CPSLUtilities::GetLongFilePathDetails(path.c_str(), m_sFilePath, m_sFileDir, m_sFileName);
}

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

void CPSLCurrentProcess::FinalRelease()
{
}

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

STDMETHODIMP CPSLCurrentProcess::get_ProcessID(long * pValue)
{
	PSL_BEGIN

	*pValue = m_dwProcessID;

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_ConsoleWnd(VARIANT * pValue)
{
	PSL_BEGIN

	CPSLUtilities::SetVariantBig(pValue, (ABIG)::GetConsoleWindow());

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_FileName(BSTR * pValue)
{
	PSL_BEGIN

	*pValue = m_sFileName.copy();

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_FilePath(BSTR * pValue)
{
	PSL_BEGIN

	*pValue = m_sFilePath.copy();
	
	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_FileDir(BSTR * pValue)
{
	PSL_BEGIN

	*pValue = m_sFileDir.copy();
	
	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_CurrentDir(BSTR * pValue)
{
	PSL_BEGIN

	tstring dir;
	dir.resize(MAX_FILE_PATH);
	if(::GetCurrentDirectory(MAX_FILE_PATH, (LPTSTR)dir.c_str()))
		::GetLongPathName(dir.c_str(), (LPTSTR)dir.c_str(), MAX_FILE_PATH);
	*pValue = ::SysAllocString(dir.c_str());

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_CurrentDir(BSTR newValue)
{
	PSL_BEGIN

	::SetCurrentDirectory(_bstr_t(newValue));
	
	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Created(DATE * pValue)
{
	PSL_BEGIN

	*pValue = m_Created;
	
	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Is64Bit(VARIANT_BOOL * pValue)
{
	PSL_BEGIN

#ifdef _WIN64
	*pValue = VARIANT_TRUE;
#else
	*pValue = VARIANT_FALSE;
#endif

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_IsDebugged(VARIANT_BOOL * pValue)
{
	PSL_BEGIN

	*pValue = FALSE;

	BOOL bDebugged = FALSE;
	if(CSystemInfoAccessor::CheckRemoteDebuggerPresent(::GetCurrentProcess(), &bDebugged))
		*pValue = bDebugged?VARIANT_TRUE:VARIANT_FALSE;
	else
	{
		DWORD dwDebugPort = 0;
		if(!CSystemInfoAccessor::NtQueryInformationProcess(::GetCurrentProcess(), ProcessDebugPort, &dwDebugPort, sizeof(DWORD), NULL))
			*pValue = (dwDebugPort > 0)?VARIANT_TRUE:VARIANT_FALSE;
	}

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_HandleCount(long * pValue)
{
	PSL_BEGIN

	*pValue = 0;

	DWORD dwHandles = 0;
	if(CSystemInfoAccessor::GetProcessHandleCount(::GetCurrentProcess(), &dwHandles))
		*pValue = dwHandles;
	else
	{
		DWORD dwCounter = 0;
		if(!CSystemInfoAccessor::NtQueryInformationProcess(::GetCurrentProcess(), ProcessHandleCount, &dwCounter, sizeof(DWORD), NULL))
			*pValue = dwCounter;
	}

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_GDICount(long * pValue)
{
	PSL_BEGIN

	*pValue = ::GetGuiResources(::GetCurrentProcess(), GR_GDIOBJECTS);

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_USERCount(long * pValue)
{
	PSL_BEGIN

	*pValue = ::GetGuiResources(::GetCurrentProcess(), GR_USEROBJECTS);

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Priority(PSLProcessPriority * pValue)
{
	PSL_BEGIN

	*pValue = (PSLProcessPriority)::GetPriorityClass(::GetCurrentProcess());
	
	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_Priority(PSLProcessPriority newValue)
{
	PSL_BEGIN

	if(!::SetPriorityClass(::GetCurrentProcess(), newValue))
	{
		if(::GetLastError() == ERROR_ACCESS_DENIED)
			SetException(exAccessDenied);
	}

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_ShutdownLevel(long * pValue)
{
	PSL_BEGIN

	DWORD dwLevel, dwFlags;
	if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
		*pValue = dwLevel;
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_ShutdownLevel(long newValue)
{
	PSL_BEGIN

	// Only values between 0 and 0x4FF
	// are known to be valid at present;
	if(newValue < 0 || newValue > 0x4FF)
		return MakeException(exInvalidParameter);

	DWORD dwLevel, dwFlags;
	if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
	{
		dwLevel = newValue;
		::SetProcessShutdownParameters(dwLevel, dwFlags);
	}

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_ShutdownFlags(long * pValue)
{
	PSL_BEGIN

	DWORD dwLevel, dwFlags;
	if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
		*pValue = dwFlags;
	else
		*pValue = 0;

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_ShutdownFlags(long newValue)
{
	PSL_BEGIN

	// Only 0 and SHUTDOWN_NORETRY are known 
	// to be supported at present;
	if(newValue != 0 && newValue != SHUTDOWN_NORETRY)
		return MakeException(exInvalidParameter);

	DWORD dwLevel, dwFlags;
	if(::GetProcessShutdownParameters(&dwLevel, &dwFlags))
	{
		dwFlags = newValue;
		::SetProcessShutdownParameters(dwLevel, dwFlags);
	}

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_AffinityMask(VARIANT * pValue)
{
	PSL_BEGIN

	CPSLUtilities::SetVariantBig(pValue, 0);

	DWORD_PTR ProcessAffinityMask = 0;
	DWORD_PTR SystemAffinityMask = 0;
	if(::GetProcessAffinityMask(::GetCurrentProcess(), &ProcessAffinityMask, &SystemAffinityMask))
		CPSLUtilities::SetVariantBig(pValue, ProcessAffinityMask);

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::put_AffinityMask(VARIANT newValue)
{
	PSL_BEGIN

	ABIG Mask = CPSLUtilities::GetVariantBig(&newValue);
	if(Mask)
	{
		DWORD_PTR ProcessAffinityMask = Mask;
		::SetProcessAffinityMask(::GetCurrentProcess(), ProcessAffinityMask);
	}
	else
		SetException(exInvalidParameter);

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Version(IPSLModuleVersion ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Version;

	CPSLModuleVersion * pVer = m_Version;
	pVer->InitInternal(m_sFilePath);
	pVer->Release();

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Memory(IPSLProcessMemory ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Memory;

	CPSLProcessMemory * pMemory = m_Memory;
	pMemory->Initialize(::GetCurrentProcessId());
	pMemory->Release();

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_IO(IPSLProcessIO ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_ProcessIO;

	CPSLProcessIO * pProcessIO = m_ProcessIO;
	pProcessIO->Initialize(::GetCurrentProcessId());
	pProcessIO->Release();

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_EnvVars(IPSLEnvironmentVars ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_EnvVars;

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Modules(IPSLModules ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Modules;

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Commands(IPSLCmdParams ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Commands;

	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Threads(IPSLThreads ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Threads;
	
	PSL_END
}

STDMETHODIMP CPSLCurrentProcess::get_Windows(IPSLWindows ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Windows;
	
	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 | Terms of Use | Mobile
Web02 | 2.8.150326.1 | Last Updated 22 Nov 2010
Article Copyright 2008 by Vitaly Tomilov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid