Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

AfxGetApp() Source Code?

Posted on 1998-07-27
10
Medium Priority
?
1,210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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