How to Use AFX_MANAGE_STATE in an OLE Control

Last reviewed: October 12, 1995
Article ID: Q127074
The information in this article applies to:
  • The OLE Control Developer's Kit (CDK), versions 1.0 and 1.1

SUMMARY

The AFX_MANAGE_STATE macro is used to establish the correct module context for OLE controls and should be used for all external entry points to the control.

The existing implementation of COleControl and COlePropertyPage use AFX_MANAGE_STATE in several locations, but there are still some situations where the control writer will need to directly invoke AFX_MANAGE_STATE. This article shows you how.

MORE INFORMATION

OLE controls written with the OLE CDK use a shared MFC DLL to eliminate the need to have a separate copy of MFC in each control. In a way, an OLE control is similar to a standard MFC extension DLL. Previous versions of MFC use the _AFXDLL model to implement extension DLLs, one limitation of this model is that the host executable had to be written to also use the shared MFC DLL, that is it must be used by an MFC application. A different mechanism is needed for OLE controls because they can be used by non-MFC applications.

MFC maintains a small amount of global state information that includes (among other things) the instance handle used when loading resources. Many MFC functions rely on this state information, so it is important that it always reflect the state of the module currently executing.

The AFX_MANAGE_STATE macro creates a small local object on the stack that swaps in the control's state information in its constructor, and then restores the previous state in its destructor. A typical use of the macro looks like this:

   AFX_MANAGE_STATE(_afxModuleAddrThis);

Every exported function should invoke AFX_MANAGE_STATE as shown in the this sample code:

STDAPI DllRegisterServer(void) {

        AFX_MANAGE_STATE(_afxModuleAddrThis);

        if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
                return ResultFromScode(SELFREG_E_TYPELIB);

        if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
                return ResultFromScode(SELFREG_E_CLASS);

        return NOERROR;
}

All OLE controls implement a self-registration feature by exporting two functions, DllRegisterServer and DllUnregisterServer. These functions are called directly from outside of the control, so AFX_MANAGE_STATE must be invoked at the beginning of each function.

AFX_MANAGE_STATE should also be used when handling messages sent to a child window created by an OLE control. Such messages are sent directly to the child window, bypassing the call to AFX_MANAGE_STATE in COleControl::WindowProc().


Additional reference words: 1.51 1.00 1.52 1.10 2.00 2.10
KBCategory: kbole kbcode
KBSubcategory: CDKIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 12, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.