Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Create a DLL in C++ Builder

 

Before being able to call a DLL, one has to create a DLL first.

 

 

To create a DLL in C++ Builder, do the following steps:

 

The following code is produced, in the default-named Unit1.cpp:

 

 

 

 

 

Unit1.cpp, created after 'File | New | Other | DLL Wizard'

 

//---------------------------------------------------------------------------

#include <windows.h>
//---------------------------------------------------------------------------
//   Important note about DLL memory management when your DLL uses the
//   static version of the RunTime Library:
//
//   If your DLL exports any functions that pass String objects (or structs/
//   classes containing nested Strings) as parameter or function results,
//   you will need to add the library MEMMGR.LIB to both the DLL project and
//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB
//   if any other projects which use the DLL will be performing new or delete
//   operations on any non-TObject-derived classes which are exported from the
//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,
//   the file BORLNDMM.DLL should be deployed along with your DLL.
//
//   To avoid using BORLNDMM.DLL, pass string information using "char *" or
//   ShortString parameters.
//
//   If your DLL uses the dynamic version of the RTL, you do not need to
//   explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
        return 1;
}
//---------------------------------------------------------------------------

 

 

You might want to 'File | Save All' and save this unit as 'UnitEntryPoint.cpp' and the project as 'ProjectDll.bpr'.

 

Now it's time to add a function. Do 'File | New | Unit'. Save the newly created unit as 'UnitFunctions.cpp'.

 

View UnitFunctions.h first.

 

 

 

 

 

UnitFunctions.h before adding our function

 

//---------------------------------------------------------------------------
#ifndef UnitFunctionsH
#define UnitFunctionsH
//---------------------------------------------------------------------------

#endif

 

There is nothing in UnitFunctions.h yet, except for an #include guard. Below 'UnitFunctions.h' has one function added.

 

 

 

 

 

UnitFunctions.h after adding our function

 

//---------------------------------------------------------------------------
#ifndef UnitFunctionsH
#define UnitFunctionsH
//---------------------------------------------------------------------------

#ifdef __cplusplus
extern "C"
{
#endif

__declspec (dllexport) const int GetAnswerOfLife();

#ifdef __cplusplus
}
#endif

//---------------------------------------------------------------------------
#endif

 

The function put in the DLL is called GetAnswerOfLife and will return the value of 42. Note the #ifdef's before and after the function. These are obligatory!

 

Now the function GetAnswerOfLife must be defined in UnitFunctions.cpp. Upon viewing it, the code looks like below

 

 

 

 

 

UnitFunctions.cpp, before adding own code

 

//---------------------------------------------------------------------------
#pragma hdrstop

#include "UnitFunctions.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)

 

Now add the code of GetAnswerOfLife in the regular way, as shown below

 

 

 

 

 

UnitFunctions.cpp, after adding own code

 

//---------------------------------------------------------------------------
#pragma hdrstop

#include "UnitFunctions.h"
//---------------------------------------------------------------------------
const int GetAnswerOfLife()
{
  return 42;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)

 

Press F9 and you have just created your first DLL! After it is created an error will appear 'One cannot debug project unless a host application is defined.'. No problem, as, again, you have just created your first DLL. Time to call a function from your DLL.

 

 

 

 

 

Adding some diagnostic features (optional)

 

If you are new to using DLL's, you might want to add some diagnostic features to UnitEntryPoint.cpp. If not, perhaps you want to call a DLL.

 

UnitEntryPoint.cpp with diagnostic features added

 

//---------------------------------------------------------------------------
#include <windows.h>
//---------------------------------------------------------------------------
// Important note about DLL memory management when your DLL uses the
// static version of the RunTime Library:
//
// If your DLL exports any functions that pass String objects (or structs/
// classes containing nested Strings) as parameter or function results,
// you will need to add the library MEMMGR.LIB to both the DLL project and
// any other projects that use the DLL. You will also need to use MEMMGR.LIB
// if any other projects which use the DLL will be performing new or delete
// operations on any non-TObject-derived classes which are exported from the
// DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
// EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
// the file BORLNDMM.DLL should be deployed along with your DLL.
//
// To avoid using BORLNDMM.DLL, pass string information using "char *" or
// ShortString parameters.
//
// If your DLL uses the dynamic version of the RTL, you do not need to
// explicitly add MEMMGR.LIB as this will be done implicitly for you
//---------------------------------------------------------------------------

#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
  switch (reason)
  {
    case DLL_PROCESS_ATTACH:
      if (lpReserved)
        MessageBox(0,"Process has attached to DLL by static loading",
          "UnitEntryPoint.cpp",MB_OK);
      else
        MessageBox (0,"Process has attached to DLL by dynamic loadinging",
        "UnitEntryPoint.cpp",MB_OK);
      break;
    case DLL_THREAD_ATTACH:
      MessageBox (0,"Thread has attached to DLL",
        "UnitEntryPoint.cpp",MB_OK);
    break;
    case DLL_THREAD_DETACH:
      MessageBox (0,"Thread has detached from DLL",
        "UnitEntryPoint.cpp",MB_OK);
      break;
    case DLL_PROCESS_DETACH:
      MessageBox (0,"Process has detached from DLL",
        "UnitEntryPoint.cpp",MB_OK);
      break;
  }

  return 1;
}
//---------------------------------------------------------------------------

 

Note the use MessageBox instead of ShowMessage, as MessageBox is a Win32 API function (so it can be found in windows.h), where ShowMessage is a VCL function.

 

Perhaps you now want to go to the calling a DLL page.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml