简体   繁体   English

.NET Framework类如何引用本机Windows DLL而不会变得特定于位?

[英]How do .NET Framework classes reference native Windows DLLs without becoming bitness-specific?

I've read many questions and answers indicating that if I want to link my C# project against native libraries, I can't use AnyCPU platform target but must make separate 32- and 64-bit builds, each linked against the native DLL of the appropriate bitness. 我已经阅读了很多问题和答案,表明如果我想将C#项目与本机库链接,我不能使用AnyCPU平台目标,但必须制作单独的32位和64位版本,每个版本都链接到本机DLL。适当的位数。

This makes me wonder how the .NET Framework assemblies themselves are, or at least appear to be, built for AnyCPU . 这让我想知道.NET Framework程序集本身是如何构建的,或者至少看起来是为AnyCPU That is, when adding a reference to my GUI application, why don't I have to pick the 32-bit or 64-bit version of System.Windows.Forms ? 也就是说,在添加对我的GUI应用程序的引用时,为什么我不必选择3​​2位或64位版本的System.Windows.Forms I thought this might just be some Visual Studio magic that would resolve to the appropriate GAC subdirectory (GAC_32 or GAC_64), but I searched for System.Windows.Forms.dll in the GAC and found it in: 我认为这可能只是一些Visual Studio魔法会解析到相应的GAC子目录(GAC_32或GAC_64),但我在GAC中搜索了System.Windows.Forms.dll并在以下位置找到它:

C:\\Windows\\Microsoft.NET\\assembly\\GAC_MSIL\\System.Windows.Forms\\v4.0_4.0.0.0__b77a5c561934e089\\System.Windows.Forms.dll C:\\ WINDOWS \\ Microsoft.NET \\装配\\ GAC_MSIL \\ System.Windows.Forms的\\ v4.0_4.0.0.0__b77a5c561934e089 \\ System.Windows.Forms.dll中

Note the "GAC_MSIL". 请注意“GAC_MSIL”。 So how does this DLL manage to wrap a native 32-bit API yet remain linkable in a 64-bit application? 那么这个DLL如何设法包装本机32位API,但仍然可以在64位应用程序中链接? And why can't I use a similar strategy to make a single C# DLL that links against a native 32-bit library yet remains runnable in 64-bit mode? 为什么我不能使用类似的策略来制作一个链接本机32位库但仍然可以在64位模式下运行的C#DLL?

Option 1: In GAC you may register 2 versions of assembly one 32 and one 64 bit with exactly same names. 选项1:在GAC中,您可以注册两个版本的程序集,一个32位,一个64位,名称完全相同。 Oracle DB Driver for .NET uses this strategy. 适用于.NET的Oracle DB Driver使用此策略。

Option 2: With your assembly that will be AnyCPU deploy two versions of native DLL and choose proper DLL at runtime (SQLite works like that). 选项2:使用您的程序集将AnyCPU部署两个版本的本机DLL并在运行时选择适当的DLL(SQLite就是这样)。 As it turns out .NET Framework is intelligent enough to load proper version of native DLL via P/Invoke ( Using a 32bit or 64bit dll in C# DllImport ) 事实证明,.NET Framework足够智能,可以通过P / Invoke加载适当版本的本机DLL( 在C#DllImport中使用32位或64位dll

I had the same problem and ended up using Fody Costura 我遇到了同样的问题,最终使用了Fody Costura

DLL Files will be shipped as embedded ressources and the lib takes care of the bitness. DLL文件将作为嵌入式资源发布,而lib则负责处理这些内容。

You could find an example for SQLite here 你可以在这里找到一个SQLite的例子

The problem I have encountered was that your application needs to have access to the Windows Temp folder to create the assemblies from the ressource. 我遇到的问题是您的应用程序需要访问Windows Temp文件夹才能从ressource创建程序集。 If you dont need it, you could disable it using a config setting createtemporaryassemblies 如果您不需要它,可以使用配置设置createtemporaryassemblies禁用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于本机 C++ 和 .NET 的 Windows DLL 是否相同? - Are the Windows DLLs used for native C++ and .NET the same? 我如何将我的 Windows 窗体与 .NET 框架链接到 Steam 而不会出现构建错误 - how do i link my windows form with .NET framework to steam without build errors 如何在 .NET Core 项目中引用 .NET Framework 项目? - How do I reference a .NET Framework project in a .NET Core project? 构建DLL时,COM如何补充.NET Framework? - How does COM complement the .NET Framework when building DLLs? 在项目中“添加为链接”时如何引用dll? - How do I reference dlls when “added as link” in project? 我必须参考哪些dll才能访问.net Framework 4.5中的JsonArray - What dlls must i reference to get access to JsonArray in .net framework 4.5 如何使用 COM 参考构建 .NET Windows 库,它可以针对 .NET Framework 4.8 和 .NET 6? - How to build a .NET Windows library using a COM reference, that could target either .NET Framework 4.8 and .NET 6? 在Windows Phone 7的c#.net Framework 2.0中调用WebService而不添加Web引用 - Calling WebService Without adding web reference in c#.net framework 2.0 for windows phone 7 没有.Net框架的基于Windows的应用程序如何运行 - How windows based applications run without .Net framework 将 .net 核心与旧的 .net 框架 dll 一起使用 - Use .net core with legacy .net framework dlls
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM