Jump to content

Creating C++ DLL: Difference between revisions

From EDM2
Created page with "; TN1001 This example creates a class called base in testdll.cpp which is placed into a DLL. Main in sample.c instantiates class base and calls a function in base to print o..."
 
Ak120 (talk | contribs)
mNo edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
This example creates a class called base in testdll.cpp which is placed into a DLL.
This example creates a class called base in testdll.cpp which is placed into a DLL.


Main in sample.c instantiates class base and calls a function in
Main in sample.c instantiates class base and calls a function in base to print out a message. C++ exception handling from a DLL is demonstrated by having the function do a throw which is caught in a catch block in main.
base to print out a message. C++ exception handling from a DLL
is demonstrated by having the function do a throw which is caught
in a catch block in main.


Mangled names apprear in the IMPORT section of sample.def below.
Mangled names apprear in the IMPORT section of sample.def below.
1) use "cppfilt /B /P sample.obj" to dump mangled names
# use "cppfilt /B /P sample.obj" to dump mangled names
2) build a library using sample.obj and get a listing using lib.exe
# build a library using sample.obj and get a listing using lib.exe
3) there are some utilities programs available that can dump obj names
# there are some utilities programs available that can dump obj names


==SOURCE==
==SOURCE==
; dlltest.hpp
; dlltest.hpp
              class base {
class base {
                public:
  public:
                  void func(void);
    void func(void);
                  ~base(void);
    ~base(void);
                  class bomb{
    class bomb{
                    public:
      public:
                      bomb(int i) { }
        bomb(int i) { }
                  };
    };
              };
};


; dlltest.cpp
; dlltest.cpp
              #include  <stdio.h>
#include  <stdio.h>
              #include  "dlltest.hpp"
#include  "dlltest.hpp"
 
              #pragma export(base::func(void))
#pragma export(base::func(void))
              #pragma export(base::~base(void))
#pragma export(base::~base(void))
 
              base::~base(void) {
base::~base(void) {
                printf("in base destructor\n");
    printf("in base destructor\n");
              }
}
 
              void base::func(void) {
void base::func(void) {
                printf("in base func\n");
    printf("in base func\n");
                throw bomb(1);
    throw bomb(1);
              }
}


; dlltest.def
; dlltest.def
              LIBRARY DLLTEST INITINSTANCE
LIBRARY DLLTEST INITINSTANCE
              DESCRIPTION 'Classes Dynamic Linking library'
DESCRIPTION 'Classes Dynamic Linking library'
              PROTMODE
PROTMODE
              DATA        MULTIPLE READWRITE LOADONCALL
DATA        MULTIPLE READWRITE LOADONCALL
              CODE        LOADONCALL
CODE        LOADONCALL
 


; sample.cpp
; sample.cpp
              #include <stdio.h>
#include <stdio.h>
              #include "dlltest.hpp"
#include "dlltest.hpp"
 
              void main()
void main()
              {
{
                base *b = new base();
    base *b = new base();
                printf("after new\n");
    printf("after new\n");
                try {
    try {
                  b->func();
      b->func();
                }
    }
                catch (base::bomb x) {
    catch (base::bomb x) {
                  printf("caught\n");
      printf("caught\n");
                }
    }
                printf("after d->func()\n");
  printf("after d->func()\n");
 
                delete b;
  delete b;
                printf("after delete b\n");
  printf("after delete b\n");
              }
}


; sample.def
; sample.def
              NAME SAMPLE WINDOWCOMPAT
NAME SAMPLE WINDOWCOMPAT
              IMPORTS
IMPORTS
                dlltest.__dt__4baseFv
  dlltest.__dt__4baseFv
                dlltest.func__4baseFv
  dlltest.func__4baseFv


==COMPILE OPTIONS==
==COMPILE OPTIONS==
  icc -Gm+ -Fd -Ge-  dlltest.cpp dlltest.def
  icc -Gm+ -Fd -Ge-  dlltest.cpp dlltest.def
  icc -Gm+ -Fd      sample.cpp sample.def
  icc -Gm+ -Fd      sample.cpp sample.def
various combinations of -Gd+ -Gm are possible depending on the desired configuration.
various combinations of -Gd+ -Gm are possible depending on the desired configuration.


==OUTPUT==
==OUTPUT==
   after new
   after new
   in base func
   in base func
Line 92: Line 83:
   in base destructor
   in base destructor
   after delete b
   after delete b
[[Category:C++ Articles]]

Latest revision as of 06:53, 27 December 2022

TN1001

This example creates a class called base in testdll.cpp which is placed into a DLL.

Main in sample.c instantiates class base and calls a function in base to print out a message. C++ exception handling from a DLL is demonstrated by having the function do a throw which is caught in a catch block in main.

Mangled names apprear in the IMPORT section of sample.def below.

  1. use "cppfilt /B /P sample.obj" to dump mangled names
  2. build a library using sample.obj and get a listing using lib.exe
  3. there are some utilities programs available that can dump obj names

SOURCE

dlltest.hpp
class base {
  public:
    void func(void);
    ~base(void);
    class bomb{
      public:
        bomb(int i) { }
    };
};
dlltest.cpp
#include  <stdio.h>
#include  "dlltest.hpp"

#pragma export(base::func(void))
#pragma export(base::~base(void))

base::~base(void) {
   printf("in base destructor\n");
}

void base::func(void) {
   printf("in base func\n");
   throw bomb(1);
}
dlltest.def
LIBRARY DLLTEST INITINSTANCE
DESCRIPTION 'Classes Dynamic Linking library'
PROTMODE
DATA        MULTIPLE READWRITE LOADONCALL
CODE        LOADONCALL
sample.cpp
#include <stdio.h>
#include "dlltest.hpp"

void main()
{
   base *b = new base();
   printf("after new\n");
   try {
     b->func();
   }
   catch (base::bomb x) {
     printf("caught\n");
   }
  printf("after d->func()\n");

  delete b;
  printf("after delete b\n");
}
sample.def
NAME SAMPLE WINDOWCOMPAT
IMPORTS
  dlltest.__dt__4baseFv
  dlltest.func__4baseFv

COMPILE OPTIONS

icc -Gm+ -Fd -Ge-  dlltest.cpp dlltest.def
icc -Gm+ -Fd       sample.cpp sample.def

various combinations of -Gd+ -Gm are possible depending on the desired configuration.

OUTPUT

  after new
  in base func
  caught
  after d->func()
  in base destructor
  after delete b