编译器错误 C3706

“function”:激发 COM 事件必须是 COM 接口

用于触发 COM 事件的事件接口必须是 COM 接口。 在这种情况下,应使用 Visual C++ 属性定义接口,或使用具有 #import embedded_idl 属性的类型库中的 #import 导入。

请注意,使用 COM 事件需要以下示例中显示的 ATL 头文件的 #include 行。 若要修复此错误,请将以下属性之一应用于接口定义:对象接口或 dispinterface,使IEvents(事件接口)成为 COM 接口。

如果接口来自 MIDL 生成的头文件,编译器将无法将其识别为 COM 接口。

以下示例生成 C3706:

// C3706.cpp
// compile with: /c
// C3706 expected
#define _ATL_ATTRIBUTES 1
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>

[module(dll, name="idid", uuid="12341234-1234-1234-1234-123412341234")];

// Uncomment the following line to resolve.
// [object, uuid="12341234-1234-1234-1234-123412341237"]
__interface IEvents : IUnknown {
   HRESULT event1(/*[in]*/ int i);   // uncomment [in]
};

[dual, uuid="12341234-1234-1234-1234-123412341235"]
__interface IBase {
   HRESULT fireEvents();
};

[coclass, event_source(com), uuid="12341234-1234-1234-1234-123412341236"]
class CEventSrc : public IBase {
   public:
   __event __interface IEvents;

   HRESULT fireEvents() {
      HRESULT hr = IEvents_event1(123);
      return hr;
   }
};