How to Use TN BRIDGE Integration Pack for Delphi: Step‑by‑Step Tutorial
This tutorial shows a practical, step‑by‑step process to install, configure, and use the TN BRIDGE Integration Pack for Delphi to expose Delphi classes/methods to .NET or other supported runtimes and consume external services from Delphi. Assumptions: you have Delphi (XE or later) installed and a working Windows development environment. If your Delphi version is different, the steps still apply with minor adjustments.
1. What TN BRIDGE Integration Pack does
- Bridge role: Enables calling between Delphi native code and managed runtimes (like .NET) and simplifies exposing Delphi functionality as services/objects consumable by other platforms.
- Common uses: Create Delphi components accessible from .NET, expose business logic as services, or call external .NET libraries from Delphi.
2. Prerequisites
- Delphi IDE installed (XE or later recommended).
- TN BRIDGE Integration Pack installer/license.
- Administrative rights for installation.
- Basic familiarity with Delphi projects, units, and packages.
3. Install TN BRIDGE Integration Pack
- Run the TN BRIDGE installer as Administrator.
- Follow the installer prompts; choose the Delphi version to integrate with when prompted.
- Allow the installer to register packages with the Delphi IDE (it will add design-time components and menu entries).
- Restart Delphi after installation completes.
4. Create a Delphi library (DLL) to expose
- In Delphi, choose File → New → Dynamic-Link Library.
- Add a new unit (File → New → Unit) and define the functions/classes you want to expose. Example exported function:
pascal
library MyBridgeLib; uses System.SysUtils, System.Classes; function AddNumbers(a, b: Integer): Integer; stdcall; begin Result := a + b; end; exports AddNumbers; begin end.
- Build the DLL. Note the output path for later registration.
5. Use TN BRIDGE tools to register Delphi types
- Open the TN BRIDGE project or utility from the Delphi menu added by the installer (e.g., Bridge Registration Wizard).
- Create a new Bridge registration profile and point it at your DLL or Delphi package.
- Define the exported functions/classes, specifying calling conventions, parameter types, and any marshaling rules (strings, records, arrays).
- Generate the bridge metadata/stubs — TN BRIDGE will produce the necessary interop wrappers or .NET assembly proxies.
6. Consume the Delphi components from .NET (example)
- In Visual Studio, add a reference to the generated proxy assembly (from TN BRIDGE).
- Use the exposed types in C#:
csharp
using MyBridgeProxy; class Program { static void Main() { var result = NativeMethods.AddNumbers(3, 4); Console.WriteLine(result); // 7 } }
- Ensure the Delphi DLL is deployed alongside the .NET executable or is accessible via PATH.
7. Call .NET libraries from Delphi (reverse)
- Use the TN BRIDGE wizard to generate Delphi wrappers for a .NET assembly.
- Place the .NET assembly path in the bridge profile, let TN BRIDGE generate Delphi import units or COM-like proxies.
- In Delphi, add the generated unit to your project and call managed methods as if they were native:
pascal
uses System.SysUtils, NetAssemblyProxy; procedure Test; var svc: TManagedService; begin svc := TManagedService.Create; try ShowMessage(svc.DoWork(‘input’)); finally svc.Free; end; end;
- Configure hosting: either use an out-of-process host or ensure the CLR is properly initialized by the bridge runtime.
8. Marshaling tips & common pitfalls
- Strings: Confirm whether ANSI/Unicode marshaling is required; prefer Unicode (UTF-16) with recent Delphi versions.
- Records/Structs: Use explicit layout and fixed-size fields to match the managed struct layout.
- Memory ownership: Establish clear rules who allocates/frees memory across the boundary; provide explicit free functions if needed.
- Calling conventions: Match stdcall/cdecl as expected by consumers.
- Threading: Ensure long-running Delphi code does not block the host runtime’s UI thread; marshal callbacks to the appropriate thread.
9. Debugging and testing
- Use logging on both sides (Delphi and managed) to trace calls.
- Test with simple functions first (integers, strings) before moving to complex types.
- Use dependency tools (e.g., Process Monitor) to ensure the DLL is loaded and paths are correct.
- If proxies fail to load, check .NET assembly versions and platform target (x86 vs x64) consistency with Delphi runtime.
10. Deployment checklist
- Matching bitness: ensure Delphi DLL and consuming process share x86 or x64.
- Include TN BRIDGE runtime files if required by the generated proxies.
- Register any COM/Registration-free components if used.
- Verify licensing/redistributable requirements from TN BRIDGE.
11. Example: Exposing an object with methods
- Define a Delphi class and exported creation/destruction functions:
pascal
type TCalc = class function Multiply(a,b: Integer): Integer; end; function CreateCalc: Pointer; stdcall; begin Result := Pointer(TCalc.Create); end; procedure FreeCalc(inst: Pointer); stdcall; begin TObject(inst).Free; end; function CalcMultiply(inst: Pointer; a,b: Integer): Integer; stdcall; begin Result := TCalc(inst).Multiply(a,b); end;
- Register these in TN BRIDGE and generate proxies so managed code can instantiate and call methods.
12. Further resources
- TN BRIDGE Integration Pack documentation and sample projects (installed with the pack).
- Delphi and .NET interoperability articles and MSDN/Embarcadero docs for advanced marshaling.
Following these steps will get a working interop setup between Delphi and managed runtimes using TN BRIDGE. For production use, expand tests, add error handling, and formalize memory/threading rules across the boundary.
Leave a Reply