Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

May you help me please to know how I can wire this event to my method?

I have an ATL class and I need to know when a layer added to map object so that I can update my list of layers in this class.My class implements IExtension and I've put this code in Startup method of this interface:

//Some code to obtain a reference to Map object __hook(&IActiveViewEvents::ItemReordered,pMap/pointer to map/,&MyClass::MyEventHandler);

and in Shutdown method of IExtension I unhook it,but I receive compile error says the source and handler method not compatible while both signatures,arguments and names are exactly same.

Is it true a all this method?any simpler method for this wiring?or any reference on net?

Regards

share|improve this question

migrated from meta.gis.stackexchange.com Jan 12 '11 at 22:08

This question came from our discussion, support, and feature requests site for cartographers, geographers and GIS professionals.

    
I think this has been erroneously placed in Meta...a redirect by moderators would be appropriate –  Dan Patterson Jan 12 '11 at 21:52
add comment

1 Answer 1

up vote 0 down vote accepted

This solution worked for me:

1.I added Interface IActiveViewEvents to my class so it seems like this

[
    coclass,

    default(IMyCOMClass),
    threading(apartment),
    vi_progid("myprg.MyCOMClass"),
    progid("myprg.MyCOMClass.1"),
    version(1.0),
    uuid("38A4302C-3513-4B20-9A38-318FE738FDCD"),
    helpstring("MyCOMClass Class"),

    implements_category(__uuidof(CATID_MxExtension))
]
class ATL_NO_VTABLE CMyCOMClass :
    public IMyCOMClass,
    public IExtension,
    public IActiveViewEvents

  1. In Startup method of IExtension,this class is added to Map event listeners for IActiveViewEvents connection point by this code

    STDMETHOD(Startup)(VARIANT * initializationData)

    {
        if(initializationData==NULL)
            return E_POINTER;
        HRESULT hr;
        IDispatch* ipDsp;
        ipDsp=initializationData->pdispVal;
        if(FAILED(hr=ipDsp->QueryInterface(IID_IApplication,(void**)&this->m_ipApp)))
            return E_UNEXPECTED;
        IDocumentPtr ipDoc;
        //m_ipApp is member variable,keeps a pointer to ArcMap application,its type is IApplicationPtr
        if(FAILED(hr=this->m_ipApp->get_Document(&ipDoc)))
            return E_UNEXPECTED;
        //m_ipMxDoc is member variable of type IMxDocumentPtr
        this->m_ipMxDoc=(IMxDocumentPtr)ipDoc;
        IMapPtr ipMap;
        if(FAILED(hr=this->m_ipMxDoc->get_FocusMap(&ipMap)))
            return E_UNEXPECTED;
        //Hook to IActiveViewEvents connection point in Map COM Object
        //m_pComSource is member variable of type LPUNKNOWN,keeps pointer to event source
        LPUNKNOWN pComSink;
        if(FAILED(hr=ipMap->QueryInterface(IID_IUnknown,(LPVOID*)&this->m_pComSource)))
            return hr;
        if(FAILED(hr=this->QueryInterface(IID_IUnknown,(LPVOID*)&pComSink)))
            return hr;
        //m_Cookie is member variable of type DWORD,will be used when we want to disconnect this class from event source
        if(FAILED(hr=AtlAdvise(this->m_pComSource,pComSink,IID_IActiveViewEvents,&this->m_Cookie)))
        {
            this->m_Cookie=0;
            return hr;
        }
        return S_OK;
    }
    

3.Finally,the tie beetwen this class and Map object(as event source) will be removed when this class shutdowns

STDMETHOD(Shutdown)()

    {
        if(this->m_Cookie!=0)
        {
            AtlUnadvise(this->m_pComSource,IID_IActiveViewEvents,this->m_Cookie);
        }
        return  S_OK;
    }

now,my class listens to Map object to find when Map fires event methods like ItemAdded,ItemDeleted or ItemReordered.

Regards

share|improve this answer
    
What happens if you delete the dataframe (map) and add a new one, do you re-wire events to the newly added map? –  Kirk Kuykendall Jan 17 '11 at 1:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.