Embed COM type library resouce (TLB) in MinGW-built DLL

0

I have a COM type library built with midl, call it "mylib.tlb". I want to embed it in a DLL, "mylib.dll" which is compiled and linked with MinGW. The DLL "mylib.dll" also contains a function myfunc(). I want to be able to embed "mylib.tlb" as a resource within "mylib.dll" so that myfunc()—which is also within "mylib.dll"—can load "mylib.tlb" using the COM function LoadTypeLibEx().

In other words:

void myfunc() // a function living within "mylib.dll"
{
    // ...
    ITypeLib * result(0);
    HRESULT hr = LoadTypeLibEx(path_to_this_dll, REGKIND_NONE, &result);
    // should successfully load "mylib.tlb" into result!!!
}

This isn't working for me and I'm not sure why. LoadTypeLibEx() is returning -2147312566 = 0x80029c4a = TYPE_E_CANTLOADLIBRARY. I can't tell if that's because it can't load the DLL at all, or if it can't find the type library within the DLL. I have verified that my path_to_this_dll variable contains the path to "mylib.dll", so I'm hoping it's the latter.

I am trying to build "mylib.tlb" into "mylib.dll" using the following MinGW tool command line:

$ windres myres.rc myres.o
$ ar rcs mylib.a a.o b.o ... myres.o ... x.o
$ g++ -o mylib.dll -shared ... mylib.a ...

Where "myres.rc" just looks like this:

1000 typelib "mylib.tlb"

When I build "mylib.dll" I can verify that it contains a bunch of other resources (icons, cursors, and so on) using a program called ResourceHacker, but I can't tell from ResourceHacker whether the TLB made it into the DLL.

  • Is my resource script ("myres.rc") right?
  • What other resource tools could I use to check if the TLB made it into the DLL?
  • Am I doing something wrong in principle?

EDIT: I noticed that the TLB file contains the string "MIDL", so I tried grepping for that in various files. Somehow the type library resource isn't getting into the DLL:

$ grep MIDL mylib.tlb
Binary file mylib.tlb matches
$ grep MIDL myres.o
Binary file myres.o matches
$ grep MIDL mylib.a
Binary file mylib.a matches
$ grep MIDL mylib.dll
$ echo $?
1

Why would the linker not put the TLB resource into the DLL?

c++
dll
com
mingw
embedded-resource
asked on Stack Overflow Oct 22, 2013 by 0xbe5077ed • edited Oct 22, 2013 by 0xbe5077ed

1 Answer

1

The problem is that the MinGW linker can only handle one object file containing resources. It takes the resources out of the first one and ignores all of the other ones.

So I was doing:

$ g++ -o mylib.dll -shared main_resources.o myres.o

where "myres.o" contains the TLB. If I invert the order of the objects on the command line, the DLL contains the TLB and none of the other resources. Other than that, my code in the question section works fine.

See:

answered on Stack Overflow Oct 22, 2013 by 0xbe5077ed

User contributions licensed under CC BY-SA 3.0