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
Name | Description |
CreateObject | Creates an object during run time. |
FromName | Creates an object during run time using the familiar class name. |
IsDerivedFrom | Determines if the class is derived from the specified class. |
CRuntimeClass Members
m_lpszClassName | The name of the class. |
m_nObjectSize | The size of the object in bytes. |
m_pBaseClass | A pointer to the CRuntimeClass structure of the base class. |
m_pfnCreateObject | A pointer to the function that dynamically creates the object. |
m_pfnGetBaseClass | Returns the CRuntimeClass structure (only available when dynamically linked). |
m_wSchem | The 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 ...
Learn on Youtube


Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions