Click here to Skip to main content
15,884,298 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 128K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLSoftware.cpp : Implementation of CPSLSoftware

#include "stdafx.h"
#include "PSLSoftware.h"
#include "PSLWindow.h"
#include "PSLProcess.h"
#include <Tlhelp32.h>

CPSLSoftware::CPSLSoftware()
{
}

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

void CPSLSoftware::FinalRelease()
{
}

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

STDMETHODIMP CPSLSoftware::get_OS(IPSLOS ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_OS;
	
	PSL_END
}

STDMETHODIMP CPSLSoftware::get_Drivers(IPSLDrivers ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Drivers;
	
	PSL_END
}

STDMETHODIMP CPSLSoftware::get_Processes(IPSLProcesses ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Processes;

	PSL_END
}

STDMETHODIMP CPSLSoftware::get_Services(IPSLServices ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Services;

	PSL_END
}

STDMETHODIMP CPSLSoftware::GetModuleVersion(BSTR ModulePath, IPSLModuleVersion ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;

	CComObject<CPSLModuleVersion> * pVersion = NULL;
	if(CComObject<CPSLModuleVersion>::CreateInstance(&pVersion) == S_OK)
	{
		pVersion->AddRef();
		if(pVersion->InitExternal(_bstr_t(ModulePath)))
			*ppValue = CComPtr<IPSLModuleVersion>(pVersion);
		else
			pVersion->Release();
	}

	PSL_END
}

STDMETHODIMP CPSLSoftware::WindowFromHandle(VARIANT Handle, IPSLWindow ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;

	bool bError = false;
	HWND hWnd = (HWND)CPSLUtilities::GetVariantBig(&Handle, &bError);
	if(bError || !::IsWindow(hWnd))
		SetException(exInvalidParameter);
	else
	{
		CComObject<CPSLWindow> * pWindow = NULL;
		if(CComObject<CPSLWindow>::CreateInstance(&pWindow) == S_OK)
		{
			pWindow->AddRef();
			pWindow->Initialize(hWnd);
			*ppValue = CComPtr<IPSLWindow>(pWindow);
		}
	}

	PSL_END
}

STDMETHODIMP CPSLSoftware::ProcessIDFromThreadID(long ThreadID, long * pValue)
{
	PSL_BEGIN

	*pValue = 0;
	HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
	if(hSnapshot != INVALID_HANDLE_VALUE)
	{
		THREADENTRY32 te;
		te.dwSize = sizeof(te);
		if(::Thread32First(hSnapshot, &te))
		{
			do
			{
				if(te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID) && te.th32ThreadID == ThreadID)
				{
					*pValue = te.th32OwnerProcessID;
					break;
				}
				te.dwSize = sizeof(te);
			}
			while(::Thread32Next(hSnapshot, &te));
		}
		::CloseHandle(hSnapshot);
	}

	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