[英]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应用程序的引用时,为什么我不必选择32位或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.