我在我的源代碼中遇到了錯誤「C3702 atl com事件需要」,但沒有任何東西可以幫助我解決這個問題。C3702 atl com事件需要
包括這些報頭中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()也被寫入在主但沒有解決
談及這一行更改錯誤性質,但沒有解決方案: // [ 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)與您所說的嘗試不同。我會看微軟爲COM Events提供的一個小例子代碼,並從中找出它的原因,以瞭解你爲什麼不同。減少你自己的示例代碼,直到你只有事件處理程序,這將比較容易。 https://msdn.microsoft.com/en-us/library/hdcxwbd5.aspx –