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
Access detailed information about the current process the easiest way.
ProcessInfo32_bin.zip
ProcessInfo64_bin.zip
ProcessInfo_bin.zip
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.7Setup.zip
#pragma once

template <typename T>
struct _CopyVariantFromAdaptItf
{
	static HRESULT copy(VARIANT * p1, const CAdapt< CComPtr<T> > * p2)
	{
		HRESULT hr = p2->m_T->QueryInterface(IID_IDispatch, (void**)&p1->pdispVal);
		if(SUCCEEDED(hr))
			p1->vt = VT_DISPATCH;
		return hr;
	}

	static void init(VARIANT * p)
	{
		::VariantInit(p);
	}

	static void destroy(VARIANT * p)
	{
		::VariantClear(p);
	}
};

template <typename T>
struct _CopyItfFromAdaptItf
{
	static HRESULT copy(T ** p1, const CAdapt< CComPtr<T> > * p2)
	{
		if(*p1 = p2->m_T)
		{
			(*p1)->AddRef();
			return S_OK;
		}
		return E_POINTER;
	}
	
	static void init(T ** p){};

	static void destroy(T ** p)
	{
		if(*p)
			(*p)->Release();
	}
};

template <class T, class CollType, class ItemType, class CopyItem, class EnumType>
class ISafeCollectionOnSTLImpl : public T
{
public:

	ISafeCollectionOnSTLImpl()
	{
		::InitializeCriticalSection(&m_csCollection);
	}

	~ISafeCollectionOnSTLImpl()
	{
		::DeleteCriticalSection(&m_csCollection);
	}

	STDMETHOD(get_Count)(long* pcount)
	{
		HRESULT hr = OnInterfaceAccess();
		if(hr != S_OK)
			return hr;

		CCritSecLock cs(m_csCollection);

		if (pcount == NULL)
			return E_POINTER;
		ATLASSUME(m_coll.size() <= LONG_MAX);

		*pcount = (long)m_coll.size();

		return S_OK;
	}

	STDMETHOD(get_Item)(long Index, ItemType* pvar)
	{
		HRESULT hr = OnInterfaceAccess();
		if(hr != S_OK)
			return hr;

		CCritSecLock cs(m_csCollection);

		if(Index < 1 || Index > (long)m_coll.size())
			return OnIndexOutOfRange();

		//Index is 1-based;
		if (pvar == NULL)
			return E_POINTER;

		hr = E_FAIL;
		Index--;
		CollType::const_iterator iter = m_coll.begin();
		while (iter != m_coll.end() && Index > 0)
		{
			iter++;
			Index--;
		}
		if (iter != m_coll.end())
			hr = CopyItem::copy(pvar, &*iter);
		return hr;
	}

	STDMETHOD(get__NewEnum)(IUnknown** ppUnk)
	{
		HRESULT hr = OnInterfaceAccess();
		if(hr != S_OK)
			return hr;

		CCritSecLock cs(m_csCollection);

		if (ppUnk == NULL)
			return E_POINTER;
		*ppUnk = NULL;
		CComObject<EnumType>* p;
		hr = CComObject<EnumType>::CreateInstance(&p);
		if (SUCCEEDED(hr))
		{
			hr = p->Init(this, m_coll);
			if(hr == S_OK)
				hr = p->QueryInterface(__uuidof(IUnknown), (void**)ppUnk);
		}
		if (hr != S_OK)
			delete p;
		return hr;
	}

	CollType m_coll;
	CRITICAL_SECTION m_csCollection;

protected:

	virtual HRESULT OnIndexOutOfRange(){return E_INVALIDARG;};
	virtual HRESULT OnInterfaceAccess(){return S_OK;}; // Notifies about access to the interface properties or methods;
};

#define DECLARE_PSL_COLLECTION(Col, T)\
	typedef vector<CAdapt<CComPtr<IPSL##T> > > Col##Type;\
	typedef CComEnumOnSTL<IEnumVARIANT, &IID_IEnumVARIANT, VARIANT, _CopyVariantFromAdaptItf<IPSL##T>, Col##Type, CComGlobalsThreadModel > C##T##EnumImpl;\
	typedef ISafeCollectionOnSTLImpl<IDispatchImpl<IPSL##Col, &IID_IPSL##Col, &LIBID_ProSysLib, PSL_HiVersion, PSL_LoVersion>, Col##Type, IPSL##T*, _CopyItfFromAdaptItf<IPSL##T>, C##T##EnumImpl> C##Col##Collection;

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)

About the Author

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

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