?
Solved

Using AFX_MANAGE_STATE in DLLs

Posted on 1997-12-23
5
Medium Priority
?
720 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 200 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

765 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