Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A question about CRuntimeClass and Dynamic Creation

A question about CRuntimeClass and Dynamic Creation

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SmilingHeart
    wrote on last edited by
    #1

    I want to implement a funtion.The follwing is its prototype: CRuntimeClass* FindClass(CString &strClassName) Parameter strClassName is the name of some class. I want to get the CRuntimeClass member variable of the class whose name is strClassName. For example : CString strClassName="CMyView"; CRuntimeClass * pRtc=FineClass(strClassName); ~~~~~~~~ I hope that I can get the pointer of CRuntimeClass member variable of CMyView; Thanks for your help I love sea

    A I 2 Replies Last reply
    0
    • S SmilingHeart

      I want to implement a funtion.The follwing is its prototype: CRuntimeClass* FindClass(CString &strClassName) Parameter strClassName is the name of some class. I want to get the CRuntimeClass member variable of the class whose name is strClassName. For example : CString strClassName="CMyView"; CRuntimeClass * pRtc=FineClass(strClassName); ~~~~~~~~ I hope that I can get the pointer of CRuntimeClass member variable of CMyView; Thanks for your help I love sea

      A Offline
      A Offline
      Andrew Quinn AUS
      wrote on last edited by
      #2

      Hi, My first thought would be to add a CMapStringToPtr object into your CDocument class Then, on the constructor of each and every object you'll need, add a call to the object, e.g.

      CMyView::CMyView() : CFormView(CMyView::IDD)
      {
      //{{AFX_DATA_INIT(CMyView)
      //}}AFX_DATA_INIT

      CMyDoc* pDoc = GetDocument();
      if (pDoc) pDoc->RegisterClass(GetRuntimeClass()->m_lpszClassName, GetRuntimeClass());
      }

      We then need to remember to add an 'unregister' to the destructor...MOST important if you have any objects that will get destroyed/recreated during the lifetime of the application - otherwise the pointer held in the map will point to junk memory

      CMyView::~CMyView()
      {
      CMyDoc* pDoc = GetDocument();
      if (pDoc) pDoc->UnregisterClass(GetRuntimeClass()->m_lpszClassName);
      }

      Here are the functions in the Document...

      BOOL CMyDoc::RegisterClass(const CString& strClassName, const CRuntimeClass* pClass)
      {
      CRuntimeClass* pLookup = NULL;
      if (m_mapRuntimeClasses.Lookup(strClassName, (void*&)pLookup))
      UnregisterClass(strClassName); // Remove existing entry ???

      m\_mapRuntimeClasses.SetAt(strClassName, ( void\*& )pClass);
      return TRUE;
      

      }

      BOOL CMyDoc::UnregisterClass(const CString& strClassName)
      {
      BOOL bRet = FALSE;

      CRuntimeClass\* pLookup = NULL;
      if (m\_mapRuntimeClasses.Lookup(strClassName, (void\*&)pLookup))
      	bRet = m\_mapRuntimeClasses.RemoveKey(strClassName);
      
      return bRet;
      

      }

      The m_mapRuntimeClasses is the CMapStringToPtr object - if you wanted to be all OO about it, then best to encapsulate the functions you want into a derived CMapStringToPtr object and have this in your CDocument. For any objects that need access to the document (other than views that already do), then preferred approach is:

      CFrameWnd\* pFrame = (CFrameWnd\*)AfxGetApp()->m\_pMainWnd;   
      if (pFrame && pFrame->GetSafeHwnd())   
      {      
      	CMyDoc\* pDoc = (CMyDoc\*)pFrame->GetActiveDocument();   
      	if (pDoc) pDoc->Register(.....);
      }
      

      Hope this helps, Andy

      S 1 Reply Last reply
      0
      • S SmilingHeart

        I want to implement a funtion.The follwing is its prototype: CRuntimeClass* FindClass(CString &strClassName) Parameter strClassName is the name of some class. I want to get the CRuntimeClass member variable of the class whose name is strClassName. For example : CString strClassName="CMyView"; CRuntimeClass * pRtc=FineClass(strClassName); ~~~~~~~~ I hope that I can get the pointer of CRuntimeClass member variable of CMyView; Thanks for your help I love sea

        I Offline
        I Offline
        igor1960
        wrote on last edited by
        #3

        Your ModuleState has a public member m_classList that you can enumerate through to find what you want... // search app specific classes AFX_MODULE_STATE* pModuleState = AfxGetModuleState(); AfxLockGlobals(CRIT_RUNTIMECLASSLIST); for (pClass = pModuleState->m_classList; pClass != NULL; pClass = pClass->m_pNextClass) { if (lstrcmpA(szClassName, pClass->m_lpszClassName) == 0) { AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST); return pClass; } } AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST); Regards "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

        S 1 Reply Last reply
        0
        • I igor1960

          Your ModuleState has a public member m_classList that you can enumerate through to find what you want... // search app specific classes AFX_MODULE_STATE* pModuleState = AfxGetModuleState(); AfxLockGlobals(CRIT_RUNTIMECLASSLIST); for (pClass = pModuleState->m_classList; pClass != NULL; pClass = pClass->m_pNextClass) { if (lstrcmpA(szClassName, pClass->m_lpszClassName) == 0) { AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST); return pClass; } } AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST); Regards "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

          S Offline
          S Offline
          SmilingHeart
          wrote on last edited by
          #4

          I have tried your code. But it does not work. "pModuleState->m_classList" is always eaqual to 0x0000. Can you tell me the reason? I love sea

          I 1 Reply Last reply
          0
          • A Andrew Quinn AUS

            Hi, My first thought would be to add a CMapStringToPtr object into your CDocument class Then, on the constructor of each and every object you'll need, add a call to the object, e.g.

            CMyView::CMyView() : CFormView(CMyView::IDD)
            {
            //{{AFX_DATA_INIT(CMyView)
            //}}AFX_DATA_INIT

            CMyDoc* pDoc = GetDocument();
            if (pDoc) pDoc->RegisterClass(GetRuntimeClass()->m_lpszClassName, GetRuntimeClass());
            }

            We then need to remember to add an 'unregister' to the destructor...MOST important if you have any objects that will get destroyed/recreated during the lifetime of the application - otherwise the pointer held in the map will point to junk memory

            CMyView::~CMyView()
            {
            CMyDoc* pDoc = GetDocument();
            if (pDoc) pDoc->UnregisterClass(GetRuntimeClass()->m_lpszClassName);
            }

            Here are the functions in the Document...

            BOOL CMyDoc::RegisterClass(const CString& strClassName, const CRuntimeClass* pClass)
            {
            CRuntimeClass* pLookup = NULL;
            if (m_mapRuntimeClasses.Lookup(strClassName, (void*&)pLookup))
            UnregisterClass(strClassName); // Remove existing entry ???

            m\_mapRuntimeClasses.SetAt(strClassName, ( void\*& )pClass);
            return TRUE;
            

            }

            BOOL CMyDoc::UnregisterClass(const CString& strClassName)
            {
            BOOL bRet = FALSE;

            CRuntimeClass\* pLookup = NULL;
            if (m\_mapRuntimeClasses.Lookup(strClassName, (void\*&)pLookup))
            	bRet = m\_mapRuntimeClasses.RemoveKey(strClassName);
            
            return bRet;
            

            }

            The m_mapRuntimeClasses is the CMapStringToPtr object - if you wanted to be all OO about it, then best to encapsulate the functions you want into a derived CMapStringToPtr object and have this in your CDocument. For any objects that need access to the document (other than views that already do), then preferred approach is:

            CFrameWnd\* pFrame = (CFrameWnd\*)AfxGetApp()->m\_pMainWnd;   
            if (pFrame && pFrame->GetSafeHwnd())   
            {      
            	CMyDoc\* pDoc = (CMyDoc\*)pFrame->GetActiveDocument();   
            	if (pDoc) pDoc->Register(.....);
            }
            

            Hope this helps, Andy

            S Offline
            S Offline
            SmilingHeart
            wrote on last edited by
            #5

            Thanks a lot. I love sea

            1 Reply Last reply
            0
            • S SmilingHeart

              I have tried your code. But it does not work. "pModuleState->m_classList" is always eaqual to 0x0000. Can you tell me the reason? I love sea

              I Offline
              I Offline
              igor1960
              wrote on last edited by
              #6

              The only reason it may not work is because you are using MFC in shared DLLs -- check _AFXDLL preprocessor definition and if it's defined you may use the following code: // search app specific classes AFX_MODULE_STATE* pModuleState = AfxGetModuleState(); AfxLockGlobals(CRIT_RUNTIMECLASSLIST); for (pClass = pModuleState->m_classList; pClass != NULL; pClass = pClass->m_pNextClass) { if (lstrcmpA(szClassName, pClass->m_lpszClassName) == 0) { AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST); return pClass; } } AfxUnlockGlobals(CRIT_RUNTIMECLASSLIST); #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(szClassName, pClass->m_lpszClassName) == 0) { AfxUnlockGlobals(CRIT_DYNLINKLIST); return pClass; } } } AfxUnlockGlobals(CRIT_DYNLINKLIST); #endif For an example of usage check code for CRuntimeClass* PASCAL CRuntimeClass::Load(CArchive& ar, UINT* pwSchemaNum) in MFC ArcCore.cpp file -- above example is taken from it -- it's used to load class during serialization... It just have to work, otherwise serialization cannot be achieved... Regards, Igor "...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..." Me

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups