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

Smart pointers

Smart pointers are auto generated classes which are wrappers over raw interfaces. VC++ MIDL compiler can create these files automatically with the import preprocessor directive. We need to import a type library or COM DLL/Exe binary which include type library. Compiler generates the header and implementation intermediate files.

Type library header(.tlh)

This file contains the interface GUID details and the prototype of the interface.

// Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0 (7328f51b).
//
// MathLib.tlh
//
// C++ source equivalent of Win32 type library MathLib.dll
// compiler-generated file created 01/26/18 at 19:19:27 - DO NOT EDIT!

#pragma once
#pragma pack(push, 8)

#include 

namespace MATHLIBLib {

//
// Forward references and typedefs
//

struct /* coclass */ Calculate;
struct __declspec(uuid("26aa3ae9-d24d-4881-9972-4b42b6b4a9b0"))
/* dual interface */ ICalculate;

//
// Smart pointer typedef declarations
//

_COM_SMARTPTR_TYPEDEF(ICalculate, __uuidof(ICalculate));

//
// Type library items
//

struct __declspec(uuid("f082638a-748a-44ac-abb3-dc346b7f128a"))
Calculate;
    // [ default ] interface ICalculate

struct __declspec(uuid("26aa3ae9-d24d-4881-9972-4b42b6b4a9b0"))
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 library implementation(.tli)

This file contains the wrapper over raw interface calls and error handing implementation. This will make the actual programming very easy. The actual client code will look like as easy as Visual Basic code.

//
// Wrapper method implementations
//

#include "MathLib.tli"

} // namespace MATHLIBLib

#pragma pack(pop)



// Created by Microsoft (R) C/C++ Compiler Version 12.00.8168.0 (7328f51b).
//
// MathLib.tli
//
// Wrapper implementations for Win32 type library MathLib.dll
// compiler-generated file created 01/26/18 at 19:19:27 - DO NOT EDIT!

#pragma once

//
// interface ICalculate wrapper method implementations
//

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

Client using Smart pointers

Client code imports MathLib.dll and compiler generates these above files. This program can calculate addition, subtraction, multiplication and division with two operands and one result is obtained after operation is performed.

#include <windows.h>
#include <stdio.h>
#import "MathLib.dll"
#include <objbase.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
  long lOperand1, lOperand2;
  unsigned char cOperation;

  CoInitialize(NULL);
  MATHLIBLib::ICalculatePtr MyCalculator;

  if(MyCalculator.CreateInstance("MathLib.Calculate") != 0) {
    cout << "MyCalculator.CreateInstance failed.";
    CoUninitialize();
    return -1;
  }
  cout << "MathLib.Calculate demo using smart pointer" << endl;
  do {
    cout << "Operand 1 : ";
    cin >> lOperand1;
    MyCalculator->PutOperand1(lOperand1);

    cout << "Operand 2 : ";
    cin >> lOperand2;
    MyCalculator->PutOperand2(lOperand2);

    cout << "Operation : ";
    cin >> cOperation;
    MyCalculator->DoOperation(cOperation);

    cout << MyCalculator->GetOperand1() << " " << cOperation 
          << " " << MyCalculator->GetOperand2() << " = "
          << MyCalculator->GetResult() << endl;

    cout << "Do you want to perform more calculation (y/n) ? ";
    cin >> cOperation;

  } while(cOperation == 'y');

  MyCalculator.Release();
  CoUninitialize();
  return 0;
}

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 Wed 30 Apr/2014 01:21:05(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.