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

Interface and Wrappers

COM/DCOM is based on interfaces. Interfaces are standard protocol on which server and client exchange data parameters. These interfaces are written in Interface definition language. C++ compiler generates the raw C++ abstract class from this interface.

	[
		object,
		uuid(26AA3AE9-D24D-4881-9972-4B42B6B4A9B0),
		dual,
		helpstring("ICalculate Interface tiny calculator demo opject"),
		pointer_default(unique)
	]
	interface ICalculate : IDispatch
	{
		[propget, id(1), helpstring("Operand1 of calculator")]
		HRESULT Operand1([out, retval] long *pVal);
		
		[propput, id(1), helpstring("Operand1 of calculator")]
		HRESULT Operand1([in] long newVal);
		
		[propget, id(2), helpstring("Operand2 of calculator")]
		HRESULT Operand2([out, retval] long *pVal);
		
		[propput, id(2), helpstring("Operand2 of calculator")]
		HRESULT Operand2([in] long newVal);
		
		[id(3), helpstring("calculator main Operation method")]
		HRESULT DoOperation([in] char operation);
		
		[propget, id(4), helpstring("Result of calculator")]
		HRESULT Result([out, retval] long *pVal);
	};

Interface in C++ abstract class

MIDL compiler will generate the C++ raw interface from IDL file. Now server developer inherits this class in a wrapper and implements its background functionalities. While client also inherits this class and wraps it in C++ class. Client has to call raw virtual interfaces with arguments and has to process the output and deal with the error codes.

class ICalculate : public IDispatch
{
  public:
    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;
};

C++ wrapper class

This abstract class is used in both COM server and COM client applications. COM server wrapper should inherit it and should implement all the functions.

class CCalculateServer : public ICalculate
{
virtual HRESULT get_Operand1(long *pVal)
{
	*pVal = Op1;
	return S_OK;
}

virtual HRESULT put_Operand1(long newVal)
{
	Op1 = newVal;

	return S_OK;
}

virtual HRESULT get_Operand2(long *pVal)
{
	*pVal = Op2;

	return S_OK;
}

virtual HRESULT put_Operand2(long newVal)
{
	Op2 = newVal;

	return S_OK;
}

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

virtual HRESULT get_Result(long *pVal)
{
	*pVal = Result;
	return S_OK;
}
};

Client wrapper should have a pointer of this interface inside the wrapper class. Client can call CreateInstance/QueryInterface to get an object instance from the server. Now client calls the raw interfaces from the wrapped functions. Client can validate input arguments, process output arguments and handle error codes.

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

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 Thu 01 May/2014 14:12:29(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.