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

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.

#