EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of COM/DCOM Questions #Next question

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;
}

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

 Vote 0

Similar topics related to this section

# COM DCOM Interview Questions and Answers :: ALL Interview .com
# COM DCOM - COM DCOM Interview Questions
# COM interview questions | TechInterviews
# COM Interview Questions - CodeProject
# Com Dcom Interview Questions - Com and Dcom in Asp Net
# ATL Interview Questions and Answers :: ALL Interview .com
# ActiveX Interview Questions and Answers :: ALL Interview .com

* #1 webmaster Mon 28 Apr/2014 07:09:34(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.