COM 事件需要 C3702 atl

C3702 atl is required for com events

本文关键字:C3702 atl 事件 COM      更新时间:2023-10-16

我的源代码中遇到错误">com 事件需要 C3702 atl",但没有什么可以帮助我解决这个问题。

在 stafx.h 或 .h 文件中包含这些标头不起作用:

#include <comdef.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlwin.h>
#include <atltypes.h>
#include <atlctl.h>
#include <atlhost.h>

在 stdafx.h 或 .h 文件中注释和取消注释此行确实有效://使用命名空间 ATL;

在 stdafx.h 或 .h 文件中添加以下行不起作用:#define _ATL_ATTRIBUTES 1

在 MFC 中添加 ATL 支持也对我不起作用。

CoInitialize(NULL( 和 CoUninitialize((也写在 main 中,但未解决

注释此行会更改错误性质,但没有解决方案://[event_receiver(com(]此行导致编译器错误 C3731(不兼容的事件"function1"和处理程序"function2";事件源和事件处理程序必须为同一类型(

.H 文件

#define _ATL_ATTRIBUTES 1
#pragma once
#include "stdafx.h"    
[event_receiver(com)]
class CMainDlg 
{
public:
CMainDlg() {};
~CMainDlg() {};
public:
bool OnCallStart();
HRESULT AbtoPhone_OnInitialized(BSTR Msg);
void HookPhoneEvents(IAbtoPhone* pSource);
void UnHookPhoneEvents(IAbtoPhone* pSource);

};//CMainDlg

CPP 文件

#include "stdafx.h"
#include "MainDlg.h"
bool CMainDlg::OnCallStart()
{
HRESULT hr = m_AbtoPhone.CreateInstance(__uuidof(CAbtoPhone));
if (FAILED(hr))
{
AfxMessageBox(_T("Can't load CAbtoPhone component.nCheck is registered 'SIPVoIPSDK.dll'"));
}
HookPhoneEvents(m_AbtoPhone);    
return true;    
}

void CMainDlg::HookPhoneEvents(IAbtoPhone* pSource)
{    
__hook(&_IAbtoPhoneEvents::OnInitialized, pSource, &CMainDlg::AbtoPhone_OnInitialized);
}

void CMainDlg::UnHookPhoneEvents(IAbtoPhone* pSource)
{    
__unhook(pSource);    
}

HRESULT CMainDlg::AbtoPhone_OnInitialized(BSTR Msg)
{
return S_OK;
}

我正在使用Microsoft Visual Studio 2017社区版。

你解决它的方法是正确的。 必须定义_ATL_ATTRIBUTES。

但是你在 #innclude"stdafx.h"之前更改代码时做到了这一点

#define _ATL_ATTRIBUTES 1
#pragma once
#include "stdafx.h"    

只要使用预编译标头,"stdafx.h"#include 之前的所有语句都将被忽略。你必须在stdafx.h中做到这一点!(即使你写道这行不通,你也没有告诉我们为什么......