【问题标题】:C3702 atl is required for com eventscom 事件需要 C3702 atl
【发布时间】:2018-01-05 15:36:06
【问题描述】:

我在源代码中面临错误“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里但是没解决 p>

评论此行会改变错误性质但没有解决方案: //[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 设置是 a) 错误和 b) 与您所说的尝试不同。我会查看 Microsoft 为 COM 事件提供的小示例代码,然后再从那里返回以了解您的不同之处。减少您自己的示例代码,直到您只有事件处理程序,这样比较容易。 msdn.microsoft.com/en-us/library/hdcxwbd5.aspx

标签: c++ events mfc com atl


【解决方案1】:

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

但是您在“stdafx.h”的#innclude 之前更改代码时这样做了

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

只要您使用预编译头文件,#include "stdafx.h" 之前的所有语句都会被忽略。您必须在 stdafx.h 中执行此操作! (即使你写了这行不通,你也没有告诉我们为什么......)

【讨论】:

    猜你喜欢
    • 2012-02-28
    • 2012-07-27
    • 2016-04-09
    • 2010-12-16
    • 2013-06-21
    • 2011-04-28
    • 1970-01-01
    • 2013-09-08
    • 2016-12-19
    相关资源
    最近更新 更多