Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. AfxGetApp NULL pointer issue

AfxGetApp NULL pointer issue

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++asp-netquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abyss
    wrote on last edited by
    #1

    In Test.exe module the application class CTestApp was derived from CWinApp. Everything worked fine. Then I decided to move some common stuff from CTestApp to CCommonApp. This CCommonApp is defined in Util.dll module. Now CTestApp is derived from CCommonApp which is derived from CWinApp. The contructor of CTestApp is called properly and all its parent contructors. However the application crashes right after this, because the AfxGetApp returns NULL in MFC core and can't call InitApplication method. Any idea what can be the problem? Thanks, Abyss

    M 1 Reply Last reply
    0
    • A Abyss

      In Test.exe module the application class CTestApp was derived from CWinApp. Everything worked fine. Then I decided to move some common stuff from CTestApp to CCommonApp. This CCommonApp is defined in Util.dll module. Now CTestApp is derived from CCommonApp which is derived from CWinApp. The contructor of CTestApp is called properly and all its parent contructors. However the application crashes right after this, because the AfxGetApp returns NULL in MFC core and can't call InitApplication method. Any idea what can be the problem? Thanks, Abyss

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • M Mark Salsbery

        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

        A Offline
        A Offline
        Abyss
        wrote on last edited by
        #3

        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 own CUtilApp : CWinApp class, and _USRDLL define in project settings. Also tried to use DllMain 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

        M 1 Reply Last reply
        0
        • A 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 own CUtilApp : CWinApp class, and _USRDLL define in project settings. Also tried to use DllMain 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

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • M Mark Salsbery

            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

            A Offline
            A Offline
            Abyss
            wrote on last edited by
            #5

            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 replaced CWinApp with CCommonApp. 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

            A 1 Reply Last reply
            0
            • A 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 replaced CWinApp with CCommonApp. 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

              A Offline
              A Offline
              Abyss
              wrote on last edited by
              #6

              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

              M 1 Reply Last reply
              0
              • A 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

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups