Solved

AfxGetApp() Source Code?

Posted on 1998-07-27
10
1,124 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 52
has22 challenge 11 105
withoutTen challenge 14 132
Unix Command -- Challenging  question 7 96
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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