home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / TN1001.ZIP / TN1001.DOC
Text File  |  1993-04-19  |  3KB  |  104 lines

  1. TITLE  Creating C++ DLL                                    TN1001
  2. =====
  3.  
  4. DESCRIPTION
  5. ===========
  6.  
  7.   This example creates a class called base in testdll.cpp which is
  8.   placed into a DLL.
  9.  
  10.   Main in sample.c instantiates class base and calls a function in
  11.   base to print out a message.  C++ exception handling from a DLL
  12.   is demonstrated by having the function do a throw which is caught
  13.   in a catch block in main.
  14.  
  15.   Mangled names apprear in the IMPORT section of sample.def below.
  16.  
  17.   1) use "cppfilt /B /P sample.obj" to dump mangled names
  18.   2) build a library using sample.obj and get a listing using lib.exe
  19.   3) there are some utilities programs available that can dump obj names
  20.  
  21. SOURCE
  22. ======
  23.  
  24.   dlltest.hpp
  25.               class base {
  26.                 public:
  27.                   void func(void);
  28.                   ~base(void);
  29.                   class bomb{
  30.                     public:
  31.                       bomb(int i) { }
  32.                   };
  33.               };
  34.  
  35.   dlltest.cpp
  36.               #include  <stdio.h>
  37.               #include  "dlltest.hpp"
  38.  
  39.               #pragma export(base::func(void))
  40.               #pragma export(base::~base(void))
  41.  
  42.               base::~base(void) {
  43.                  printf("in base destructor\n");
  44.               }
  45.  
  46.               void base::func(void) {
  47.                  printf("in base func\n");
  48.                  throw bomb(1);
  49.               }
  50.  
  51.   dlltest.def
  52.               LIBRARY DLLTEST INITINSTANCE
  53.               DESCRIPTION 'Classes Dynamic Linking library'
  54.               PROTMODE
  55.               DATA        MULTIPLE READWRITE LOADONCALL
  56.               CODE        LOADONCALL
  57.  
  58.  
  59.   sample.cpp
  60.               #include <stdio.h>
  61.               #include  "dlltest.hpp"
  62.  
  63.               void main()
  64.               {
  65.                 base *b = new base();
  66.                 printf("after new\n");
  67.                 try {
  68.                   b->func();
  69.                 }
  70.                 catch (base::bomb x) {
  71.                   printf("caught\n");
  72.                 }
  73.                 printf("after d->func()\n");
  74.  
  75.                 delete b;
  76.                 printf("after delete b\n");
  77.               }
  78.  
  79.   sample.def
  80.               NAME SAMPLE WINDOWCOMPAT
  81.               IMPORTS
  82.                 dlltest.__dt__4baseFv
  83.                 dlltest.func__4baseFv
  84.  
  85. COMPILE OPTIONS
  86. ===============
  87.  
  88.   icc -Gm+ -Fd -Ge-  dlltest.cpp dlltest.def
  89.   icc -Gm+ -Fd       sample.cpp sample.def
  90.  
  91.   various combinations of -Gd+ -Gm are possible depending on the desired
  92.   configuration.
  93.  
  94. OUTPUT
  95. ======
  96.  
  97.    after new
  98.    in base func
  99.    caught
  100.    after d->func()
  101.    in base destructor
  102.    after delete b
  103.  
  104.