OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
Writing Shared Classes

A class instance in a DLL can be shared among multiple applications.

For example, you can share code that defines a dialog box by defining a shared dialog class in a DLL. The shared class can then be used in or derived from by other applications or DLLs. To share a class you need to export the class from the DLL and import the class into your application.

To define shared classes:

  • Conditionally declare your class as either _export or _import.
  • Pass a TModule* parameter to the window constructors (in some situations).

If you declare a shared class in an include file that is included by both the DLL and an application using the DLL, the class must be declared _export when compiling the DLL and _import when compiling the application. You can do this by defining a group of macros, one of which is conditionally set to _export when building the DLL and to _import when using the DLL. For example:

// BUILDEXAMPLEDLL is defined when building EXAMPLE as a DLL,
// but not when building it as a static library.
#if defined(BUILDEXAMPLEDLL)
#define _EXAMPLECLASS __export
// USEEXAMPLEDLL is defined when building an application or DLL
// that uses EXAMPLE as a DLL.
#elif defined (USEEXAMPLEDLL)
#define _EXAMPLECLASS __import
// This is used when compiling EXAMPLE as a static library.
#else
#define _EXAMPLECLASS
#endif
class _EXAMPLECLASS TColorControl : public TControl {
public:
...
};

By defining BUILDEXAMPLEDLL (on the command line, for example) when you are building the DLL, you cause _EXAMPLECLASS to expand to _export. This causes the class to be exported and shared by applications using the DLL.

By defining USEEXAMPLEDLL when you are building the application that will use the DLL, you cause _EXAMPLECLASS to expand to _import. The application will know what type of object it will import.