Solved

Using AFX_MANAGE_STATE in DLLs

Posted on 1997-12-23
5
719 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
[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 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1313014
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
ID: 1313015
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
ID: 1313016
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
ID: 1313017
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
ID: 1313018
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

687 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