我面临着实现 IDispatch
界面。有四种方法,幸运的是其中 3 个很简单:
function TIEEventsSink.GetTypeInfoCount(...): HResult;
{
Result := E_NOTIMPL;
}
function TIEEventsSink.GetTypeInfo(...): HResult;
{
Result := E_NOTIMPL;
}
function TIEEventsSink.GetIDsOfNames(...): HResult;
{
Result := E_NOTIMPL;
}
这是最后一个方法,Invoke
比较困难。在这里,我面临着必须实际处理 DispID 的情况,并调用我适当的方法;从变体数组中解码参数。
function Invoke(
dispIdMember: DISPID;
riid: REFIID;
lcid: LCID;
wFlags: WORD;
var pDispParams: DISPPARAMS;
var pVarResult: VARIANT;
var pExcepInfo: EXCEPINFO;
var puArgErr: DWORD
): HRESULT;
不想编写所有繁琐的样板代码,我确信会有错误,所以我去谷歌搜索 - 而不是做任何工作。
我在 the MSDN Documentation of IDispatch.Invoke
上找到了这个片段:
Generally, you should not implement Invoke directly.
太棒了!反正我不想实现它!继续阅读:
Instead, use the dispatch interface to create functions CreateStdDispatch and DispInvoke. For details, refer to CreateStdDispatch, DispInvoke, Creating the IDispatch Interface and Exposing ActiveX Objects.
Creating the IDispatch Interface链接说:
You can implement IDispatch by any of the following means:
- [snip]
- Calling the CreateStdDispatch function. This approach is the simplest, but it does not provide for rich error handling or multiple national languages.
- [snip]
非常好,CreateStdDispatch它是:
Creates a standard implementation of the IDispatch interface through a single function call. This simplifies exposing objects through Automation.
HRESULT CreateStdDispatch( IUnknown FAR* punkOuter, void FAR* pvThis, ITypeInfo FAR* ptinfo, IUnknown FAR* FAR* ppunkStdDisp );
我打算将其称为:
CreateStdDispatch(
myUnk, //Pointer to the object's IUnknown implementation.
anotherObject, //Pointer to the object to expose.
nil //Pointer to the type information that describes the exposed object (i has no type info)
dispInterface //the IUnknown of the object that implements IDispatch for me
);
我不明白的是CreateStdDispatch
的Windows API实现如何知道在我的对象上调用什么方法 - 特别是因为CreateStdDispatch
不知道什么是面向对象的我正在使用的语言或其调用约定。
CreateStdDispatch
如何知道
- 针对给定的
dispid
调用什么方法? - 我的语言的调用约定?
- 如何处理我的面向对象对象所使用的语言的异常?
注意:我别无选择,只能实现一个dispinterface
;我没有定义the interface 。我希望它是一个简单的早期绑定(bind) IUnknown
,但事实并非如此。
最佳答案
传递给 CreateStdDispatch
的 ITypeInfo
参数是否公开了所有方法信息?
因此,您需要首先调用 CreateDispTypeInfo
创建类型信息,并将其传递给 CreateStdDispatch
,然后后者可以使用类型信息来确定自 起调用哪个方法>CreateDispTypeInfo
需要包含所有这些信息的 INTERFACEDATA
我可能错了,因为我没有时间去研究它,但这对我来说是有意义的。 我稍后会对此进行调查并更新答案。
关于winapi - CreateStdDispatch 如何知道要调用哪个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6508580/