AfxGetApp NULL pointer issue
-
In Test.exe module the application class
CTestApp
was derived fromCWinApp
. Everything worked fine. Then I decided to move some common stuff fromCTestApp
toCCommonApp
. ThisCCommonApp
is defined in Util.dll module. NowCTestApp
is derived fromCCommonApp
which is derived fromCWinApp
. The contructor ofCTestApp
is called properly and all its parent contructors. However the application crashes right after this, because theAfxGetApp
returns NULL in MFC core and can't callInitApplication
method. Any idea what can be the problem? Thanks, Abyss -
In Test.exe module the application class
CTestApp
was derived fromCWinApp
. Everything worked fine. Then I decided to move some common stuff fromCTestApp
toCCommonApp
. ThisCCommonApp
is defined in Util.dll module. NowCTestApp
is derived fromCCommonApp
which is derived fromCWinApp
. The contructor ofCTestApp
is called properly and all its parent contructors. However the application crashes right after this, because theAfxGetApp
returns NULL in MFC core and can't callInitApplication
method. Any idea what can be the problem? Thanks, AbyssYou'll still need a global app object in the EXE project, something like: // The one and only CTestApp object CTestApp theApp; If you already have that... How is CCommonApp declared? It should be like this: class AFX_EXT_CLASS CCommonApp : public CWinApp { ... }; Your DLL needs to be a legitimate MFC extension DLL as well. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
You'll still need a global app object in the EXE project, something like: // The one and only CTestApp object CTestApp theApp; If you already have that... How is CCommonApp declared? It should be like this: class AFX_EXT_CLASS CCommonApp : public CWinApp { ... }; Your DLL needs to be a legitimate MFC extension DLL as well. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Thanks for advice. I have this global application object in EXE. My DLL is also a dynamic DLL, where the CCommonApp is exported using
__declspec(dllexport)
and imported in EXE using__declspec(dllimport)
. I'm not sure what did you mean by legitime MFC extension DLL. My Util.dll project has its ownCUtilApp : CWinApp
class, and_USRDLL
define in project settings. Also tried to useDllMain
method with_AFXEXT
define. It did not help :-(. Maybe something is wrong with project settings, maybe something else. But I have no idea what could be wrong. Thanks, Abyss -
Thanks for advice. I have this global application object in EXE. My DLL is also a dynamic DLL, where the CCommonApp is exported using
__declspec(dllexport)
and imported in EXE using__declspec(dllimport)
. I'm not sure what did you mean by legitime MFC extension DLL. My Util.dll project has its ownCUtilApp : CWinApp
class, and_USRDLL
define in project settings. Also tried to useDllMain
method with_AFXEXT
define. It did not help :-(. Maybe something is wrong with project settings, maybe something else. But I have no idea what could be wrong. Thanks, AbyssAbyss wrote:
I'm not sure what did you mean by legitime MFC extension DLL.
See Extension DLLs[^] Classes derived from MFC classes in a DLL and then derived from in an EXE means the DLL MUST be an MFC extension DLL. MFC requires certain initialization in DllMain() and import/export properties on classes must be dealt with. It's all documented at the link (and sublinks) above. Also important - you can't statically link to MFC libraries - the DLL and the EXE need to use the shared MFC DLL library. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
-
Abyss wrote:
I'm not sure what did you mean by legitime MFC extension DLL.
See Extension DLLs[^] Classes derived from MFC classes in a DLL and then derived from in an EXE means the DLL MUST be an MFC extension DLL. MFC requires certain initialization in DllMain() and import/export properties on classes must be dealt with. It's all documented at the link (and sublinks) above. Also important - you can't statically link to MFC libraries - the DLL and the EXE need to use the shared MFC DLL library. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
Well, I tried to generate these two projects from scratch using VS Wizard. The EXE application is a simple MFC non doc-view architecture module. The DLL is a MFC Extension module. I added my
CCommonApp
class to the DLL and exported using AFX_EXT_CLASS macro. In EXE I replacedCWinApp
withCCommonApp
. The result is the same :(( :confused: Now I would say that this is not supported. However I can remember that in the past I saw several projects where it worked well (unfortunately I can't find them)... Any help is appreciated. Thanks, Abyss -
Well, I tried to generate these two projects from scratch using VS Wizard. The EXE application is a simple MFC non doc-view architecture module. The DLL is a MFC Extension module. I added my
CCommonApp
class to the DLL and exported using AFX_EXT_CLASS macro. In EXE I replacedCWinApp
withCCommonApp
. The result is the same :(( :confused: Now I would say that this is not supported. However I can remember that in the past I saw several projects where it worked well (unfortunately I can't find them)... Any help is appreciated. Thanks, AbyssI found the culprit. My project is base on Multibyte character set. When I introduced the new DLL module it was set to unicode character set by default. And this caused the whole problem - different MFC libraries where linked to my modules. It was enough to switch the project to Multibyte char. set and it started to working. It does not matter if the DLL is MFC Extension or Regular DLL. In both cases it works fine. Thanks for your help. Abyss
-
I found the culprit. My project is base on Multibyte character set. When I introduced the new DLL module it was set to unicode character set by default. And this caused the whole problem - different MFC libraries where linked to my modules. It was enough to switch the project to Multibyte char. set and it started to working. It does not matter if the DLL is MFC Extension or Regular DLL. In both cases it works fine. Thanks for your help. Abyss
Cool! Glad you found it! :)
Abyss wrote:
It does not matter if the DLL is MFC Extension or Regular DLL. In both cases it works fine.
It will probably matter eventually - the initialization in the extension DLL is important to many of the MFC classes. It just depends which ones you use and where :) The CWinApp class is one of them - it's initialization in the DLL module is used with window/dialog creation for example. Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder