Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
Before being able to call a DLL, one has to create a DLL first.
Download all files created in this page (zip).
To create a DLL in C++ Builder, do the following steps:
The following code is produced, in the default-named Unit1.cpp.
//---------------------------------------------------------------------------
#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
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
* View this code in plain text.
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.
//---------------------------------------------------------------------------
#ifndef UnitFunctionsH
#define UnitFunctionsH
//---------------------------------------------------------------------------
There is nothing in UnitFunctions.h yet, except for an #include guard. Below 'UnitFunctions.h' has one function added.
//---------------------------------------------------------------------------
#ifndef UnitFunctionsH
#define UnitFunctionsH
//---------------------------------------------------------------------------
#ifdef __cplusplus
extern "C"
{
__declspec (dllexport) const int GetAnswerOfLife();
#ifdef __cplusplus
}
//---------------------------------------------------------------------------
* View this code in plain text
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
//---------------------------------------------------------------------------
#pragma hdrstop
#include "UnitFunctions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
Now add the code of GetAnswerOfLife in the regular way, as shown below
//---------------------------------------------------------------------------
#pragma hdrstop
#include "UnitFunctions.h"
//---------------------------------------------------------------------------
{
return 42;
}
//---------------------------------------------------------------------------
#pragma package(smart_init)
* View this code in plain text.
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.
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.
//---------------------------------------------------------------------------
#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
//---------------------------------------------------------------------------
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);
MessageBox (0,"Process has attached to DLL by dynamic loadinging",
"UnitEntryPoint.cpp",MB_OK);
case DLL_THREAD_ATTACH:
MessageBox (0,"Thread has attached to DLL",
"UnitEntryPoint.cpp",MB_OK);
case DLL_THREAD_DETACH:
MessageBox (0,"Thread has detached from DLL",
"UnitEntryPoint.cpp",MB_OK);
case DLL_PROCESS_DETACH:
MessageBox (0,"Process has detached from DLL",
"UnitEntryPoint.cpp",MB_OK);
}
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.