Click here to Skip to main content
15,884,472 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 191K   3.4K   232  
A simplified and unified way for accessing most frequently used information about Process, System, and Environment.
#pragma once

#include "ProSysLib_i.h"

////////////////////
// Exception codes;
//
#define EX_MIN				exGeneric			// Minimum exception code;

#define EX_GENERALFAILURE	exGeneric			// Generic or unknown Run-Time error occurred.
#define EX_INDEXOUTOFRANGE	exIndexOutOfRange	// Object index is outside the range of array bounds.
#define EX_NOTIMPLEMENTED	exNotImplemented	// Property or method was only declared but not implemented in your version of ProSysLib.
#define EX_LOWMEMORY		exLowMemory			// Failed to allocate enough memory to perform the operation.
#define EX_NOACCESS			exAccessDenied		// Insufficient Access Rights or Privileges to perform the operation.
#define EX_INVALIDPARAMETER	exInvalidParameter	// Value for property or a method parameter wasn�t recognized as valid.
#define EX_WMIERROR			exWMIError			// A WMI-specific error occurred.

#define EX_MAX				exWMIError			// Maximum exception code;

#define COM_ERROR_BASE		0x200				// Custom error code base as recommended by Microsoft;

#define DECLARE_EXCEPTION(A) const int E_##A = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, COM_ERROR_BASE + EX_##A);

///////////////////////////////////////
// Declaring all supported exceptions;
//
DECLARE_EXCEPTION(GENERALFAILURE)
DECLARE_EXCEPTION(INDEXOUTOFRANGE)
DECLARE_EXCEPTION(NOTIMPLEMENTED)
DECLARE_EXCEPTION(LOWMEMORY)
DECLARE_EXCEPTION(NOACCESS)
DECLARE_EXCEPTION(INVALIDPARAMETER)
DECLARE_EXCEPTION(WMIERROR)

HRESULT GetGlobalExitCode(PSLException exCode, const CLSID & clsID, const IID ID);

template<const CLSID * clsID, const IID * ID>
class CPSLException: public ISupportErrorInfo
{
public:

	CPSLException():m_exCode(exNone){}

	void SetException(PSLException exCode)
	{
		m_exCode = exCode;
	}

	HRESULT MakeException(PSLException exCode)
	{
		m_exCode = exCode;
		return GetGlobalExitCode(exCode, *clsID, *ID);
	}

protected:

	STDMETHODIMP InterfaceSupportsErrorInfo(REFIID riid)
	{
		static const IID* arr[] = {ID};
		for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
		{
			if(InlineIsEqualGUID(*arr[i], riid))
				return S_OK;
		}
		return S_FALSE;
	}

	HRESULT GetExitCode()
	{
		return GetGlobalExitCode(m_exCode, *clsID, *ID);
	}

private:

	PSLException m_exCode; // Exception Code;
};

#define PSL_BEGIN	SetException(exNone);try{
#define PSL_END	}catch(...){SetException(exGeneric);}return GetExitCode();

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