How to instantiate a COM object using Qt?

Suppose you have a myprogrammingnotes.dll that contains a COM object. You want to instantiate and use that COM object in your Qt app. How to do that? If myprogrammingnotes.dll contains the type lib of that COM object, you can add the following line in the .pro file of your Qt project:

TYPELIBS = myprogrammingnotes.dll

If you only know the ID of the type lib but do not know the file name that contains the typlib, you can instead use the following line:

 

TYPELIBS = $$system(dumpcpp -getfile {xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx})

When running qmake, it will produce recipes in the generated Makefile.Debug/Makefile.Release to generate a .cpp file(myprogrammingnotes.cpp) and a .h file(myprogrammingnotes.h) from the type lib in myprogrammingnotes.dll. The .cpp/.h contain two classes: YourComClassBinder and IYourComClassBinder in a namespace myprogrammingnotesLib. In your Qt project, you can include the header fileĀ  myprogrammingnotes.h to use the two classes. For example:

#include "myprogrammingnotes.h"

...

myprogrammingnotesLib::YourComClassBinder *p=new myprogrammingnotesLib::YourComClassBinder;
(QAxObject*)p)->generateDocumentation ();
void * pinterface=NULL;
((QAxObject*)p)->queryInterface(QUuid(IID_IContextMenu),&pinterface);
if(pinterface)
{
    CMINVOKECOMMANDINFO ici;
    ici.lpVerb=0;
    LPCMINVOKECOMMANDINFO pici=&ici;
    ((IContextMenu*)pinterface)->InvokeCommand(pici);
}

The “new myprogrammingnotesLib::YourComClassBinder;” will call QAxbase::setControl in the constructor of YourComClassBinder given the CLASSID of YourComClassBinder which eventually calls the COM lib function CoCreateInstance to instantiate the COM object(see QAxBase::initialize in qaxbase.cpp). After getting the pointer to the COM object, you can call its queryInterface function(actually QAxBase::queryInterface) to get the interface you want then call the methods of that interface.

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply