howto use COM type library
-
Hi at all,
I have to use an external COM library (EZSocketNc.dll) under Windows 10 in QT.
I never use that so I read some docs.I have a functional example in C++ VS2017
int main(void) { HRESULT hr = S_OK; // COM initialization hr = CoInitialize(NULL); if (S_OK != hr) { wprintf(L"Failed in CoInitialize!\n"); return 1; } //Communication object IEZNcCommunication3 *pIEZNcCom = NULL; //EZNcCommunication object creation CLSID clsid; CLSIDFromProgID(L"EZSocketNc.EZNcCommunication", &clsid); hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IEZNcCommunication3, (void**)&pIEZNcCom); if (S_OK != hr) { wprintf(L"EZSocket is not installed!\n"); return 2; } //Opens the communication line long lPort = 683; long plRet; //FunctionSetTCPIPProtocol hr = pIEZNcCom->SetTCPIPProtocol(L"192.168.50.51", lPort, &plRet); if (S_OK != hr) { printf("Error SetTCPIPProtocol: 0x%x", plRet); return 3; } //Function Open2 long lSystemType = EZNC_SYS_MELDAS800M; long lMachine = 1; LONG lTimeOut = 100; hr = pIEZNcCom->Open2(lSystemType, lMachine, lTimeOut, &plRet); if (S_OK != hr) { printf("Error Open2: 0x%x", plRet); return 4; } else { printf("Connection successfull"); } }
...but I read that in QT is a little bit different, I used the dumpcpp utility from prompt to create a wrapper like this:
dumpcpp.exe -o EZSocketNc "C:\\Program Files (x86)\\EZSocket\\EZSocketNc\\EZSocketNc.dll"
the dumpcpp has generated the EZSocketNc.h and EZSocketNc.cpp, but now when I try to use it I don't find the same function... for example the "SetTCPIPProtocol" is missing in the class "EZSOCKETNCLib::EZNcCommunication" and also I don't have the "Open2" or "Open3" function but only a "Open".
That is the right approach?
Do you have any advice?
Thank you very much. -
Hi,
From what I remember, you can still use that kind of code with Qt.
However you should likely wrap that in a class.
-
@SGaist said in howto use COM type library:
From what I remember, you can still use that kind of code with Qt.
@addebito
IIRC, the one thing you may have to be careful about/have trouble with is the call toCoInitialize(NULL);
. I think Qt already does this once (might depend on what the code does), there can be issue about this. You might have to omit it, and you might be restricted on what apartment models you can use. -
Thanks @LeLev , @SGaist , @JonB .
Actually this is the code that works on QT.
int hr = S_OK; // hr = CoInitialize(nullptr); // if (S_OK != hr) // { // qDebug() << "Failed in CoInitialize!"; // return 1; // } //Communication object IEZNcCommunication3 *pIEZNcCom = nullptr; CLSID clsid; CLSIDFromProgID(L"EZSocketNc.EZNcCommunication", &clsid); hr = CoCreateInstance(clsid, nullptr, CLSCTX_INPROC_SERVER, IID_IEZNcCommunication3, (void**)&pIEZNcCom); if (S_OK != hr) { qDebug() << "EZSocket is not installed!"; return 2; } long lPort = 683; long plRet; //FunctionSetTCPIPProtocol hr = pIEZNcCom->SetTCPIPProtocol(L"192.168.1.10", lPort, &plRet); if (S_OK != hr) { qDebug() << "Error SetTCPIPProtocol: 0x%x" << plRet; return 3; } //Function Open2 long lSystemType = EZNC_SYS_MELDAS800M; long lMachine = 1; LONG lTimeOut = 100; hr = pIEZNcCom->Open2(lSystemType, lMachine, lTimeOut, &plRet); if (S_OK != hr) { qDebug() << "Error Open2: 0x%x" << plRet; return 4; } qDebug() << "Connection successfull"; long ret = 0; hr = pIEZNcCom->SetHead(1, &ret); IEZNcTool3 *pIEZNcTool = nullptr; pIEZNcCom->QueryInterface(IID_IEZNcTool3, (void**)&pIEZNcTool); if (pIEZNcTool != nullptr) { // Some stuff... }
I've commented out the CoInitilize function because return FALSE.
https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize
FALSE = The COM library is already initialized on this thread.Tomorrow I'll move the code above in a QThread object, so I think I'll have to uncomment the CoInitilize function and put at the end the CoUninitialize.
Thanks for the time being.