PRODUCT : Borland C++ NUMBER : 1718 VERSION : All OS : WIN DATE : October 25, 1993 PAGE : 1/2 TITLE : Configuring Classes for Use in DLLs and EXEs Some C++ considerations when using classes in DLLs, and other situations. It is often convenient in a C++ Windows application to put a class and its member functions in a DLL, or to derive a class from a class defined in a DLL, or to call a member function in an exe from a DLL. In each of these situations, one must be aware that, since both the DLL and the EXE have their own Data Segment, virtual function calls with near virtual tables (the default) will not work. [NOTE: The compiler calls virtual functions by retrieving their address from a table commonly called the vtable - This table usually resides in the default data segment of an application or DLL. In the default scenario, the compiler accesses the table using a near pointer which implies that a class shared across DLL and EXE will be unable to access a far virtual table]. Therefore some care must be taken to remedy the situation. In order to ensure that far virtual tables are used in the .EXE file, classes should be defined as huge classes, thus: class huge foo { ... }; Additionally, smart callbacks must to be used, and case sensitive (imports and) exports enabled. Smart callbacks are needed so that DS will be loaded when necessary through SS as opposed to AX. Traditionally Callback functions have prolog code which reloads their data segment register with the value the AX register. This scheme works fine for functions called by Windows since the latter ensures that AX contains the proper value. However, with C++ it is possible to have a method in an .EXE call a virtual function defined in a DLL which in turn calls another virtual function defined in the .EXE. In this scenario, the data segment of the application must be restore from the SS register instead of AX since the DLL method will not set the AX register before calling the method in the EXE. In the DLL, you must declare the class with the _export PRODUCT : Borland C++ NUMBER : 1718 VERSION : All OS : WIN DATE : October 25, 1993 PAGE : 2/2 TITLE : Configuring Classes for Use in DLLs and EXEs modifier. This will define the class as huge, and will also export all member functions and static data members. In addition, you will need to compile with "DLL all functions exportable" for the Entry/Exit code. Case sensitive exports (and imports) should also be enabled. In the file _DEFS.H, you will find the following macros: # if defined(__DLL__) # if defined(_RTLDLL) || defined(_CLASSDLL) # define _CLASSTYPE _export # else # define _CLASSTYPE far # endif # define _FAR far # elif defined(_RTLDLL) || defined(_CLASSDLL) # define _CLASSTYPE huge The above can be used to simplify the declaration of classes which are to be shared across DLLs and EXEs. The following is an example: //MYCLASS.H (Header declaring classes) class _CLASSTYPE SampleClass { ... }; The _CLASSTYPE macro will expand to _export when building a DLL and to huge when building a EXE provided that the symbol '_CLASSDLL' is defined. Make sure you define the '_CLASSDLL' macro. DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.