Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
874 views
in Technique[技术] by (71.8m points)

winapi - How do I invoke the MIDL compiler to generate a .TLB file (type library) from an .IDL file?

I am struggling with something seemingly super-simple: I'd like to use the MIDL compiler to generate a type library (.tlb file) from a .idl file. However, I just can't get MIDL to generate a .tlb file.

This is my Foo.idl:

import "unknwn.idl";

[object, uuid(400075B9-4BD6-45A5-B8B7-9DA0CF7B9B13)]
interface IFoo : IUnknown
{
    HRESULT DoFoo([in] int arg, [out, retval] int *result);
}

This is how I invoke the MIDL compiler:

midl Foo.idl /tlb Foo.tlb

Which writes the following output to the console:

Microsoft (R) 32b/64b MIDL Compiler Version 7.00.0555
Copyright (c) Microsoft Corporation. All rights reserved.
Processing .Foo.idl
Foo.idl
Processing C:Program Files (x86)Microsoft SDKsWindowsv7.0Aincludeunknwn.idl
unknwn.idl
Processing C:Program Files (x86)Microsoft SDKsWindowsv7.0Aincludewtypes.idl
wtypes.idl
Processing C:Program Files (x86)Microsoft SDKsWindowsv7.0Aincludeasetsd.h
basetsd.h
Processing C:Program Files (x86)Microsoft SDKsWindowsv7.0Aincludeguiddef.h
guiddef.h

The MIDL compiler produces three files: Foo.h, Foo_i.c, Foo_p.c… but no Foo.tlb. Am I misunderstanding something, or what has gone wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The .idl file requires a library{} block before it will generate a type library. Inside this block you'll need to declare the types that need to be present inside the library. A normal library only has coclass definitions in the library section, midl automatically injects any interfaces that the coclasses use.

Getting just the interface requires moving it inside the library block:

[
  uuid(34DC0E7C-37C1-41C1-B3FD-1755A0396308),
  version(1.0),
]
library MyLibrary
{
    importlib("stdole2.tlb");

    [object, uuid(400075B9-4BD6-45A5-B8B7-9DA0CF7B9B13)]
    interface IFoo : IUnknown {
        HRESULT DoFoo([in] long arg, [out, retval] long *result);
    };
};

Use your own uuid, version number and name for the library. The importlib voodoo ensures that the definitions for IUnknown and GUID don't get embedded in the type library as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...