Get functions/objects of imported .tlb
Asked Answered
B

1

2

I've got a program which shipped with a .tlb file to access some functions/objects (read variables etc.) with my own C++ program. I did a search and imported the .tlb file with:

#import "MyLib.tlb" named_guids no_namespace

I can also import it by using the libid from oleview.exe (ProgId does not work).

Even if I get some warnings (as follows), my program still runs:

C4278 ['TextOut', 'CreateEvent', 'DeleteFile'] is already a macro; use the 'rename' qualifier

But.. how can I gain access of the functions/objects now? Sorry I'm a beginner so please be patient. Does it work somehow with IDispatch? Do I need to import some more dll's or do I need more #include directives?

I'm using Visual C++ 2008 Express.

--
Edit: Ok sorry, I already have access to the header of the objects (I see "Application" in auto completion) but I have no idea how to get the objects.

Object Overview

And I think I found the related wikipedia article.

Bullpen answered 6/12, 2011 at 7:54 Comment(1)
books.google.com/…Junco
D
2

Importing type library gives you description of all the interfaces and identifiers of that library. Normally you should not include additionally any header files. You should just normally create these interfaces using COM smart pointer and call their methods:

CComPtr pInterface;
pInterface.CoCreateInstance(__uuidof("ClassNameFromTLB"));
pInterface->CallMethod();
Distillery answered 6/12, 2011 at 11:9 Comment(7)
Thanks for your answer! Do I need ATL for CComPtr because I have MSVC++ 2008 Express Edition and your Code does not work for me. Can you explain it a litte more please?Bullpen
Use the _com_ptr_t derived smart pointer types that are auto-generated by #import instead. Available in Express and generally a much better solution if a type library is available.Junco
Thank you @HansPassant for the hint and the link. But it's really hard for me to understand it. Shall I do it like that?: _COM_SMARTPTR_TYPEDEF(Application, __uuidof(Application)); And then: _com_ptr_t<Application>::CreateInstance(Application); Something like that? embarrassedBullpen
The compiler already did that for you, just use the ApplicationPtr type. Getting good docs for this is a bit hard to come by, at least review the book link I left.Junco
Now I got it. Didn't know that CoHexagon and IShapeEdit were from their example. Now I realize that the book explains it really good! Thank you very much @HansPassant!Bullpen
Now someone just has to told me how to get the value of the pointer. I did IApplicationPtr spApplication(__uuidof(Application)); and then spApplication->Path; But when I print *spApplication I get an address, not the path to the application..Bullpen
This works if the import is within the same class. What should I do if I have to import the tlb in the main.cpp but want to use the class in another file? Do I have to include the .tlh and .tli files? where do I find them?Cereus

© 2022 - 2024 — McMap. All rights reserved.