使用和派生自另一个COM库中定义的接口

时间:2010-08-16 14:19:59

标签: com dll dependencies atl

我在Visual Studio中有两个C ++ COM项目。在ProjectA中,我在MIDL中定义了InterfaceA。在ProjectB我想定义继承自InterfaceB的{​​{1}}。如果不从InterfaceA导入IDL或H文件,可以这样做吗?

以下是代码的布局方式,这可能更清晰。库很大,所以我把东西放在单独的文件中,以便于维护。

项目A

InterfaceA.idl

ProjectA

CoclassA.idl

import "oaidl.idl";
import "ocidl.idl";

[ uuid( ... ) ]
interface InterfaceA : IDispatch { ... }

ProjectA.idl

import "InterfaceA.idl";

[ uuid( ... ) ]
interface CoclassA
{
    [default] interface InterfaceA;
};

项目B

InterfaceB.idl

import "InterfaceA.idl";

[ uuid( ... ) ]
library ProjectA
{
    interface InterfaceA;
    #include "CoclassA.idl"
}

CoclassB.idl

import "oaidl.idl";
import "ocidl.idl";

// If both interfaces were in the same project, I'd just import:
//import "InterfaceA.idl";

// Without the import I get a compilation error, obviously. I don't want to
// add ProjectA as an additional include directory because I don't want ProjectB
// to be dependent on how ProjectA's files are organized. I just want the types
// from ProjectA to be available here.

[ uuid(...) ]
interface InterfaceB: InterfaceA { ... }

ProjectB.idl

import "InterfaceB.idl";

[ uuid( ... ) ]
interface CoclassB
{
    [default] interface InterfaceB;
};

我有一种感觉,我想做的事情是不可能的。如果MIDL支持库之外的import "InterfaceB.idl"; [ uuid( ... ) ] library ProjectB { // I wish using importlib would help here, but it won't since InterfaceB is // defined and compiled by MIDL separately in InterfaceB.idl. //importlib("ProjectA.tlb"); // I could try to #include "InterfaceB.idl", but then I would lose the // automatic dependency analysis that MIDL does. I am also having trouble // getting the MIDL compiler to understand #defines. interface InterfaceB; #include "CoclassB.idl" } 等效,那么这不会成为问题!

0 个答案:

没有答案