Use COM in C ++ Builder

I'm new to COM libraries, and I'm stuck on using the COM DLLs in my C ++ Builder (XE2) application. DLL is registered. What are the steps that allow me to create objects belonging to such a DLL and call their methods? I mean statically.

I could not find the tutorial, but I saw different ways:

  • Component> Import Component> it creates a new shell ... and then what?
  • import DLL with an absolute path (why? is it registered in the system)

    #import "C:\Path\to\the\LIB1.dll" rename_namespace ("LIB1") 

    ... and then what?

  • use CoCreateInstance ... how exactly? without import / inclusion?

In Visual C #, I do it just by adding a link and using !

I am very confused! Any help is appreciated.

+4
source share
1 answer

I found a way (but tell me if there are any better):

  • Component> Import Component ...> Import Type Library> select library
  • Unit Dir Name = and uncheck "Create component wrappers"
  • "Add Block to MyProject.cbproj Project"> "Finish"
  • in the client class> File> Use Block ...> select the created device
  • in the client class write this code to use the COM DLL:

     CoInitialize(NULL); //Init COM library DLLs ICompany *company; HRESULT hr = CoCreateInstance ( CLSID_Company, NULL, CLSCTX_INPROC_SERVER, IID_ICompany, (void**) &company ); if (SUCCEEDED (hr)) { //TODO here you can use your company object! //and finally release such resource company->Release(); } CoUninitialize(); 

Where Company was the source class that the DLL was open that I wanted to make out.

Introduction to COM - what it is and how to use it. helped me a lot.

Note , this requires the creation of * _TLB. * and * _OCX. * units . Is there any way to avoid this?

+1
source

Source: https://habr.com/ru/post/1446990/


All Articles