Solved

Using AFX_MANAGE_STATE in DLLs

Posted on 1997-12-23
5
710 Views
Last Modified: 2013-11-19
I'm looking for a brief discussion of AFX_MANAGE_STATE(AfxGetStaticModuleState()).  When I use this at the entry point to a DLL as stated in the comments generated by AppWizard, I cannot use CWnd or derivatives of CWnd in the DLL created in the application since they are in the applications window map not the DLL's.  The debug ASSERTions fail.  For example, if I pass a pointer to the status bar of CMainFrame to a DLL exported function, I cannot update the status bar in the DLL function with code like:  pBar->SetPaneText(0, "String", FALSE).  Is there a proper way to accomplish this that I am not doing?
0
Comment
Question by:hlavinka
5 Comments
 
LVL 23

Expert Comment

by:chensu
Comment Utility
You'd better pass the window handle instead of CWnd *. For example, you can pass the handle of the status bar and send the SB_SETTEXT message to accomplish pBar->SetPaneText(0, "String", FALSE).
0
 

Author Comment

by:hlavinka
Comment Utility
I know I can use the window handle with SDK calls to accomplish this simplified task.  However, in reality I want to know if I can use the CWnd derivative itself so that 1) its member data are properly updated reflecting any changes made in the DLL, and 2) its member functions can be called.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
The best discussion of this that I've seen is in "Professional MFC" by Mike Blaszczak (WROX Press).  In my opinion, no MFC developer should be without this book.
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
What you want to do is similar to writing an MFC extension DLL. Extension DLLs can also be used for passing MFC-derived objects between the application and the DLL. Look into the Visual C++ documentation about Extension DLLs and the MFC Advanced Concepts sample DLLHUSK.
0
 

Accepted Solution

by:
JHaack earned 100 total points
Comment Utility
The purpose of calling AFX_MANAGE_STATE(AfxGetStaticModuleState()) is to allow the main application access to your DLL's state information.  That is, to allow the main application to use the window maps, resources, and other global data associated with the DLL but not the main application.

If you want the DLL to use window maps, resources, and other global data associated with the application, you must use the main application's state information (do not switch to the DLL's state information).  As you'll recall, a DLL shares the same address space as the calling application, so pointers passed to the DLL are valid.  But, the window pointers passed from the application are meaningles in the scope of the DLL unless they are added to the DLLs state information using Attach or FromHandle.

Simply, if the resource belongs to the calling application, use the calling applications state information; if the resource belongs to the DLL, switch to the DLL's state information before using it.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

10 Experts available now in Live!

Get 1:1 Help Now