COM Wrapper classes

Developer can implement wrapper classes written from scratch or can use different tools and libraries available with VC++ IDE. ATL or ActiveX template library is a set of C++ template libraries to easily implement server wrapper classes. On the other hand Client wrapper classes can be written as needed by developer or autogenerated type library header classes can be used.

Server wrapper class

Server should derive a class from ICalculate/interface and implement its all pure virtual member functions. This class CCalculate is the server wrapper C++ class. We have used ATL template libraries and multiple inheritance has been used.

/////////////////////////////////////////////////////////////////////////////
// CCalculate
class ATL_NO_VTABLE CCalculate : 
	public CComObjectRootEx,
	public CComCoClass,
	public IDispatchImpl
{
protected:
	long Op1,Op2,Result;
public:
	CCalculate()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_CALCULATE)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CCalculate)
	COM_INTERFACE_ENTRY(ICalculate)
	COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()

// ICalculate
public:
	STDMETHOD(get_Result)(/*[out, retval]*/ long *pVal);
	STDMETHOD(DoOperation)(/*[in]*/ unsigned char operation);
	STDMETHOD(get_Operand2)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_Operand2)(/*[in]*/ long newVal);
	STDMETHOD(get_Operand1)(/*[out, retval]*/ long *pVal);
	STDMETHOD(put_Operand1)(/*[in]*/ long newVal);
};

STDMETHODIMP CCalculate::get_Operand1(long *pVal)
{
	*pVal = Op1;
	return S_OK;
}

STDMETHODIMP CCalculate::put_Operand1(long newVal)
{
	Op1 = newVal;

	return S_OK;
}

STDMETHODIMP CCalculate::get_Operand2(long *pVal)
{
	*pVal = Op2;

	return S_OK;
}

STDMETHODIMP CCalculate::put_Operand2(long newVal)
{
	Op2 = newVal;

	return S_OK;
}

STDMETHODIMP CCalculate::DoOperation(unsigned char operation)
{

	switch(operation){
	case '+':
		Result = Op1 + Op2;
		break;
	case '-':
		Result = Op1 - Op2;
		break;
	case '*':
		Result = Op1 * Op2;
		break;
	case '/':
		Result = Op1 / Op2;
		break;
	default:
		;
}
return S_OK;
}

STDMETHODIMP CCalculate::get_Result(long *pVal)
{
	*pVal = Result;

	return S_OK;
}

Client wrapper C++ raw

Client wrapper from raw C++ class can be made manually or the component developer can include the header and library/C++ implementation file along with the COM/DCOM server package. This is wrapper over raw interface class and can provide error handling.

class CCalculateClient 
{
private:
  ICalculate m_pCalculate;
public:
  HRESULT CreateInstance()
  {
	  m_pCalculate = NULL;
	  HRESULT hr;
	  hr = CoCreateInstance(CLSID_Calculate, 
	  NULL, CLSCTX_INPROC_SERVER,
	  IID_ICalculate,
     reinterpret_cast(&m_pCalculate));
	 return hr;
	  
  }
long GetOperand1 ( ) {
    long _result;
    HRESULT _hr = m_pCalculate->get_Operand1(&_result);
    if (FAILED(_hr)) throw _hr;
    return _result;
}

void SetOperand1 ( long pVal ) {
    HRESULT _hr = m_pCalculate->put_Operand1(pVal);
    if (FAILED(_hr)) throw _hr;
}

long GetOperand2 ( ) {
    long _result;
    HRESULT _hr = m_pCalculate->get_Operand2(&_result);
    if (FAILED(_hr)) throw _hr;
    return _result;
}

void SetOperand2 ( long pVal ) {
    HRESULT _hr = m_pCalculate->put_Operand2(pVal);
    if (FAILED(_hr)) throw _hr;
}

HRESULT DoOperation ( char operation ) {
    HRESULT _hr = m_pCalculate->raw_DoOperation(operation);
    if (FAILED(_hr)) throw _hr;
    return _hr;
}

long GetResult ( )
{
    long _result;
    HRESULT _hr = m_pCalculate->get_Result(&_result);
    if (FAILED(_hr)) throw hr;
    return _result;
}
};

Client wrapper (type library header)

Client wrapper can be auto generated using type library file. These files are generated with #import directive given to COM/DCOM server binary file or type library file. Type library header (.tlh) contains the prototype header and type library implementation (.tlh) file contains the wrapper implementation.

/* -- Type lib header (.tlh) -- */
 
ICalculate : IDispatch
{
    //
    // Property data
    //

    __declspec(property(get=GetOperand1,put=PutOperand1))
    long Operand1;
    __declspec(property(get=GetOperand2,put=PutOperand2))
    long Operand2;
    __declspec(property(get=GetResult))
    long Result;

    //
    // Wrapper methods for error-handling
    //

    long GetOperand1 ( );
    void PutOperand1 (
        long pVal );
    long GetOperand2 ( );
    void PutOperand2 (
        long pVal );
    HRESULT DoOperation (
        char operation );
    long GetResult ( );

    //
    // Raw methods provided by interface
    //

    virtual HRESULT __stdcall get_Operand1 (
        long * pVal ) = 0;
    virtual HRESULT __stdcall put_Operand1 (
        long pVal ) = 0;
    virtual HRESULT __stdcall get_Operand2 (
        long * pVal ) = 0;
    virtual HRESULT __stdcall put_Operand2 (
        long pVal ) = 0;
    virtual HRESULT __stdcall raw_DoOperation (
        char operation ) = 0;
    virtual HRESULT __stdcall get_Result (
        long * pVal ) = 0;
};

/* -- Type lib implementation (.tli) -- */

inline long ICalculate::GetOperand1 ( ) {
    long _result;
    HRESULT _hr = get_Operand1(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;
}

inline void ICalculate::PutOperand1 ( long pVal ) {
    HRESULT _hr = put_Operand1(pVal);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
}

inline long ICalculate::GetOperand2 ( ) {
    long _result;
    HRESULT _hr = get_Operand2(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;
}

inline void ICalculate::PutOperand2 ( long pVal ) {
    HRESULT _hr = put_Operand2(pVal);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
}

inline HRESULT ICalculate::DoOperation ( char operation ) {
    HRESULT _hr = raw_DoOperation(operation);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _hr;
}

inline long ICalculate::GetResult ( ) {
    long _result;
    HRESULT _hr = get_Result(&_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _result;
}

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

#