Solved

Using AFX_MANAGE_STATE in DLLs

Posted on 1997-12-23
5
713 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
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

11 Experts available now in Live!

Get 1:1 Help Now