Run Time Class Handler: Instantiating Classes by Name

Environment: Win16 (VC++ 1.52) and Win9x/NT (VC++ 6.0)

With this class I was able to simply add a file with a new class to my project and to instantiate new objects from this class simply by altering or adding the name of the class in the database or in configuration files.  This way I didn't need to add code for the support of the added class to existing code or to register the class with class factories.

So this is VERY useful if you like to disconnect classes from each other on compile level or to avoid making special class factories.

This class is tested and is in use for both 16/32 bit versions.

Notes:

  1. Use DECLARE_SERIAL and the IMPLEMENT_SERIAL macro for classes which should be used for this function.

  2. As with all classes which use DECLARE_SERIAL a default empty constructor is needed.  If you need initialization, a "Create" function be made and used after a cast of the returned object to the appropriate class.

  3. DECLARE_DYNCREATE does not work with the 32 bit part of this code, perhaps somebody knows why.

  4. As far as I know, this way of coding is part of SmallTalk.

Downloads

Download source - 2 Kb


Comments

  • Solution to make it work with classes in MFC extension DLLs

    Posted by Legacy on 06/03/1999 12:00am

    Originally posted by: Peter Luxem

    I used this code to (Copy and Paste out of MFC source), but I've copied everything, so it finds classes in
    MFC extension DLL's too (even if you load them explicitly).
    
    

    CRuntimeClass *FindRuntimeClass( const char *szClass )
    {
    #if _MSC_VER != 1200
    #pragma message( "Check if this code is still correct" )
    #endif
    bool bFound=false;

    //>> Undocumented MFC, can break in next version, copy/paste from arccore.cpp line 315
    void AFXAPI AfxLockGlobals(int nLockType);
    void AFXAPI AfxUnlockGlobals(int nLockType);
    #define CRIT_RUNTIMECLASSLIST 0
    #define CRIT_DYNLINKLIST 0

    // Alleen voor Declare_serials
    AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
    AfxLockGlobals(CRIT_RUNTIMECLASSLIST);
    for ( CRuntimeClass *pClass = pModuleState->m_classList; pClass != NULL;
    pClass = pClass->m_pNextClass)
    {
    if ( lstrcmpA( szClass, pClass->m_lpszClassName) == 0 )
    {
    bFound = true;
    break;
    }
    }
    AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST);

    if ( !bFound )
    {
    // This code is to check for the class in loaded DLL's
    #ifdef _AFXDLL
    // search classes in shared DLLs
    AfxLockGlobals(CRIT_DYNLINKLIST);
    for (CDynLinkLibrary* pDLL = pModuleState->m_libraryList; pDLL != NULL;
    pDLL = pDLL->m_pNextDLL)
    {
    for (pClass = pDLL->m_classList; pClass != NULL;
    pClass = pClass->m_pNextClass)
    {
    if (lstrcmpA(szClass, pClass->m_lpszClassName) == 0)
    {
    bFound = true;
    break;
    }
    }

    if ( bFound )
    break;
    }
    AfxUnlockGlobals(CRIT_DYNLINKLIST);
    }
    #endif
    //<< Undocumented MFC

    return pClass;
    }

    Reply
  • Reason DECLARE_SERIAL is needed

    Posted by Legacy on 06/02/1999 12:00am

    Originally posted by: ChrisD

    The list you are travering to find the class name is only created for objects that have DECLARE/IMPLEMENT _SERIAL defined. The IMPLEMENT_SERIAL definition has a line that reads
    static const AFX_CLASSINIT _init_##class_name(RUNTIME_CLASS(class_name));

    this line instantiates a static AFX_CLASSINIT object for the class suppllied which add it to the list of RuntimeClass names.

    Unless you are supllying strings at runtime you can use in your code
    COBject* pBoj = RUNTIME_CLASS(CreatableClass)->CreateObject();
    Chris

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds