Example of building a simple C++ DLL

To build a simple C++ DLL, use the _Export keyword or the #pragma export directive to export specific external functions and variables. Ensure that classes and class members are exported correctly, especially if they use templates.

For example, Figure 1 shows how to create a DLL executable module named triangle using the #pragma export directive. This example exports the functions getarea(), getperim(), the static member objectCount, and the constructor for class triangle.

Figure 1. Using #pragma export to create the triangle DLL executable module
   class triangle
   {
      public:
         static int objectCount;
         getarea();
         getperim();
         triangle(void);
   };
   #pragma export(triangle::objectCount)
   #pragma export(triangle::getarea())
   #pragma export(triangle::getperim())
   #pragma export(triangle::triangle(void))

Similarly, Figure 2 shows how to create a DLL executable module named triangle using the _Export keyword:

Figure 2. Using _Export to create the triangle DLL executable module
   {
      public:
         static int _Export objectCount;
         double _Export getarea();
         double _Export getperim();
         _Export triangle::triangle(void);
   };
There are some restrictions when using the _Export keyword.
If you apply the _Export keyword to a class, then it automatically exports the static members, defined functions, constructors, and destructors of that class, as shown in the following example. This behavior is the same as using the EXPORTALL compiler option.
   class triangle
   {
      public:
         static int objectCount;
         double getarea();
         double getperim();
         triangle(void);
   };