The information in this article applies to:
SYMPTOMSIf you build an MFC _USRDLL using Visual C++, version 1.0, without defining a CWinApp object, when its LibMain() function is called you will get the following message in the debugger Output Window: If you are using Visual C++, version 1.0 for Windows NT, you will get the following message box when DllMain() is called: If you are using Visual C++, version 1.5, you will get the following message when LibMain() is called: If you are running a Windows-based application, the call to LibMain() will fail without warning, unless you are running under the debugger. Under Windows NT, you will get the warning shown previously when calling DllMain() (if you are running a debug version of your DLL), followed by message boxes from the system stating that the application caused a problem. In order to support resource-only DLLs, the assertions described above were removed in Microsoft Visual C++ 32-bit Edition, versions 2.0, 2.1, 2.2, and 4.0. Using those versions, calling a CWinApp member function through the pointer returned by AfxGetApp() will result in an access violation if there is no CWinApp object defined in the DLL. CAUSE
When an attempt is made to load a DLL for the first time, the DLL[ASCII 146]s entry
point (LibMain() for Windows, DllMain() for Windows NT) is called to
initialize its instance. For a _USRDLL, the entry point calls AfxWinInit(),
which calls AfxGetApp(). If no CWinApp object has been instantiated in the
DLL, AfxGetApp() returns "NULL". AfxGetApp() cannot access the calling
application's CWinApp (if the calling application is an MFC application).
This causes the assertion. RESOLUTION
Be sure to instantiate an object of type CWinApp or a class derived from
CWinApp. If it is important that you not have a CWinApp object in your
_USRDLL, see the "MORE INFORMATION" section of this article, below, for
suggestions. MORE INFORMATION
It is not necessary for your DLL to call AfxWinInit() and AfxWinTerm() for
it to use the MFC collection classes and the simple class types such as
CString. If you wish to use only these MFC classes in your DLL, you can
omit the instantiation of a CWinApp object, but you also must write your
own LibMain() or DllMain() function for the DLL. Sample Code for WindowsThe code below shows one possible implementation of LibMain(). For the sake of pointing out the initialization portion, LibMain() calls MyInit(), which consists of a very simplified simulation of AfxWinInit().
Sample Code for Windows NTThe code below shows one possible implementation of DllMain(). For the sake of pointing out the initialization portion, DllMain() calls MyInit(), which consists of a very simplified simulation of AfxWinInit().
Additional query words: 1.00 1.50 2.00 2.10 2.50 4.00
Keywords : kbDLL kbMFC kbVC |
Last Reviewed: July 28, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |