允许的"出"参数类型在COM自动化接口 [英] Allowed "out" parameter types in a COM automation interface
问题描述
我执行应用程序COM自动化(双接口)。自动化接口将从VBScript被调用。我不是被允许哪些类型的方法的参数相当清楚。我知道基本价值观必须适应在 VARIANT
,但这是否意味着类型的每个参数 INT
必须是穿过一个 VARIANT
,还是可以一通 INT
直接?
I'm implementing COM automation (dual interface) for an application. The automation interface will be called from VBScript. I'm not quite clear on what types are allowed for the method's arguments. I do know that basically values have to fit in a VARIANT
, but does that mean every parameter of type int
must be passed through a VARIANT
, or can one pass int
directly?
例如,两种方法我在MIDL文件是:
For example, two methods I have in my MIDL file are:
HRESULT SetDate([in] int Year, [in] int Month, [in] int Day);
HRESULT GetDate([out] int* pYear, [out] int* pMonth, [out] int* pDay);
调用的setdate
从VBScript的作品。调用 GETDATE
如图失败,这就是说,在执行,这是在C ++中,的ITypeInfo ::调用
返回code指示错误类型。
Calling SetDate
from VBScript works. Calling GetDate
as shown fails, which is to say that in the implementation, which is in C++, ITypeInfo::Invoke
returns a code indicating a type error.
我观察到,如果使用VARIANT的不是int,如下图所示,它的工作原理。
I observe that if a use VARIANT instead of int, as shown below, it works.
HRESULT GetDate([out] VARIANT* pYear, [out] VARIANT* pMonth, [out] VARIANT* pDay);
所以 INT
不允许参数(因为双界面),或者我必须做别的事情了?如果 INT
是不允许的,为什么的setdate
工作 - 有没有在这方面和输出参数之间的差异?
So is int
not allowed for parameters (because of the dual interface), or must I be doing something else wrong? If int
is not allowed, why does SetDate
work - is there a difference between in and out parameters in this respect?
此外,这副作品的方法,虽然两者使用 INT
:
Furthermore, this pair of methods works, although both use int
:
[propget] HRESULT System([out, retval] int* pSystem);
[propput] HRESULT System([in] int System);
怎么来 - 是一个属性不同的允许的参数类型的规则,或当参数被声明为 RETVAL
我不能完全理解它的所有 - 将不胜感激,如果有人能澄清这
I can't quite make sense of it all - would be grateful if anyone can clarify this.
推荐答案
显然,当你调用通过的IDispatch
接口,所有参数都始终以通过 VARIANT
秒。然而,你可能实现使用其他类型。缺口如何弥合?
Obviously, when you call the interface via IDispatch
, all the parameters are always passed as VARIANT
s. Yet, your implementation potentially uses other types. How is the gap bridged?
ATL(假设这是你所使用的)将实施调用
为您服务,与code,从转换参数VARIANT
S到呼叫转移到实际的方法之前使用您的方法签名的正确类型。
ATL (assuming that's what you're using) will implement Invoke
for you, with code that converts arguments from VARIANT
s to the proper types used by your method signature before forwarding the call to the actual method.
下面就是这些规则:
-
[中]
参数可以是几乎任何类型的,适合在VARIANT
,你发现。 ATL(或任何库您正在使用)会照顾你翻译的参数。
[in]
parameters can be just about any type that fits in aVARIANT
, as you discovered. ATL (or whatever library you're using) will take care of translating parameters for you.
[IN,OUT]
参数必须是 VARIANT *
。如果你使用别的,无论是通话将无法正常工作或返回值将会丢失(我不记得它去哪个方向,你表明你有这个情况运行时错误)。当你的方法返回时,ATL将参数转换成相应的 VARIANT
这样的VBScript(或任何的IDispatch
客户端进行的调用)可以得到产值的保持。
[in, out]
parameters must be VARIANT*
. If you use anything else, either the call won't work or the return value will be lost (I don't remember which way it goes; you indicated you had a run-time error for this situation). Upon your method returning, ATL will convert the argument into an appropriate VARIANT
so that VBScript (or whatever IDispatch
client made the call) can get a hold of the output value.
[RETVAL,走出]
是一个特例。您可以使用一个指向您选择的任何类型,ATL将照顾它。我presume什么使这成为可能的是,返回值是 DISPPARAMS
机制之外提供了回去。
[retval, out]
are a special case. You can use a pointer to whatever type you choose, and ATL will take care of it. I presume that what makes this possible is that the return value is provided back outside of the DISPPARAMS
mechanism.
[出] </ code> ...只是不。他们没有工作 - VBScript中不能使用
[出] </ code>参数正常。你要知道,他们将工作,该方法将执行没有错误,但VBScript中不能
区分[出] </ code>和
[IN,OUT]
,这意味着VBScript中预计的方法来释放任何价值的参数,当你收到。如果你使用 [出] </ code>,无论客户端code制作方法调用之前置于参数将被永久泄露。
[out]
... just don't. They don't work - VBScript cannot use [out]
parameters correctly. Mind you, they will "work" in that the method will execute without errors, but VBScript cannot distinguish between [out]
and [in, out]
, which means that VBScript expects your method to release whatever value was on the parameter when you received it. If you use [out]
, whatever the client code placed on the parameter before making the method call will be leaked permanently.
这篇关于允许的&QUOT;出&QUOT;参数类型在COM自动化接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!