开发者

What is a mixed mode assembly?

I am looking at the System.Data.SQLite download page, and it lists mixed mode assembly for .NET 4 and a regular (I assume). My project that is going to use this library is all .NET 4 which will be compiled to x86.

I have 2 questions:

  1. What is a mixed-mode assembly? Google returns a confusing array of answers, none of which make a lot of sense. One answer states that it's all about mixing native and manage开发者_StackOverflow中文版d code, while others claim its for mixing .Net versions.
  2. Which download should I get for my situation?


http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

1) Allways check msdn first. Mixed mode means that the assembly can run managed and unmanaged code.

2) Setups for 32-bit Windows (.NET Framework 4.0)

http://system.data.sqlite.org/sqlite-netFx40-setup-bundle-x86-2010-1.0.74.0.exe

You kinda answered that question yourself "My project that is going to use this library is all .NET 4 which will be compiled to x86."


There is a way to produce a mixed-mode assembly from pure IL. The final part of creating an assembly using reflection involves a call to AssemblyBuilder.Save. If you just pass PortableExecutableKinds.Required32Bit for the portableExecutableKind argument then you will create a mixed-mode assembly. You can avoid the problems this causes by also passing PortableExecutableKinds.ILOnly. i.e.


    assembly_bldr.Save(exe_name, PortableExecutableKinds.Required32Bit | PortableExecutableKinds.ILOnly, ImageFileMachine.I386 );

Needless to say, this information was hard won...


Taken from the official FAQ

(14) What is a mixed-mode assembly?

A mixed-mode assembly is a dynamic link library that contains both managed code and native code for a particular processor architecture. Since it contains native code it can only be loaded into a process that matches the processor architecture it was compiled for. Also see this StackOverflow question.

It even references this question!


For SQLite, the bundle contains a mixed-mode assembly for System.Data.SQLite; however, the default package contains two separate assemblies, one completely native, and the other an interop assembly which is completely managed.

There's actually a FAQ for this on the SQLite site.

(15) What is a "bundle" package (i.e. from the download page)?

The "bundle" packages listed on the download page contains the System.Data.SQLite mixed-mode assembly in a file named "System.Data.SQLite.dll" (see question #14) instead of separate "System.Data.SQLite.dll" and "SQLite.Interop.dll" files to contain the managed code and native code, respectively.

And the previous FAQ:

(14) What is a mixed-mode assembly?

A mixed-mode assembly is a dynamic link library that contains both managed code and native code for a particular processor architecture. Since it contains native code it can only be loaded into a process that matches the processor architecture it was compiled for. Also see this StackOverflow question.

Why are there two options?

Based on my research, the non-bundled form is available so that you can use P/Invoke in managed code (e.g. C#) to call into unmanaged code, rather than using IJW. IJW is magically better, and the mechanism to compile a C++ program into separate managed and unmanaged DLLs like this (so that P/Invoke even applies) is deprecated as of VS 2015, so newer versions of SQLite might drop the non-bundled package.

You should double-check my research, as I don't have direct evidence of anything in the preceding paragraph. Here's my research:

  • https://learn.microsoft.com/en-us/cpp/dotnet/mixed-native-and-managed-assemblies?view=vs-2019
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/interoperability-overview
  • https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/8977675-do-not-remove-clr-pure-and-clr-safe-from-c-cli
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜