我使用ATL简单对象向导在C++中使用ATL构建了一个COM服务器DLL。我遵循微软的ATLDLLCOMServer例子。除了一件事情之外,一切都很好:我没有在VBScript中收到COM事件。我确实收到了C#中的事件。我在早期的基于MFC的实现中使用VBScript中的事件作为ActiveX控件。将COM事件暴露给VBScript(ATL)
我的控制是这样定义的:
class ATL_NO_VTABLE CSetACLCOMServer :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CSetACLCOMServer, &CLSID_SetACLCOMServer>,
public IConnectionPointContainerImpl<CSetACLCOMServer>,
public CProxy_ISetACLCOMServerEvents<CSetACLCOMServer>,
public IDispatchImpl<ISetACLCOMServer, &IID_ISetACLCOMServer, &LIBID_SetACLCOMLibrary, /*wMajor =*/ 1, /*wMinor =*/ 0>,
public IProvideClassInfo2Impl<&CLSID_SetACLCOMServer, &DIID__ISetACLCOMServerEvents, &LIBID_SetACLCOMLibrary> /* Required for event support in VBS */
{
public:
BEGIN_COM_MAP(CSetACLCOMServer)
COM_INTERFACE_ENTRY(ISetACLCOMServer)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IConnectionPointContainer)
COM_INTERFACE_ENTRY(IProvideClassInfo)
COM_INTERFACE_ENTRY(IProvideClassInfo2)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(CSetACLCOMServer)
CONNECTION_POINT_ENTRY(__uuidof(_ISetACLCOMServerEvents))
END_CONNECTION_POINT_MAP()
[...]
在VBScript中我使用COM对象是这样的:
set objSetACL = WScript.CreateObject("SetACL.SetACL", "SetACL_")
' Catch and print messages from the SetACL COM server which are passed (fired) as events.
' The name postfix of this function (MessageEvent) must be identical to the event name
' as defined by SetACL.
' The prefix (SetACL_) can be set freely in the call to WScript.CreateObject
sub SetACL_MessageEvent (Message)
WScript.Echo Message
end sub
的IDL的相关部分是这样的:
[
object,
uuid(E1B57CA5-FD7F-4304-BC0A-8BEBDE231D53),
dual,
nonextensible,
pointer_default(unique),
helpstring("SetACL COM Interface")
]
interface ISetACLCOMServer : IDispatch
{
};
[
uuid(00D4DCD3-02B9-4A71-AB61-2283504620C8),
version(1.0),
helpstring ("SetACL Type Library")
]
library SetACLCOMLibrary
{
importlib("stdole2.tlb");
[
uuid(35F76182-7F52-4D6A-BD6E-1317345F98FB),
helpstring ("SetACL Event Interface")
]
dispinterface _ISetACLCOMServerEvents
{
properties:
methods:
[id(1), helpstring("Receives string messages that would appear on the screen in the command line version")]
void MessageEvent([in] BSTR message);
};
[
uuid(13379563-8F21-4579-8AC7-CBCD488735DB),
helpstring ("SetACL COM Server"),
]
coclass SetACLCOMServer
{
[default] interface ISetACLCOMServer;
[default, source] dispinterface _ISetACLCOMServerEvents;
};
};
问题:SetACL_MessageEvent永远不会获得calle d。
我曾尝试:
- 阅读this KB article后,我加入IProvideClassInfo2的实施,但这并没有帮助。
- 这个FAQ提到输出接口不应该定义为双接口。我从我的界面定义中删除了“双重”,但这没有帮助。
- 我发现this SO question似乎描述了一个类似的问题。答案从来没有给出,只有一个解决方法。
更新1:
我现在知道它是失败的,但不是原因:在_ISetACLCOMServerEvents_CP.h火灾事件方法进行的调用失败,E_UNEXPECTED。这是该行:
hr = pConnection->Invoke(1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶ms, NULL, NULL, NULL);
解决方案:
问题是,我所谓的事件从COM对象的背景线程发射功能,从错误的公寓等字样。
难道你不需要在vbscript/vba中使用'WithEvents'来响应COM事件吗? – 2012-07-29 22:13:05
不,根据http://msdn.microsoft.com/en-us/library/ms974564.aspx你没有。 – 2012-07-30 08:44:27
我想你在'IProvideClassInfo2Impl'中丢失了第二个参数。我应该是事件接口'IID'。 – 2012-07-30 09:14:55