如何调用 MIDL 编译器从 .IDL 文件生成 .TLB 文件(类型库)? [英] How do I invoke the MIDL compiler to generate a .TLB file (type library) from an .IDL file?

查看:44
本文介绍了如何调用 MIDL 编译器从 .IDL 文件生成 .TLB 文件(类型库)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为看似超级简单的事情苦苦挣扎:我想使用 MIDL 编译器从 .idl 生成类型库(.tlb 文件)文件.但是,我就是无法让 MIDL 生成 .tlb 文件.

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.

这是我的Foo.idl:

import "unknwn.idl";

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

这就是我调用 MIDL 编译器的方式:

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

MIDL 编译器生成三个文件:Foo.hFoo_i.cFoo_p.c……但没有 Foo.tlb.是我误解了什么,还是这里出了什么问题?

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?

推荐答案

.idl 文件在生成类型库之前需要一个 library{} 块.在此块中,您需要声明库中需要存在的类型.一个普通的库在 library 部分只有 coclass 定义,midl 会自动注入 coclass 使用的任何接口.

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);
    };
};

使用您自己的 uuid、版本号和库名称.importlib voodoo 确保 IUnknown 和 GUID 的定义也不会嵌入到类型库中.

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.

这篇关于如何调用 MIDL 编译器从 .IDL 文件生成 .TLB 文件(类型库)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆