0
votes

I am facing ERROR "C3702 atl is required for com events" in my source code but nothing help me to resolve this one.

Including these headers in stafx.h or .h file does not work:

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

commenting and un commenting this line in stdafx.h or .h file does work: //using namespace ATL;

Adding below line in stdafx.h or .h file does not work: #define _ATL_ATTRIBUTES 1

Adding ATL support in MFC also does not work for me.

CoInitialize(NULL) and CoUninitialize() are also written in the main but not solved

Commenting this line change the error nature but no solution: //[event_receiver(com)] This line cause Compiler Error C3731 (incompatible event 'function1' and handler 'function2'; event source and event handler must be the same type)

.H File

#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 File

#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;
}

I am using Microsoft Visual Studio 2017 Community edition.

1
Your example is in an unclear state vs the list of things you've (admirably) tried. For example the _ATL_ATTRIBUTES setting in this code is a) wrong and b) different from what you said you tried. I'd look at the small example code Microsoft provides for COM Events and work back from that to see why yours is different. Cut down your own sample code until you have only the Event Handler, and that will make it easier to compare. msdn.microsoft.com/en-us/library/hdcxwbd5.aspxSteve Townsend

1 Answers

0
votes

Your way to solve it is correct. _ATL_ATTRIBUTES must be defined.

But you did it in changing code before the #innclude of "stdafx.h"

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

As long as you use precompiled header all statements before the #include "stdafx.h" are ignored. You must do it in the stdafx.h! (Even you wrote that this doesn't work, you don't told us why...)