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. How to access Member functions of Application class

How to access Member functions of Application class

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
4 Posts 4 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.
  • S Offline
    S Offline
    Shenthil
    wrote on last edited by
    #1

    I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil

    S A A 3 Replies Last reply
    0
    • S Shenthil

      I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil

      S Offline
      S Offline
      Shehzad Salim
      wrote on last edited by
      #2

      the application class object is taken global named theApp which you can see in your global list simple extern that variable in the file where u want to use that variable then call the user defined functions using the theApp object

      1 Reply Last reply
      0
      • S Shenthil

        I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil

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

        AfxGetApp()

        1 Reply Last reply
        0
        • S Shenthil

          I have created a Dialog based application using appwizard. now, i want to access a user defined function in the application class from my dialog class. how can i do that? Shenthil

          A Offline
          A Offline
          Antti Keskinen
          wrote on last edited by
          #4

          Like the MFC Reference states, your application class is derived from CWinApp. There is a global MFC function AfxGetApp which returns a pointer to your class's parent CWinApp. You can use the below method to access the class. Here, we assume that your the implementation of your dialog class (.cpp file) #includes the application class's header file. This is usually done for you when you create an application, but make sure just in case. If you have not included it, you will receive errors like 'Unknown variable'

          // Achieve a pointer to the application class
          CMyApp* ptrApp = DYNAMIC_DOWNCAST( CMyApp, AfxGetApp() );

          if ( !ptrApp )
          TRACE0("Failed to get application pointer\n");

          // Use the pointer
          UINT uiReturnValue = ptrApp->MyFunction(PARAM param1, PARAM param2);

          The DYNAMIC_DOWNCAST macro does a safe downcast from CWinApp* returned by AfxGetApp into the CMyApp* type. As your application class is derived from CWinApp, the cast is perfectly legal and will yeild a pointer to your derived class. You can use a similar method with any MFC class, for example, you can use GetParentFrame method of getting a view's parent frame (CFrameWnd*) then cast it down to your derived CMyFrameWindow class. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

          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