Solved

AfxGetApp() Source Code?

Posted on 1998-07-27
10
1,089 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

910 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

16 Experts available now in Live!

Get 1:1 Help Now