Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

c# - SQL Server: "CREATE ASSEMBLY for assembly 'Test' failed because assembly 'Test' is malformed or not a pure .NET assembly."

I get this error when I attempt to load a mixed mode C++/CLI assembly into SQL Server 2012:

CREATE ASSEMBLY [Test]
AUTHORIZATION [dbo]
from 'H:est.dll'
WITH PERMISSION_SET = SAFE

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is impossible to load a mixed mode C++/CLI assembly into SQL Server.

You can only load a "pure" C++/CLI assembly:

enter image description here

However, there may be a way around this. Use P/Invoke in your C# file. This is equivalent to writing a mixed mode C++/CLI assembly wrapper, because when CLR code calls native code in your assembly, it performs an implicit P/Invoke (let me know if this works for you).

Why does SQL server prevent mixed mode assemblies from being loaded?

This is my theory:

If we go back to 2005, things were different. When they released the first version of SQL Server to support the CLR, mixed mode assemblies would sometimes deadlock on a load. See Microsoft - Mixed DLL Loading Problem. Even after they fixed this issue, there was still arguments centered around security and stability which tipped the scales against allowing loading of mixed mode C++/CLI assemblies into the core of SQL server.

If anyone on the original Microsoft design team can fill in the blanks, I'd be very curious!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...