ActiveX Events and MFC State

来源:互联网 发布:java实现oracle jdbc 编辑:程序博客网 时间:2024/05/22 07:08

http://www.codeproject.com/Articles/7128/ActiveX-Events-and-MFC-State




ATL ActiveX Event Sink

This approach involves creating an IDispEventSimpleImpl derived class. Once again, methods must be added to match the event source.

Unlike the MFC approach, in this case you are responsible for managing the MFC state yourself. To do this, you should make use of the AFX_MANAGE_STATE macro discussed earlier. In this case however, since the event sink object is instantiated in the main application, use the AfxGetAppModuleState() function to supply the pointer to theAFX_MODULE_STATE structure.

For example, a typical event sink function for an object constructed in the main application should start with the following code.

//// Make sure the MFC state is correct.//         AFX_MANAGE_STATE(AfxGetAppModuleState())    ////       The rest of your code...//

As mentioned earlier, when this function returns, the MFC state will be automatically restored.

The demo program illustrates the use of this technique in the CAtlEventSink class.


Note: there are some exceptions to the state management rules which make this topic even more confusing. Regular dlls that statically link to MFC and MFC extension dll's should not use AFX_MANAGE_STATE. See the MSDN article "Regular DLLs Dynamically Linked to MFC" for further details.