Article ID: 131946 - Last Review: November 21, 2006 - Revision: 2.1 PRB: Bad Pointer from RUNTIME_CLASS with Class from _AFXDLLThis article was previously published under Q131946 On This PageSYMPTOMS
An application uses the RUNTIME_CLASS macro to obtain a pointer to the
CRuntimeClass associated with a class implemented in an extension DLL. When
that pointer is used, an access violation or other unexpected behavior
occurs.
CAUSE
The CRuntimeClass data member of the class is not properly declared as
imported data. For example, this error occurs if the CRuntimeClass object is exported via the DLL's module definition (.def) file, but AFX_DATA is not defined as __declspec(dllimport). This must be done in the header file containing the class declaration when the .exe file that imports the class is built. This enables the linker to resolve the reference to the CRuntimeClass using the import library for the DLL, so no build-time errors occur. However, the code generated by the compiler doesn't use the import table to determine the address of the item. RESOLUTION
Ensure that AFX_DATA is defined as __declspec(dllimport) when including the
class header file in code for the module that imports the class from the
DLL. See the "Sample Code" section of this article for a detailed example.
STATUS
This behavior is by design.
MORE INFORMATION
The DECLARE_DYNAMIC macro declares a CRuntimeClass object as follows:
The RUNTIME_CLASS macro is defined in afx.h as follows: Here is some disassembled (X86) code from an .exe file that uses a class named CMyClass, which it imports from an extension DLL. The extension DLL uses its module definition file to export the members of the class, and the header file does not declare the class or any of its members as imported. m_pMyClass has just been initialized as follows: Here is the disassembly of the same code when the header file defines AFX_DATA as __declspec(dllimport): Notice the difference in the way the stack is set up prior to the call to IsKindOf, especially the first item pushed. In the second case, eax is loaded with the address of an item located through the import table, while in the first case, a direct value is used. Sample CodeREFERENCES
For additional information on how to export classes from extension DLLs,
please see:
APPLIES TO
| Article Translations
|

Back to the top
