Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. howto use COM type library

howto use COM type library

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    addebito
    wrote on 10 Feb 2020, 08:55 last edited by
    #1

    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.

    O 1 Reply Last reply 10 Feb 2020, 10:36
    0
    • A addebito
      10 Feb 2020, 08:55

      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.

      O Offline
      O Offline
      ODБOï
      wrote on 10 Feb 2020, 10:36 last edited by
      #2

      @addebito hi
      see if qaxobject class can help

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 10 Feb 2020, 19:09 last edited by
        #3

        Hi,

        From what I remember, you can still use that kind of code with Qt.

        However you should likely wrap that in a class.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply 11 Feb 2020, 09:14
        2
        • S SGaist
          10 Feb 2020, 19:09

          Hi,

          From what I remember, you can still use that kind of code with Qt.

          However you should likely wrap that in a class.

          J Offline
          J Offline
          JonB
          wrote on 11 Feb 2020, 09:14 last edited by JonB 2 Nov 2020, 09:17
          #4

          @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 to CoInitialize(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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            addebito
            wrote on 11 Feb 2020, 16:46 last edited by
            #5

            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.

            1 Reply Last reply
            0

            4/5

            11 Feb 2020, 09:14

            • Login

            • Login or register to search.
            4 out of 5
            • First post
              4/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved