Click here to Skip to main content
15,798,826 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day,

I am trying to create a CRuntimeClass pointer using a string with the name of the Class, CMyClass, as per below

CRuntimeClass * pClass = CRuntimeClass::FromName(L"CMyClass");

CMyClass has been already declared as DYNCREATE with base class CRecordView.

No matter all efforts spent, specific pointer returns as NULL.

On debugging, i have noticed that in the following code:

CRuntimeClass* PASCAL CRuntimeClass::FromName(LPCSTR lpszClassName)
    CRuntimeClass* pClass=NULL;


    // search app specific classes
    AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
    for (pClass = pModuleState->m_classList; pClass != NULL;
        pClass = pClass->m_pNextClass)
        if (lstrcmpA(lpszClassName, pClass->m_lpszClassName) == 0)
            return pClass;


variable m_classList do NOT contain my CMyClass.

Any help is much appreciated.
Updated 24-Jul-23 22:41pm

You are passing a Unicode string to a function that requires ASCII. Either change your constant or rebuild your application for Unicode.
Share this answer
Dimitrios Fountoukidis 22-Oct-15 5:48am    
Thanks but as per below, didn't work

CStringA strClass("CMyClass");
CRuntimeClass * pClass = CRuntimeClass::FromName(strClass);

Still returns NULL

Also as per MS: FromName accepts:

static CRuntimeClass* PASCAL FromName(
LPCSTR lpszClassName
static CRuntimeClass* PASCAL FromName(
LPCWSTR lpszClassName

thanks again
Richard MacCutchan 22-Oct-15 5:51am    
You need to ensure all your code is Unicode or all ASCII. And do not use the A or W suffixes on class and function names, it just confuses everything.
Dimitrios Fountoukidis 22-Oct-15 6:14am    
I have also tried the following

CRuntimeClass* point = RUNTIME_CLASS(CMyClass);
CRuntimeClass* pClass = CRuntimeClass::FromName(point->m_lpszClassName);

Although point was successfully created but pClass returned as NULL.

Do i need to declare or include any library in my code?
Richard MacCutchan 22-Oct-15 6:43am    
I do not use MFC so cannot test a sample for you.

Only then your class is added to ModuleState->m_classList.
Share this answer
OriginalGriff 25-Jul-23 3:44am    
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 8 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900