简体   繁体   English

COM接口中VARIANT的专有使用

[英]The exclusive use of VARIANT in a COM interface

I've inherited a project that includes a COM DLL. 我继承了一个包含COM DLL的项目。 I'm sort of new to COM but something doesn't seem right. 我对COM有点陌生,但似乎有些不对劲。 The interface defined in the IDL only uses the VARIANT type for all properties and method returns/parameters. IDL中定义的接口仅对所有属性和方法返回/参数使用VARIANT类型。 Is there any possible justification for this? 有什么可能的理由吗? I have a feeling the previous developer was just winging some things, but I want to be sure. 我有一种感觉,以前的开发人员只是在做一些事情,但是我想确定。

Here's what my IDL looks like: 这是我的IDL的样子:

interface IMyComInterface : IDispatch
{
    [id(1), helpstring("method CheckMessage")] HRESULT CheckMessage([in] VARIANT vMsg);
    [id(2), helpstring("method CheckFolder")] HRESULT CheckFolder([in] VARIANT Folder, [out] VARIANT *pCount, [out, retval] VARIANT *pErrorCount);
    [propget, id(3), helpstring("property Flags")] HRESULT Flags([out, retval] VARIANT *pVal);
    [propput, id(3), helpstring("property Flags")] HRESULT Flags([in] VARIANT newVal);
    [propget, id(4), helpstring("property MessageStore")] HRESULT MessageStore([out, retval] VARIANT *pVal);
    [propput, id(4), helpstring("property MessageStore")] HRESULT MessageStore([in] VARIANT newVal);
    [propget, id(5), helpstring("property Directory")] HRESULT Directory([out, retval] VARIANT *pVal);
    [propput, id(5), helpstring("property Directory")] HRESULT Directory([in] VARIANT newVal);
    [propget, id(6), helpstring("property MessageCount")] HRESULT MessageCount([out, retval] VARIANT *pVal);
};

Much thanks. 非常感谢。

EDIT: 编辑:

To make things clear, all of these VARIANT s could be replaced by explicit types. 为了清楚VARIANT ,所有这些VARIANT都可以由显式类型替换。

If you need performance or simplification, you change VARIANT type for appropriate values, more closely to type used. 如果需要性能或简化,可以将VARIANT类型更改为适当的值,更接近于所使用的类型。 Remember, it is an interface break. 记住,这是一个接口中断。
View valid values to use : 查看要使用的有效值:
COM Data Types COM数据类型

Your interface derives from IDispatch which means it's a dual interface - you can call the methods directly from the interface, or you can call them through iDispatch::Invoke . 您的接口是从IDispatch派生的,这意味着它是一个双重接口-您可以直接从接口调用方法,也可以通过iDispatch::Invoke调用它们。 The parameters passed to Invoke must all be of type VARIANTARG which is just another name for VARIANT , see http://msdn.microsoft.com/en-us/library/ms891678.aspx . 传递给Invoke的参数必须全部为VARIANTARG类型,这只是VARIANT另一个名称,请参见http://msdn.microsoft.com/zh-cn/library/ms891678.aspx

For certain types of dual interfaces that suppose to work with VB and scripting languages it is better to use VARIANT that direct type. 对于某些假定要与VB和脚本语言一起使用的双接口类型,最好使用直接类型的VARIANT For example it is better to use VARIANT over almost all kind of SAFEARRAY and interface pointers. 例如,最好对几乎所有种类的SAFEARRAY和接口指针使用VARIANT But you may test and see. 但是您可能会测试并看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM