Click here to Skip to main content
12,454,104 members (54,612 online)
Click here to Skip to main content

Stats

73.9K views
1.8K downloads
170 bookmarked
Posted

ProSysLib: Dissecting the Process

, 22 Nov 2010 CPOL
Access detailed information about the current process the easiest way.
ProcessInfo.exe
ProSysLib.dll
Interop.ProSysLib.dll
Interop.ProSysLib.dll
ProcessInfo.exe
ProSysLib.dll
PSL64v0.9.dll
Interop.ProSysLib.dll
ProcessInfo.exe
PSL32v0.9.dll
ProSysLib_v0.9
Bin
PSL32v0.9.dll
PSL64v0.9.dll
Help
Samples
C#2008
ProcessInfo
Properties
ProcessViewer
Properties
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.exe
PSLv0.7Setup.exe
// PSLWindows.cpp : Implementation of CPSLWindows

#include "stdafx.h"
#include "PSLWindow.h"
#include "PSLWindows.h"

CPSLWindows::CPSLWindows()
{
}

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

HRESULT CPSLWindows::OnInterfaceAccess()
{
	InternalUpdate(false);
	return GetExitCode();
}

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

void CPSLWindows::FinalRelease()
{
}

bool CPSLWindows::IsValidWindow(HWND hWnd, CPSLWindowsFilter::CWindowsFilter * pFilter)
{
	if(!::IsWindow(hWnd))
		return false;

	if(pFilter->Enabled != ffAny)
	{
		BOOL bEnabled = ::IsWindowEnabled(hWnd);
		if(pFilter->Enabled == ffON && !bEnabled)
			return false;
		if(pFilter->Enabled == ffOFF && bEnabled)
			return false;
	}

	if(pFilter->HasText != ffAny)
	{
		int iLength = ::GetWindowTextLength(hWnd);
		if(pFilter->HasText == ffON && iLength < 1)
			return false;
		if(pFilter->HasText == ffOFF && iLength > 0)
			return false;
	}

	if(pFilter->Visible != ffAny)
	{
		BOOL bVisible = ::IsWindowVisible(hWnd);
		if(pFilter->Visible == ffON && !bVisible)
			return false;
		if(pFilter->Visible == ffOFF && bVisible)
			return false;
	}

	return true;
}

BOOL CALLBACK CPSLWindows::EnumChildWndProc(HWND hWnd, LPARAM lParam)
{
	CWndEnumHelper * pHelper = (CWndEnumHelper*)lParam;
	if(pHelper->pFilter->ThreadID)
	{
		if(::GetWindowThreadProcessId(hWnd, NULL) != pHelper->pFilter->ThreadID)
			return TRUE;
	}
	if(IsValidWindow(hWnd, pHelper->pFilter))
	{
		CComObject<CPSLWindow> * pWindow = NULL;
		if(CComObject<CPSLWindow>::CreateInstance(&pWindow) == S_OK)
		{
			pWindow->Initialize(hWnd);
			pHelper->pList->push_back(CComPtr<IPSLWindow>(pWindow));
		}
	}
	return TRUE; // Continue;
}

BOOL CALLBACK CPSLWindows::EnumWndProc(HWND hWnd, LPARAM lParam)
{
	CWndEnumHelper * pHelper = (CWndEnumHelper*)lParam;
	DWORD dwProcessID = 0;
	DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwProcessID);
	if(pHelper->dwProcessID == dwProcessID)
	{
		if(pHelper->pFilter->TopLevel != ffOFF && IsValidWindow(hWnd, pHelper->pFilter) && (!pHelper->pFilter->ThreadID || pHelper->pFilter->ThreadID == dwThreadID))
		{
			CComObject<CPSLWindow> * pWindow = NULL;
			if(CComObject<CPSLWindow>::CreateInstance(&pWindow) == S_OK)
			{
				pWindow->Initialize(hWnd);
				pHelper->pList->push_back(CComPtr<IPSLWindow>(pWindow));
			}
		}
		if(pHelper->pFilter->TopLevel != ffON)
			::EnumChildWindows(hWnd, EnumChildWndProc, lParam);
	}
	return TRUE; // Continue;
}

void CPSLWindows::InternalUpdate(bool bForceUpdate)
{
	CCritSecLock cs(m_csCollection);

	CPSLWindowsFilter::CWindowsFilter Filter;

	CPSLWindowsFilter * pFilterClass = m_Filter;
	bool bDirty = pFilterClass->CheckDirtyAndReset(&Filter);
	pFilterClass->Release();

	if(!bForceUpdate && !bDirty)
		return;

	m_coll.clear();

	CWndEnumHelper helper;
	helper.pList = &m_coll;
	helper.dwProcessID = ::GetCurrentProcessId();
	helper.pFilter = &Filter;

	if(Filter.ParentWnd)
	{
		if(IsValidWindow(Filter.ParentWnd, &Filter))
		{
			bool bHasParent = (::GetParent(Filter.ParentWnd) != NULL);
			if((Filter.TopLevel == ffAny) || (Filter.TopLevel == ffON && !bHasParent) || (Filter.TopLevel == ffOFF && bHasParent))
			{
				CComObject<CPSLWindow> * pWindow = NULL;
				if(CComObject<CPSLWindow>::CreateInstance(&pWindow) == S_OK)
				{
					pWindow->Initialize(Filter.ParentWnd);
					m_coll.push_back(CComPtr<IPSLWindow>(pWindow));
				}
			}
		}
		if(Filter.TopLevel != ffON)
			::EnumChildWindows(Filter.ParentWnd, EnumChildWndProc, (LPARAM)&helper);
	}
	else
	{
		if(Filter.ThreadID)
			::EnumThreadWindows(Filter.ThreadID, EnumWndProc, (LPARAM)&helper);
		else
			::EnumWindows(EnumWndProc, (LPARAM)&helper);
	}
}

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

STDMETHODIMP CPSLWindows::Update()
{
	PSL_BEGIN

	InternalUpdate(true);

	PSL_END
}

STDMETHODIMP CPSLWindows::Find(VARIANT Handle, IPSLWindow ** ppValue)
{
	PSL_BEGIN

	*ppValue = NULL;
	bool bError = false;
	HWND hWnd = (HWND)CPSLUtilities::GetVariantBig(&Handle, &bError);
	if(bError || !::IsWindow(hWnd))
		return MakeException(exInvalidParameter);
	CCritSecLock cs(m_csCollection);
	InternalUpdate(false);
	VARIANT v;
	::VariantInit(&v);
	for(WindowsType::iterator i = m_coll.begin();i != m_coll.end();i ++)
	{
		if(i->m_T->get_Handle(&v) == S_OK)
		{
			HWND hListWnd = (HWND)CPSLUtilities::GetVariantBig(&v);
			::VariantClear(&v);
			if(hWnd == hListWnd)
			{
				IPSLWindow * pWindow = i->m_T;
				pWindow->AddRef();
				*ppValue = pWindow;
				break;
			}
		}	
	}

	PSL_END
}

STDMETHODIMP CPSLWindows::get_Filter(IPSLWindowsFilter ** ppValue)
{
	PSL_BEGIN

	*ppValue = m_Filter;

	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

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 22 Nov 2010
Article Copyright 2008 by Vitaly Tomilov
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid