Define interface using C++

Interface can be defined as a class with one or more public pure virtual functions. Interface used in COM/DCOM should always return success or error as HRESULT. Input arguments are as normal as input variables and output/return arguments should be passed as reference or pointer.

class ICalculate : public IDispatch
{
public:
  virtual HRESULT get_Operand1(long *pVal) = 0;
  virtual HRESULT put_Operand1(long newVal) = 0;
  virtual HRESULT get_Operand2(long *pVal) = 0;
  virtual HRESULT put_Operand2(long newVal) = 0;
  virtual HRESULT DoOperation(char operation) = 0;
  virtual HRESULT get_Result(long *pVal) = 0;
};

Interface IDL

Interface should be defined in IDL file using interface definition language. Interface defined in IDL can be used in all programming languages. C/C++/C#, VB, Java Script, JAVA and all language supports these standard.

  • Define an unique GUID and other attributes of the interface in [].
  • Define the class structure of the interface with interface name and the inherited interface name.
  • Add the properties and methods under {}
  • Properties and methods should have ID and attributes inside []
  • Properties and methods should have a function prototype

Now this interface should be used to access one or more coclasses.

Interface definition syntax

[<Interface ID and attribute>]
interface <Interface Name> : <Inherited from interface>
{
  [<get/put/method ID/attributes>]<Property get/put/method prototype>;
};

Example Interface in IDL

// MathLib.idl : IDL source for MathLib.dll
//

// This file will be processed by the MIDL tool to
// produce the type library (MathLib.tlb) and marshalling code.

import "oaidl.idl";
import "ocidl.idl";
  [
    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);
  };

[
  uuid(B9BBFE19-AC06-48A4-85BB-F9F8380A511B),
  version(1.0),
  helpstring("MathLib 1.0 Type Library")
]
library MATHLIBLib
{
  importlib("stdole32.tlb");
  importlib("stdole2.tlb");

  [
    uuid(F082638A-748A-44AC-ABB3-DC346B7F128A),
    helpstring("Calculate Class")
  ]
  coclass Calculate
  {
    [default] interface ICalculate;
  };
};

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.

Learn on Youtube

#