Multiple Intrface to a single COM Object

I have one component class and I want several interfaces to this particular class which have their own methods and I should be able to invoke these methods using the VB.
vipin072998Asked:
Who is Participating?
 
Tommy HuiConnect With a Mentor EngineerCommented:
The short answer is you need to define these interfaces in the .IDL file and put them in the type library. VB will then read your type library and generate the proper code to call your methods. The important parts of the IDL file are the interface declaration and the coclasses. The interface declaration should typically be dual:

      [
            object,
            uuid(BB627197-8C3C-22D3-AF65-00A0C9A71054),
            dual,
            helpstring("ITimeSeriesOne Interface"),
            pointer_default(unique)
      ]
      interface ITimeSeriesOne : IDispatch
      {
            [id(1), helpstring("method SetStartingDate")] HRESULT SetStartingDate([in] DATE startDate);
            [id(2), helpstring("method SetEndingDate")] HRESULT SetEndingDate([in] DATE endingDate);
            [id(3), helpstring("method SetDataType")] HRESULT SetDataType([in] ETs1DataType dataType);
            [id(4), helpstring("method SetSymbol")] HRESULT SetSymbol([in] BSTR symbol);
            [id(5), helpstring("method GetData")] HRESULT GetData();
            [id(6), helpstring("method GetFieldName")] HRESULT GetFieldName([in] ETs1Fid id, [out, retval] VARIANT* fieldName);
      };

You would have multiple of these interface declarations. Then in your coclass statement, you need to list the interfaces your object supports:

      [
            uuid(BB627198-8C3C-22D3-AF65-00A0C9A71054),
            helpstring("TimeSeriesOne Class")
      ]
      coclass TimeSeriesOne
      {
            [default] interface ITimeSeriesOne;
            interface IIntf2;
            interface IIntf3;
            // etc
            [default, source] dispinterface _ITimeSeriesOneEvents;
      };

Finally, in your C++ code, you will need to respond correct to the QueryInterface code.

In VB, you can do the following to invoke the QueryInterface:

  dim obj as new TimeSeriesOne
  dim int2 as Intf2;
  set int2 = obj;  // calls queryinterface

0
 
hedCommented:
what language do you use to implement the com object itself ?
0
 
Alexey FedorovSoftware developerCommented:
It is simple to do. Here the example.
It is definition of several interfaces and one COM object with multiple interfaces on IDL.

import "oaidl.idl";
import "ocidl.idl";
      [
            object,
            uuid(54E18944-8338-11D3-8DDB-00504E02C39D),
            oleautomation,
            dual,
            helpstring("IDTst Interface"),
            pointer_default(unique)
      ]
      interface IDTst : IDispatch
      {
            [propget, id(1)] HRESULT NameProp([out, retval] BSTR *pVal);
            [propput, id(1)] HRESULT NameProp([in] BSTR newVal);

        [propget, bindable, requestedit, id(111)] HRESULT NamePropB([out, retval] BSTR *pVal);
            [propput, bindable, requestedit, id(111)] HRESULT NamePropB([in] BSTR newVal);

            [id(2)] HRESULT TstCall( [in]BSTR bs );
      };

      [
            object,
            uuid(5EDA4011-834A-11d3-8DDB-00504E02C39D),
            oleautomation,
            dual,
            helpstring("IDTst2 Interface"),
            pointer_default(unique)
      ]
      interface IDTst2 : IDTst
      {
            [propget, id(21)] HRESULT NameProp2([out, retval] BSTR *pVal);
            [propput, id(21)] HRESULT NameProp2([in] BSTR newVal);
            [id(22)] HRESULT SetTwoProp([in] BSTR sNameProp, [in, optional, defaultvalue("NameProp2")] BSTR sNameProp2);
      };

      [
            object,
            uuid(5EDA4012-834A-11d3-8DDB-00504E02C39D),
            oleautomation,
            dual,
            helpstring("IDTstAdv Interface"),
            pointer_default(unique)
      ]
      interface IDTstAdv : IDispatch
      {
            [propget, id(1)] HRESULT NamePropAdv([out, retval] BSTR *pVal);
            [propput, id(1)] HRESULT NamePropAdv([in] BSTR newVal);
            [id(2)] HRESULT SetTwoProp2([in] BSTR sNameProp, [in, optional, defaultvalue("NamePropAdv")] BSTR sNamePropAdv);
      };
      [
            object,
            uuid(54E18947-8338-11D3-8DDB-00504E02C39D),
            oleautomation,
            dual,
            helpstring("ITAgg Interface"),
            pointer_default(unique)
      ]
      interface ITAgg : IDispatch
      {
            [id(1), helpstring("method GHellAggregat")] HRESULT GHellAggregat([in] long lVal, [out, retval] BSTR* pRes);
      };

[
      uuid(54E18938-8338-11D3-8DDB-00504E02C39D),
      version(1.0),
      helpstring("DUAL_TST 1.0 Type Library")
]
library DUAL_TSTLib
{
      importlib("stdole32.tlb");
      importlib("stdole2.tlb");

      [
            uuid(54E18945-8338-11D3-8DDB-00504E02C39D),
            helpstring("DTst Class")
      ]

      coclass TAgg
      {
            [default] interface ITAgg;

            interface IDTst2;
            interface IDTst;
            interface IDTstAdv;            
      };
};

Thus, we have COM object "TAgg", which supports 4 interfaces: ITAgg, IDTst2, IDTst and IDTstAdv.

Using "TAgg" in Visual Basic is simple.
Visual Basic shows to you only "ITAgg" interface methods and "ITAgg" properties (marked as [default]). But you can use any of other suppoted interfaces through next trick:

Dim obj1 As New TAgg
Dim sRes As String, lV As Long
sRes = obj1.GHellAggregat(lV)

Dim obj2 As IDTst 'now using one of nondefault interfaces
Set obj2 = obj1
obj2.NameProp = "Test"

'It is all, what you need to do!

Good luck!

0
All Courses

From novice to tech pro — start learning today.