![developer16 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/developer16.jpg?resize=1140%2C694&ssl=1)
To this end, the sample uses the CliHandle and SharedCliHandle properties of the TFDConnection class.
Table of Contents
Location
You can find the DLLSharing sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
Object PascalDatabaseFireDACSamplesComp LayerTFDConnectionDLL_Sharing
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
How to Use the Sample
- Navigate to the location given above and open
Project1.dproj
. - Press F9 or choose Run > Run.
![DLLSharing | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/11/DLLSharing.png?resize=750%2C405&ssl=1)
Files
File in Delphi | Contains |
---|---|
Project1.dproj Project1.dpr | The project itself. |
Unit1.pas Unit1.fmx | The main form. |
Project2.dproj Project2.dpr | The DLL. |
Unit2.pas Unit2.fmx | The DLL form. |
Implementation
The sample uses FireDAC to implement a connection with a dynamic-link library. To this end, the sample implements three buttons named: Button1, Button2 and Button3. Each button handles a OnClick event to implement the following features:
- Load a DLL
- Show data
- Unload the DLL
Load DLL
To transfer a connection between an application and a DLL, the sample transfers the TFDCustomConnection.CliHandle property value to the DLL. The handle must be assigned to the TFDCustomConnection.SharedCliHandle property.
After the TFDCustomConnection.SharedCliHandle is assigned, the DLL connection can be activated by setting the Connected property to True
. Note that there is no need to set up a connection definition, including DriverID.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<em>// Application code:</em> FhDll: THandle; <em>// ...</em> procedure TForm1.Button1Click(Sender: TObject); begin FhDll := LoadLibrary(PChar('Project2.dll')); @FpShowData := GetProcAddress(FhDll, PChar('ShowData')); end <em>// DLL code:</em> class procedure TForm2.ShowData(ACliHandle: Pointer); var oForm: TForm2; begin oForm := TForm2.Create(Application); oForm.FDConnection1.SharedCliHandle := ACliHandle; oForm.FDConnection1.Connected := True; oForm.FDQuery1.Active := True; oForm.Show; end; |
Show data
Then, the connection can be used as a normal database connection:
1 2 3 4 5 6 7 8 |
TShowDataProc = procedure (ACliHandle: Pointer); stdcall; <em>// ...</em> FpShowData: TShowDataProc; <em>// ...</em> procedure TForm1.Button3Click(Sender: TObject); begin FpShowData(FDConnection1.CliHandle); end |
Unload DLL
To unload the dynimic-link library, the sample implements the following code:
1 2 3 4 5 6 7 8 9 |
TShutdownProc = procedure; stdcall; <em>// ... </em> FpShutdown: TShutdownProc; <em>// ...</em> procedure TForm1.Button2Click(Sender: TObject); begin FpShutdown(); FreeLibrary(FhDll); end; |
Head over and check out the full sample source code for DLL sharing with FireDAC in Delphi.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition