Importing interfaces in IDL from an external type library - atl

I have two type libraries with COM interfaces that I wrote using ATL and Microsoft's IDL. I would like an interface in one library to inherit from an interface in the other.
Essentially, I would like to do the same thing that Steven described at How do I create interface methods using .tlb types in VS C++?. The only person who answered him did not seem to understand the question.
Here's what I'd like to do, in code:
SomeLibrary DLL/TLB
ISomeInterface.idl
interface ISomeInterface : IDispatch { ... };
SomeLibrary.idl
import "ISomeInterface.idl";
library SomeLibrary
{
interface ISomeInterface;
};
SomeOtherLibrary DLL/TLB
ISomeOtherInterface.idl
// What do I put here so that the MIDL compiler knows
// what to do when it encounters the ISomeInterface type?
interface ISomeOtherInterface : ISomeInterface { ... };
SomeOtherLibrary.idl
import "ISomeOtherInterface.idl";
library SomeOtherLibrary
{
interface ISomeOtherInterface;
};
The MIDL import directive only works when importing IDL files, and I only have a DLL and TLB. I can't use importlib because that only works within a library definition. The MIDL compiler doesn't understand Microsoft's C++ import, importidl, and importlib attributes.
What to do?

If you are willing to introduce a manual step, you can open the tlb in oleview and get a generated .idl file that way. oleview.exe lives in the bin dir of the Windows SDK, eg
C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\oleview.exe

Related

programmatically calling functions from type library

I need to import a type library (it's actually an .exe that gives access to a COM interface implementation).
The thing is that i want to do that but with the MinGW compiler and not VSC++.
There are many topics and threads on SO that explain the difference between the two #import directives, but the main idea is that we can't import type libraries with the MinGW import.
So why not try to load the type library programmatically at runtime using the
LoadTypeLibrary function and the ITypeLib interface.
Except that ITypeLib is rather used to describe the objects stored in the type library.
Now i have run on my .exe code similar to the one in this link here
to retrieve information about my .exe's methods and it shows what i expected (the same information i found in the generated .tlh with vc++)
Is there a way to call directly the library's functions ?? If yes :
Should I use the IDispatch::Invoke method ?
Is there a necessity for binding functions in the type library or mapping the class members (using ITypeComp::Bind) ?
Is there any way to convert the generated .tlh and .tli with vc++ into a normal .h & .cpp file and be able to use it with mingw compiler?
Thank you for your precisions !

Making a DLL into a COM component

I'm new to COM so this maybe a stupid question, but I'll ask it anyway. :)
As far as I understand, in order to make a DLL COM we need to define its interface in a .idl file, compile it with the MIDL compiler and then simply implement the classes that use the defined interfaces. Then we need to expose the DllRegisterServer, DllUnregisterServer and DllGetClassObject. and it should be enough, right?
But I did all this and when I try to check it in a client C++ program, my visual studio says
(on the #import statement) that it can't find the tlh file...
This is how my .idl looks like:
import "someIdl.idl";
[
uuid(some-guid-Num1),
version(1.0),
helpstring("The library's description")
]
library LibName
{
importlib("stdole2.tlb");
[
uuid(some-guid-Num2),
helpstring("Some Description")
]
coclass ClassName
{
interface Interface_defined_in_someIdl;
}
};
Am I missing something in the .idl? Did I get the COM making process wrong?
The problem was that I didn't embed the TLB file into the DLL as resource.

Get functions/objects of imported .tlb

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.
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();

Relationship between IDL and c++ source file

To lean about interfaces, I'm trying to write a test project to implement the IDispatch interface.
So far I've:
created an MFC dll project in visual studio 2010
written the following IDL
[ uuid(68B0FAE7-3828-415D-94B0-720A007311FF), version(1.0) ]
library Test
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(DD35D282-ABEF-4411-B3D1-B4FD848516A5)
]
interface _Test : IDispatch
{
};
};
Created a C header file called Test.h but I'm wandering how I declare this to be linked to the IDL. Any help or hello world tutorials would be great. Thanks
You can you the
midl compiler
It is capable of generating header/sources in c (or C++? don't remember), but most importantly it will get you a typelibrary (tlb) file
#import directive
Will generate c++ definitions for the typelibrary contents.

How do I create interface methods using .tlb types in VS C++?

Background:
The .TLB file contains interfaces written in language 'X'. I don't have .h, .idl, .tlh, or any other header files - just the .TLB file. Language 'X' does not export compatible .h, .idl, etc.
I use the VS wizard to add an ATL simple object to my ATL project.
I want to add a method to the interface of my simple ATL object that uses one of the .TLB defined types for a parameter.
// Something like the following in the .idl file:
interface ISomeInterface : IUnknown {
HRESULT SomeMethod([in] ITypeFromTLB* aVal); // ITypeFromTLB declared in .TLB file.
};
How can I do this? I'm hoping for a wizard, or a line in the .idl interface declaration that would bring in the .tlb information. midl's include (no .tlb), import (no tlb), and importlib (library only) don't seem to provide a solution (I need proxy/stub to be working, so I cannot put this inside the library declaration with the importlib command).
Use #import in cpp/h to bring TLB interfaces to your namespace.
On Visual Studio command line do oleview. Then File -> View Type Lib, give it full path to your foo.tlb. Now in ITypeLib Viewer do File -> Save As .. and you can export all 3 (.h, .idl, .c) from there.