Click here to Skip to main content
15,892,809 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 129K   2.6K   174  
Access detailed information about the current process the easiest way.
// PSLThreads.cpp : Implementation of CPSLThreads

#include "stdafx.h"
#include "PSLThread.h"
#include "PSLThreads.h"

CPSLThreads::CPSLThreads()
{
}

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

HRESULT CPSLThreads::FinalConstruct()
{
	InternalUpdate();
	return S_OK;
}

void CPSLThreads::FinalRelease()
{
}

void CPSLThreads::InternalUpdate()
{
	CCritSecLock cs(m_csCollection);
	m_coll.clear();
	HANDLE hSnapshot = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
	if(hSnapshot != INVALID_HANDLE_VALUE)
	{
		THREADENTRY32 te;
		te.dwSize = sizeof(te);
		if(::Thread32First(hSnapshot, &te))
		{
			DWORD dwProcessID = ::GetCurrentProcessId();
			do
			{
				if(te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID) && te.th32OwnerProcessID == dwProcessID)
				{
					CComObject<CPSLThread> * pThread = NULL;
					if(CComObject<CPSLThread>::CreateInstance(&pThread) == S_OK)
					{
						pThread->Initialize(&te);
						m_coll.push_back(CComPtr<IPSLThread>(pThread));
					}
				}
				te.dwSize = sizeof(te);
			}
			while(::Thread32Next(hSnapshot, &te));
		}
		::CloseHandle(hSnapshot);
	}
}

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

STDMETHODIMP CPSLThreads::Update()
{
	PSL_BEGIN

	InternalUpdate();

	PSL_END
}

STDMETHODIMP CPSLThreads::Find(long ThreadID, IPSLThread ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;
	CCritSecLock cs(m_csCollection);
	for(ThreadsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
	{
		long ID = 0;
		if(i->m_T->get_ThreadID(&ID) == S_OK && ID == ThreadID)
		{
			IPSLThread * pThread = i->m_T;
			pThread->AddRef();
			*ppValue = pThread;
			break;
		}	
	}

	PSL_END
}

STDMETHODIMP CPSLThreads::FindCurrent(IPSLThread ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;
	CCritSecLock cs(m_csCollection);
	for(ThreadsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
	{
		VARIANT_BOOL IsCurrent = VARIANT_FALSE;
		if(i->m_T->get_IsCurrent(&IsCurrent) == S_OK && IsCurrent)
		{
			IPSLThread * pThread = i->m_T;
			pThread->AddRef();
			*ppValue = pThread;
			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