Solved

Using AFX_MANAGE_STATE in DLLs

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
move a line in eclipse 3 107
How to convert MFC::CString to UTF8 wchar_t* 10 286
firstChar challenge 13 121
Problem to event 3 104
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: 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. …
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.

832 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