Solved

AFX_MODULE_STATE with USRDLL vs AFXDLLs

Posted on 1997-01-10
2
1,577 Views
Last Modified: 2013-11-20
Will someone please explain how "Module state" is maintained
differently when using a USRDLL vs an AFXDLL? For example,
for a USRDLL, when an CWinApp's InitInstance is called,
the runtime automatically switches the state, but when I
use the shared MFC DLL, I must call AFX_MAINTAIN_STATE(...)
at the beginning of all exported functions.

I don't really understand what is going on with the state, especially when multithreading comes into the picture.
For example, if in an USRDLL I call AfxBeginThread and I'm
linking to shared MFC DLL, how does module state come into play?
0
Comment
Question by:rachels
2 Comments
 

Accepted Solution

by:
doronh earned 200 total points
ID: 1317040
OK, this is a complex issue, but here are a couple of resources that will explain the MFC universe of dll's.  The very basic gist of all these AFX_MANAGE macros is to handle resources, different threads, and ownership.

1) In VC Books online, an article titled Managing the State Data of MFC Modules. It can be found in
Visual C++ Books
 MFC 4.2 (4.x depending on your version)
  Programming with MFC Encyclopedia
   Managing the State Data of MFC Modules
2) Tech article #58:  MFC Module State Implementation
   
Here is the first paragraph from resource #1:
This article discusses the state data of MFC modules and how this state is updated when the flow of execution (the path code takes through an application when executing) enters and leaves a module. Switching module states with the AFX_MANAGE_STATE and METHOD_PROLOGUE macros is also discussed.
 
Note   The term "module" here refers to an executable program, or to a DLL (or set of DLLs) that operate independently of the rest of the application, but uses a shared copy of the MFC DLL. An OLE control is a typical example of a module.
 
As shown in Figure 1, MFC has state data for each module used in an application. Examples of this data include: Windows instance handles (used for loading resources), pointers to the current CWinApp and CWinThread objects of an application, OLE module reference counts, and a variety of maps that maintain the connections between Windows object handles and corresponding instances of MFC objects. However, when an application uses multiple modules, the state data of each module is not application-wide. Rather, each module has its own private copy of the MFC’s state data.

The difference between USRDLL and AFXDLL is who the dll is targeted for.  An AFXDLL is made for use w/in VC++ only.  A USRDLL is created for the non VC users out there (VB, Delphi, straight C).  If your dll is going to be used w/in VC only, then create an AFXDLL.

Hopefully this is enough to get a strong foothold.  Whenever I need to figure things like this out I first search through the on-line help.  Also, I highly highly recommend getting MSDN (MS Developers Network)....can't live w/out it really.

doron
0
 

Expert Comment

by:codediscuss
ID: 20498196
but WHY the state data of each module is not application-wide?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Line meaning 9 82
sumHeights  challenge 17 66
Updating statistics with error notification email in SQL server 4 73
Message not shown 5 33
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 (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

15 Experts available now in Live!

Get 1:1 Help Now