Why does AfxGetApp return null when called from a release dll that is getting called from a debug dll

Alexandra Pasinski 1 Reputation point
2022-03-03T19:04:09.393+00:00

I am attempting to register a Visual C++ COM DLL that I have built with a debug configuration.
This COM DLL depends on another Visual C++ COM DLL that was built with a release configuration.
Both DLLs are being registered with regsvr32 (the release DLL has already been successfully registered).
I am using the 32 bit version of regsvr32 (located in SysWOW64) to register both DLLs (since they are both targeting x86).
The DLLs are compiled with the Visual C++ 2010 compiler (with Service Pack 1 installed).
When I attempt to register the debug DLL, regsvr32 fails with this error message.
179788-image.png
The blacked out text is just the name of the debug DLL I attempted to register.
The image of the error message has the following text:

"""
RegSvr32

The module "REDACTED" failed to load.

Make sure the binary is stored at the specified path or debug
it to check for problems with the binary or dependent .DLL
files.

A dynamic link library (DLL) initialization routine failed.
"""

I then attempted to debug the code in the debug DLL that gets called by regsvr32 to register it. The debug DLL initializes itself and then attempts to create an object from the release DLL that it depends on and call a method on that object.

The method in the release DLL that gets called then immediately calls the method AfxGetApp() to get a pointer to the application. This function returns a null pointer. The application then attempts to dereference this null pointer and throws an exception.

It is my understanding that this function should not be returning a null pointer in this case. It returns a valid pointer when both DLLs are built with a debug configuration or when both are built with a release configuration. It only returns null when the DLLs are built with opposite configurations.

What regsvr32.exe calls into:

BOOL DebugDLLApp::InitInstance()  
{  
    if (CWinApp::InitInstance() == FALSE)  
    {  
        return FALSE;  
    }  
  
    ReleaseDLLClass releaseClass;  
    HINSTANCE hInstDll = releaseClass.LoadResourceDLL();  
    ...  
}  

What the debug DLL calls into:

BOOL ReleaseDLLClass::LoadResourceDLL()  
{  
    CWinApp* winApp = AfxGetApp(); // Returns null.  
    LPCSTR exeName = winApp->m_pszExeName; // Null reference exception occurs here.  
    ...  
}  

So, my question is this:

Is there something inherent in MFC that disallows using DLLs with dependencies that were made with different build configurations? If not, why would AfxGetApp() return a null pointer in these specific cases?

Thank you for any guidance you have.

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.