Solved

AfxGetApp() Source Code?

Posted on 1998-07-27
10
1,078 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 50 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now