Sql-server – extend SQL Server with C++ or C

database-internalssql serversql-clr

So, I created a PE32+ DLL, in an attempt to load it into SQL Server,

$ file example_dll.dll 
example_dll.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows

but when I try to compile it as an assembly, with CREATE ASSEMBLY [ok] AUTHORIZATION [dbo] ... WITH PERMISSION_SET = UNSAFE; I get,

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

It as come to my attention that what Micrsoft calls CLR "safe" may just mean, .NET's C++. I know that C# is the prefered method of extending SQL Server, but regardless does Microsoft permit loading shared objects or DLL into the server at all? Can you extend SQL Server with "native code"?

Best Answer

SQLCLR only allows for managed code / pure-MSIL Assemblies. So .NET Visual C++ might work, and I think I have seen one example of it.

You can load / call unmanaged C++ DLLs in an UNSAFE C# / VB.NET Assembly, but that can be riskier than using "unsafe" .NET functionality.

In theory, any .NET language should work IF it produces a pure-MSIL Assembly, but I have yet to see working examples of this. I have seen an example of Visual C++, and I have seen an example of someone coding actual MSIL (on Stack Overflow), though I'm still doubtful as to the supposed benefit of that.

Folks have tried F#, but can't doing that as SAFE since it requires another library for F#:

F# with sqlclr in a reasonably safe way and scripted assembly

As to the updated question, which now also asks about the Extended Stored Procedure (xp) API, that should be deprecated and frowned-upon.