我使用Visual Studio 2015,并且我有一个定义COM接口并在编译时生成.tlb文件的C#应用程序项目。现在我想将该Csharp.tlb导入到idl中。在idl中使用importlib时自动生成.tlh中的编译器错误C3646'未知重写说明符'
MyLibrary.idl:
import "oaidl.idl";
import "ocidl.idl";
import "Cplusplus.idl";
library MyLibrary
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
importlib("Csharp.tlb");
interface IMyCOM : IDispatch
{
[propget, id(1)]
HRESULT CpluplusObject
(
[out,retval] ICplusplusObject** cplusplusObject
);
[propget, id(2)]
HRESULT CsharpObject
(
[out, retval] ICsharpObject** csharpObject
);
}
coclass MyCOM
{
[default] interface IMyCOM;
};
}
在编译过程中,我得到
C3646 'csharpObject' 的错误:在MyLibrary.tlh未知重写说明符 MyLibrary.tlh被产生
汽车汇编,看起来如下
MyLibrary.tlh:
#pragma once
#pragma pack(push, 8)
#include <comdef.h>
namespace MyLibrary {
struct __declspec(uuid("8e664998-bc93-48e7-adcc-84fc8598cd5d"))
/* dual interface */ ICplusplusObject;
_COM_SMARTPTR_TYPEDEF(ICplusplusObject, __uuidof(ICplusplusObject));
struct __declspec(uuid("388ebf11-05c8-4b86-b2bd-60f0ef38695e"))
IMyLibrary : IDispatch
{
__declspec(property(get=GetCplusplusObject))
ICplusplusObjectPtr cplusplusObject;
__declspec(property(get=GetCsharpObject))
ICsharpObjectPtr csharpObject;
ICplusplusObjectPtr GetCplusplusObject ();
ICsharpObjectPtr GetCsharpObject ();
virtual HRESULT __stdcall get_CplusplusObject (
/*[out,retval]*/ struct ICplusplusObject * * cplusplusObject) = 0;
virtual HRESULT __stdcall get_CsharpObject (
/*[out,retval]*/ struct ICsharpObject * * csharpObject) = 0;
}
__declspec(implementation_key(1)) ICplusplusObjectPtr IMyLibrary::GetcplusplusObject ();
__declspec(implementation_key(2)) ICsharpObjectPtr IMyLibrary::GetcsharpObject ();
}
该错误表示ICsharpObjectPtr或ICsharpObject分别是不知道的,这是我目前所了解的。 ICplusplusObjectPtr是已知的,因为import“ICplusplus.idl”将定义添加到.tlh和importlib(“ICsharp.tlb”);没有明显。
出于测试原因,我使用OLE/COM Object Viewer从.tlb生成了ICsharp.idl,并导入了该idl。之后,错误消失了。
但是为什么.tlb的importlib不能直接工作?我不想每次出.tlb时生成一个idl文件。
我认为有一个#include“ICsharp.tlh”缺失或者使类型为.tlh所知。但如何告诉idl或编译器正确引用ICsharpObject?
非常感谢您的帮助。
您正在'library'块的中间定义方法。这没有任何意义。方法自然必须是接口的一部分。 –
你想用你的'#import'指令使用['auto_search'](https://msdn.microsoft.com/en-us/library/0346ezww.aspx)属性。否则显式地''先导入Csharp.tlb。 –
我对MyLibrary.idl代码进行了更正。我只是忘了写下你已经注意到的界面。谢谢。 – MaLe