Click here to Skip to main content
15,892,537 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.9K   3.4K   232  
A simplified and unified way for accessing most frequently used information about Process, System, and Environment.
// 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)


Written By
Software Developer (Senior) Sibedge IT
Ireland Ireland
My online CV: cv.vitalytomilov.com

Comments and Discussions