Solved

AFX_MODULE_STATE with USRDLL vs AFXDLLs

Posted on 1997-01-10
2
1,572 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

12 Experts available now in Live!

Get 1:1 Help Now