15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


CRuntimeClass

Marwan Abi-Antoun -- mea03@aub.edu.lb
Wednesday, April 02, 1997

Environment: Porting from VC++ 1.5x To VC++4.0, Win95

One message in the MFC-list archive explains how to create a CRuntimeClass
object by passing the name of the class as a string. 
As a reminder, the macro RUNTIME_CLASS takes the name of the class not as a
string.

However, the provided code compiles only on VC++ 1.5x. After looking at the
source code of MFC, I was able to find the equivalent code in VC++ 4.0. It
seems to work...any better ideas ? 


IMPLEMENT_DYNAMIC( CxObject, CObject )

//////////////////// VC++1.5x ////////////////////////////

CRuntimeClass *CxObject::FindRuntimeClass( const CString &className )
{

	CRuntimeClass* pRuntimeClass = AfxGetCoreState( )->m_pFirstClass;
	while ( pRuntimeClass )
	{
		if ( className == pRuntimeClass->m_lpszClassName ) 
			return pRuntimeClass;
		pRuntimeClass = pRuntimeClass->m_pNextClass;
	}


	return NULL;
}
//////////////////// END OF VC++1.5x////////////////////////////

///////////////////////// VC++ 4.0 ////////////////////////////

CRuntimeClass *CxObject::FindRuntimeClass( const CString &className )
{
	CRuntimeClass * pRTC = NULL;
	// just walk through the simple list of registered classes
	AFX_MODULE_STATE * pCoreState = AfxGetModuleState();
	for (CRuntimeClass * pRuntimeClass = pCoreState->m_classList.GetHead(); 
		pRuntimeClass != NULL;
		pRuntimeClass = pCoreState->m_classList.GetNext(pRuntimeClass))
	{
		// class name must match
		if ( strcmp(pRuntimeClass->m_lpszClassName, className) == 0 )
		{
			pRTC = pRuntimeClass;
			break;
		}
	}
	return pRTC;
}

///////////////////////// END OF  VC++ 4.0 ////////////////////////////




Olivier PLANCHON -- Olivier.Planchon@orstom.sn
Thursday, April 03, 1997

It's rare to _really_ need to get a runtime class from a string :

if you know the name in advance, use the macro RUNTIME_CLASS( CMyObject )
if you do not know it in advance use :
CRuntimeClass* runtime_class = curent_object_in_a_while_loop->GetRuntimeClass()
then access to the string if necessary with runtime_class->m_lpszClassName

hope this help.

At 11:13 02/04/97 +0300, you wrote:
>Environment: Porting from VC++ 1.5x To VC++4.0, Win95
>
>One message in the MFC-list archive explains how to create a CRuntimeClass
>object by passing the name of the class as a string. 
>As a reminder, the macro RUNTIME_CLASS takes the name of the class not as a
>string.
>
>However, the provided code compiles only on VC++ 1.5x. After looking at the
>source code of MFC, I was able to find the equivalent code in VC++ 4.0. It
>seems to work...any better ideas ? 
>
>
>IMPLEMENT_DYNAMIC( CxObject, CObject )
>
>//////////////////// VC++1.5x ////////////////////////////
>
>CRuntimeClass *CxObject::FindRuntimeClass( const CString &className )
>{
>
>	CRuntimeClass* pRuntimeClass = AfxGetCoreState( )->m_pFirstClass;
>	while ( pRuntimeClass )
>	{
>		if ( className == pRuntimeClass->m_lpszClassName ) 
>			return pRuntimeClass;
>		pRuntimeClass = pRuntimeClass->m_pNextClass;
>	}
>
>
>	return NULL;
>}
>//////////////////// END OF VC++1.5x////////////////////////////
>
>///////////////////////// VC++ 4.0 ////////////////////////////
>
>CRuntimeClass *CxObject::FindRuntimeClass( const CString &className )
>{
>	CRuntimeClass * pRTC = NULL;
>	// just walk through the simple list of registered classes
>	AFX_MODULE_STATE * pCoreState = AfxGetModuleState();
>	for (CRuntimeClass * pRuntimeClass = pCoreState->m_classList.GetHead(); 
>		pRuntimeClass != NULL;
>		pRuntimeClass = pCoreState->m_classList.GetNext(pRuntimeClass))
>	{
>		// class name must match
>		if ( strcmp(pRuntimeClass->m_lpszClassName, className) == 0 )
>		{
>			pRTC = pRuntimeClass;
>			break;
>		}
>	}
>	return pRTC;
>}
>
>///////////////////////// END OF  VC++ 4.0 ////////////////////////////
>
>
>
---------------------------------------
Olivier PLANCHON, ORSTOM, BP 1386 Dakar
tel(221) 32 34 80    fax (221) 32 43 07
e-mail Olivier.Planchon@orstom.sn





Become an MFC-L member | Вернуться в корень Архива |