Search code examples
sql-serverdelphi-2010sqlclrsql-server-2017

how to use Delphi DLL in SQL Server assembly


Delphi DLL

library SqlDll;

uses
  System.SysUtils,
  System.Classes;

{$R *.res}

function SqlQuery(_a, _b: Integer): Integer; stdcall; export;
begin
  Result := _a + _b;
end;

exports
   SqlQuery;

begin

end.

SQL Server

DROP ASSEMBLY CalculateDelphi
GO
CREATE ASSEMBLY CalculateDelphi 
FROM 'C:\SqlDLL\SqlDll.dll' 
WITH PERMISSION_SET = unsafe  
GO

Error

Msg 6544, Level 16, State 1, Line 153 CREATE ASSEMBLY for assembly 'CalculateDelphi' failed because assembly 'CalculateDelphi' is malformed or not a pure .NET assembly. Unverifiable PE Header/native stub.


Solution

  • The error message is self explanatory:

    'CalculateDelphi' failed because assembly 'CalculateDelphi' is malformed or not a pure .NET assembly.

    SQL Server wants a .NET assembly, but you are not giving it a .NET assembly, you are giving it a plain C-style DLL instead, which SQL Server does not support importing like some other databases do.

    Delphi cannot produce .NET assemblies anymore, it lost that ability when Delphi.NET was dropped after Delphi 8.

    For what you are trying to do, you will have to use a .NET capable compiler, like Visual Studio, RemObjects Oxygene, etc to either:

    • create a .NET assembly wrapper that calls into your DLL internally.

    • port the DLL code to compile directly as its own .NET assembly.