Automating .Net DLL and WSDL Imports for Delphi Developers

How to Import .Net DLLs and WSDL Services into Delphi ProjectsInteroperating Delphi with .NET components and web services is a common task when modernizing legacy applications or integrating with third‑party systems. This article walks through practical methods for importing .NET DLLs and WSDL services into Delphi projects, explains tradeoffs, and gives clear step‑by‑step examples you can adapt to your environment.


Overview and when to use each method

  • For calling native .NET libraries from Delphi applications, common approaches are:
    • Expose .NET logic as an unmanaged/native DLL (C++/CLI or mixed-mode) or as a COM-visible assembly; call it from Delphi as you would any native library or COM object.
    • Use IPC (named pipes, sockets), REST/gRPC wrappers, or a local service when tighter isolation and language/runtime separation are desired.
  • For consuming SOAP web services described by WSDL:
    • Use Delphi’s built-in SOAP client generation (WSDL Importer) to create Delphi interface units.
    • Or call SOAP endpoints manually (HTTP + XML) or via third‑party libraries.

Choice depends on performance needs, deployment constraints, development effort, and whether you can change the .NET side.


Part A — Importing .NET DLLs into Delphi

There are two main patterns to invoke .NET code from Delphi: using COM interop or exposing native/unmanaged entry points. A third approach is to create a small native wrapper around the .NET runtime.

Advantages:

  • Natural object-oriented interaction from Delphi.
  • Delphi has built-in COM support and type library importing.

Steps:

  1. Prepare the .NET assembly

    • Make classes COM-visible:
      • In C# add [ComVisible(true)] to the assembly or class, assign GUIDs with [Guid(“…”)], and mark interfaces and classes appropriately.
      • Ensure types are public and interfaces are defined explicitly.
    • Register for COM interop:
      • Either use regasm.exe to register the assembly on the target machine: regasm YourAssembly.dll /codebase /tlb
      • Or during build, set “Register for COM interop” in project settings (Visual Studio) for development machines.
  2. Create and register a type library (TLB)

    • regasm /tlb:YourAssembly.tlb YourAssembly.dll
    • Place the TLB somewhere Delphi can access it.
  3. Import the type library in Delphi

    • In Delphi IDE: Component → Import Component → Import a Type Library → select the registered TLB → generate a Pascal unit and install component/package or just use the generated unit.
    • The import generates interface declarations and CoClass wrappers you can instantiate.
  4. Use the COM object in Delphi

    • Example usage (pseudo-Delphi):
      
      var MyObj: IMyComInterface; begin MyObj := CoMyComClass.Create; ShowMessage(MyObj.SomeMethod('input')); end; 

Notes and pitfalls:

  • Versioning: COM registration ties to machine registry; use GUIDs and careful version strategy.
  • 32-bit vs 64-bit: Ensure the bitness of Delphi app matches the registered COM server or use out‑of‑process COM servers to bridge.
  • Security/permissions: regasm may require administrative rights.

2) Expose native entry points (P/Invoke style)

If you can modify the .NET side, you can export C-style unmanaged functions from a .NET assembly using C++/CLI or the Unmanaged Exports technique (DllExport) to create native-callable functions.

Approach A — C++/CLI mixed-mode DLL:

  • Create a C++/CLI project that references the .NET assemblies and provides extern “C” exported functions that forward calls to managed code.
  • Build the DLL as a native library with exported functions.
  • Call those exported functions from Delphi via standard external declarations.

Delphi example declaration:

function CreateManagedObject(param: PAnsiChar): Pointer; cdecl; external 'ManagedBridge.dll'; 

Approach B — Unmanaged Exports for C# (third-party library):

  • Use libraries like DllExport or RGiesecke.DllExport to annotate C# methods that should be exported.
  • This approach can be brittle across .NET versions and tooling; mixed-mode C++/CLI is more robust for production.

Pros/cons:

  • Pros: Direct native calls, good performance, easy to deploy a single DLL file.
  • Cons: Requires additional build step and knowledge of C++/CLI or export tooling; more fragile across CLR versions.

3) Hosting the CLR or using a native wrapper

If you need finer control or want to load multiple CLR versions, you can host the CLR from Delphi (call into mscoree.dll / hostfxr / CoreCLR hosting APIs) or create a small native host that loads .NET Core/5+/6+ runtime and provides a C API. This is advanced but allows side‑by‑side CLR hosting and better control over lifetime and memory.

Key points:

  • .NET Core/.NET 5+ uses hostfxr and native hosting APIs (look up hosting samples) to call managed functions from native code.
  • You typically write a small native shim in C/C++ that Delphi calls.

Part B — Importing WSDL SOAP Services into Delphi

Delphi provides tools to generate client stubs from WSDL. Modern SOAP services often use WS-* or complex types; the Delphi importer handles many cases but sometimes requires manual adjustments.

1) Use Delphi’s WSDL Importer (IDE or command-line)

Steps in the IDE:

  1. File → New → Other… → WebServices → WSDL Importer (or use WSDL Importer under Tools depending on version).
  2. Enter the WSDL URL or local file path.
  3. The importer generates a unit containing interface types, data classes, and an HTTP/SOAP transport implementation (often THTTPRIO based).
  4. Use the generated interface to call operations.

Example (after import):

var   svc: IMyServicePortType; begin   svc := GetIMyServicePortType(False, '', 'http://example.com/service');   result := svc.SomeOperation('param1'); end; 

Command-line:

  • WSDLIMP utility (e.g., WSDLIMP.exe) can be used to generate units in automated builds.

Notes and troubleshooting:

  • If WSDL references XSDs or imports other WSDLs, ensure all referenced files are accessible.
  • Complex schema constructs (xsd:any, certain choice sequences, arrays with unusual wrappers) may require manual edits to the generated unit.
  • For WS-Security or custom headers, you’ll need to extend THTTPRIO with custom invoker/handlers or set HTTP headers manually via HTTPRIO.HTTPWebNode.

2) Using SOAP with Authentication, MTOM, or Attachments

  • Basic auth: set HTTP headers or use THTTPReqRespHandler and set credentials on HTTPRIO.
  • WS-Security: Delphi doesn’t provide full WS-Security out of the box — use custom SOAP headers or a proxy service that handles security.
  • MTOM/attachments: the built-in support is limited; attachments often require manual handling or a custom transport.

3) Alternative — Use REST or XML over HTTP

If you control the service, prefer REST/JSON for easier cross-language compatibility. When only SOAP/WSDL is available, a lightweight proxy converting SOAP to REST can simplify Delphi clients.


Troubleshooting and common issues

  • Mismatched bitness: 32‑bit Delphi apps cannot load 64‑bit DLLs (and vice versa). Ensure the .NET wrapper / COM registration matches the app architecture.
  • Missing dependencies: Native wrappers may rely on MSVC runtimes or .NET runtime presence; deploy prerequisites or produce self-contained hosting.
  • Data marshalling differences: Complex .NET types (generics, tuples, delegates, events) don’t map directly to Delphi — expose simple classes/interfaces or use serialization (JSON/XML).
  • Exception handling: Exceptions thrown in .NET should be translated to error codes or wrapped so that Delphi can handle them safely.
  • Versioning and deployment: COM registration ties installations to machine registry; consider side‑by‑side strategies or registry-free COM alternatives when deploying.

Example scenarios

  1. Quick integration with control over .NET source:
  • Make the .NET assembly COM-visible, register with regasm, import the TLB in Delphi, call methods directly.
  1. High-performance native calls:
  • Implement C++/CLI bridge exposing extern “C” functions, call those from Delphi via external declarations.
  1. Consume third‑party SOAP service:
  • Use Delphi’s WSDL importer to generate stubs and call the service via THTTPRIO. If WS-Security required, add a small proxy or implement custom headers.

Deployment checklist

  • Match bitness (x86/x64) for DLLs and COM servers.
  • Ship required runtimes (.NET framework or .NET runtime/hosting files).
  • If using COM, register or use registration‑free COM where possible.
  • Test across environments: development, staging, target OS versions.
  • Include error logging on the .NET side to diagnose marshalling/runtime issues.

Conclusion

Importing .NET DLLs and WSDL services into Delphi projects is fully feasible with several practical options:

  • Use COM interop for natural object mapping when you can change the .NET assembly.
  • Use native exported entry points or a mixed‑mode bridge for direct calls and better performance.
  • For SOAP services, use Delphi’s WSDL importer and extend transports for advanced security or attachments.

Which approach suits you depends on control of the .NET code, deployment constraints, and how tightly you need the two runtimes to integrate. If you tell me your Delphi version, target platform (x86/x64), and whether you can modify the .NET code, I can provide a tailored step‑by‑step example and code snippets.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *