?
Solved

AfxGetApp() Source Code?

Posted on 1998-07-27
10
Medium Priority
?
1,244 Views
Last Modified: 2013-11-20
I need to know where the source code for AfxGetApp() is located. I am trying to derive my own class from CWinApp and am calling this function from an MFC extension DLL. The problem is that I am not able to use this function to call the functions I overrode in my derived class.

Thanks.

AWD
0
Comment
Question by:awd
  • 5
  • 5
10 Comments
 
LVL 1

Accepted Solution

by:
eburley earned 200 total points
ID: 1319778
you have a couple of options.  You don't really need to see the source to AfxGetApp() (although it's in afxwin1.inl in the mfc include directory, and implemented like:
_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
      { return afxCurrentWinApp; }
)

but it sounds like your trying to call stuff in your class via the returned ptr.  you have two options.  you can simply cast the return of AfxGetApp to a ptr to your class, either the new way (dynamic_cast<yourApp*> AfxGetApp()) or the old way ( (yourApp*)(AfxGetApp()), if you want to be vigilant, you can add an ASSERT(IS_KIND_OFF(RUNTIME_CLASS(yourApp)))  to make sure you got the correct result.  another way would be to define your own function like getMyApp and have it return a ptr of type yourApp.
0
 
LVL 1

Author Comment

by:awd
ID: 1319779
I understand what you are saying and it sounds like it would work. The question is:
How can I reference the class I need to cast to (it resides in the main program)? The cast needs to be done in the DLL.

Thanks.

-Albert
0
 
LVL 1

Expert Comment

by:eburley
ID: 1319780
look at the help files on AFX_MANAGE_STATE.  the module state information includes a ptr to the app.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:awd
ID: 1319781
Is the AFX_MANAGE_STATE macro necessary if the DLL is an MFC extension DLL? I thought this was only used with regular DLLs.
0
 
LVL 1

Expert Comment

by:eburley
ID: 1319782
on second thought, no it's not, but you should be able to do the same thing in the extension dll as in your app.  I looked at some production code we have at work that does precisely what your doing.  it's an extension dll, and it casts the result of AfxGetApp.  I suggest you try it.
0
 
LVL 1

Author Comment

by:awd
ID: 1319783
I would like to try it. I have 2 problems:

1. If I try to cast the AfxGetApp() call in the DLL, I can't get it to recognize the class I need to cast to (the one that I derive from CWinApp).

2.A call to AfxGetApp() works in the main program returning a reference to the object instantiated from my class. I don't need to cast it here anyway. I don't even need to call AfxGetApp() here.

Am I just missing something obvious?

I do appreciate your time. I am new to Experts Exchange. Is 50 pts a reasonable amount for this question?
0
 
LVL 1

Expert Comment

by:eburley
ID: 1319784
pts aren't that relevent to me, and I'm pretty new.  you do need to include the class declaration for your app in the module in the dll that's calling it.

e.g.:

yourApp.h:

class myApp: public CWinApp
{...};

yourExtensionDll.cpp:

#include <yourApp.h>

.
(myApp*)(AfxGetApp())->yourFunctionHere();

understand?
0
 
LVL 1

Author Comment

by:awd
ID: 1319785
I understand your suggestion. I keep getting

'CmyApp' : undeclared identifier

I just went through my code. I have the myApp.h included in the CPP file for the object in my DLL file that makes the function call. And I am casting it the same as your suggestion.

I have went so far as to put a full path between quotes in the #include directive. I'm stumped! Any thoughts? Could anything other than the header file be the problem?
0
 
LVL 1

Expert Comment

by:eburley
ID: 1319786
I'm out of suggestions. sorry.
0
 
LVL 1

Author Comment

by:awd
ID: 1319787
Thanks for the help. You more than answered the original question.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question