CRuntimeClass Structure

CRuntimeClass is a structure to hold the runtime type, name, parent class and methods to indentify class information and dynamic creation function. MFC base class CObject holds CRuntimeClass and thus each class derived from CObject is associated with a CRuntimeClass structure that you can use to obtain information about an object or its base class at run time. Here are CRuntimeClass methods and member variables.

CRuntimeClass Methods

NameDescription
CreateObjectCreates an object during run time.
FromNameCreates an object during run time using the familiar class name.
IsDerivedFromDetermines if the class is derived from the specified class.

CRuntimeClass Members

m_lpszClassNameThe name of the class.
m_nObjectSizeThe size of the object in bytes.
m_pBaseClassA pointer to the CRuntimeClass structure of the base class.
m_pfnCreateObjectA pointer to the function that dynamically creates the object.
m_pfnGetBaseClassReturns the CRuntimeClass structure (only available when dynamically linked).
m_wSchemThe schema number of the class.

CreateObject/m_pfnCreateObject

class CMyClass : public CWnd
{
  DECLARE_DYNCREATE(CMyClass);
};
IMPLEMENT_DYNCREATE(CMyClass,CWnd)

int main() 
{
  CRuntimeClass* pClass = RUNTIME_CLASS(CMyClass);
  CObject* pObject = pClass->m_pfnCreateObject();
  or
  CObject* pObject = pClass->CreateObject();
}

CRuntimeClass::FromName

CRuntimeClass* pClass = CRuntimeClass::FromName(_T("CMyClass"));
if (pClass == NULL)
{
 // not found, display a warning for diagnostic purposes
 AfxMessageBox(_T("Warning: CMyClass not defined"));
 return NULL;
}

CRuntimeClass::IsDerivedFrom

// We only want to create an object derived from CWnd.
if (!pClass->IsDerivedFrom(RUNTIME_CLASS(CWnd)))
{
 TRACE(_T("Error; Object %s is not derived from CWnd\n"),
	pClass->m_lpszClassName);
 return FALSE;
}

ClassName, BaseClass, size, schema

// Get a pointer to the base class CRuntimeClass.
#ifdef _AFXDLL
 CRuntimeClass* pBaseClass = pClass->m_pfnGetBaseClass();
#else
 CRuntimeClass* pBaseClass = pClass->m_pBaseClass;
#endif
ASSERT(pBaseClass != NULL);

TRACE("Creating object %s derived from %s, with object size %d "
 "and schema %d\n", pClass->m_lpszClassName, 
 pBaseClass->m_lpszClassName, pClass->m_nObjectSize, 
 pClass->m_wSchema);

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#