- eine DLL kann auch ohne *.lib-Datei in ein BC++-Projekt eingebunden werden (z.B. Delphi-DLLs)
- wichtige Befehle: LoadLibrary(), GetProcAddress(), FreeLibrary()
- Im Beispiel wird eine Delphi-DLL geladen, deren Funktion „Addiere“ aufgerufen, die 2 int-Werte verlangt. Das Ergebnis der Addition wird in einem Label dargestellt.
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <FileCtrl.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // Von der IDE verwaltete Komponenten
TButton *Button1;
TLabel *Label1;
TButton *Button2;
TButton *Button3;
TEdit *Edit1;
TEdit *Edit2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // Anwender-Deklarationen
HINSTANCE DllInstance; // Instanz auf die DLL deklarieren
public: // Anwender-Deklarationen
__fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1;
#endif
#include <vcl.h>
#pragma hdrstop
#include "windows.h" // Wichtig!!
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!DllInstance) DllInstance = LoadLibrary("DllName.dll"); // DLL laden
if (DllInstance) Form1->Caption = "DLL wurde geladen.";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if (DllInstance)
if (FreeLibrary(DllInstance)) DllInstance = NULL; // DLL entladen
if (!DllInstance) Form1->Caption = "DLL nicht geladen.";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Funktionstyp deklarieren
typedef int (__stdcall *IMPFUNC) (int, int);
// würde die Funktion 2 Floatwerte benötigen und als Ergebnis einen Bool-Wert liefern
// sähe die typdef-Deklaration folgendermaßen aus:
// typedef bool (__stdcall *IMPFUNC) (float, float);
// Deklaration für die DLL-Funktion
IMPFUNC DllFunktion;
int iSumme;
if (DllInstance)
{
// Addresse der Funktion "Addiere" in der DLL herausfinden
// Falls die Funktion nicht gefunden wird, einfach vor den
// Funktionsnamen ein "_" setzen
// Bsp.: statt "Addiere", "_Addiere" verwenden
DllFunktion = (IMPFUNC)GetProcAddress(DllInstance, "Addiere");
if (DllFunktion)
{
// DLL-Funktion aufrufen,
// An die DLL werden 2 int-Werte übergeben (aus Edit1 und Edit2)
// Rückgabewert ist ein int-Wert (Summe)
iSumme = DllFunktion(Edit1->Text.ToIntDef(0), Edit2->Text.ToIntDef(0));
Label1->Caption = IntToStr(iSumme);
}
else Label1->Caption = "Funktionsname existiert nicht!";
}
else
{
Label1->Caption = "Bitte DLL laden!";
}
}