[英]What's the harm to install a “AnyCPU” program on a 64-bit Windows using a 32-bit MSI?
[英]Best way to craft an AnyCPU assembly using either 32-bit or 64-bit native DLLs?
我為本機庫生成了綁定,但意識到導入在 32 位和 64 位之間是不同的。
不僅入口點不同,調用約定也不同:
32 位導入:
[DllImport("implot", EntryPoint = "??0ImPlotPoint@@QAE@XZ", CallingConvention = CallingConvention.ThisCall)]
64 位導入:
[DllImport("implot", EntryPoint = "??0ImPlotPoint@@QEAA@XZ", CallingConvention = CallingConvention.Cdecl)]
由於這些是編譯時常量,因此無法使用if (64bit) then... else...
這些導入。
使用序號而不是裝飾名作為入口點:
那是行不通的,因為它們不一定指向同一個出口。
將內容延遲到兩個內部類,例如 NativeMethods32、NativeMethods64:
必須在各處進行位數檢查才能發送到正確的 class,這也很乏味。
生成兩個內部托管程序集,一個 32 位,一個 64 位:
讓 AnyCPU 程序集在運行時動態加載適當的托管程序集。
是否有有效的、經過驗證的模式來解決此類問題?
我解決了使用 Roslyn 重寫整個內容以生成 AnyCPU 模塊的問題。
像這樣:
public class Whatever
{
internal partial struct __Internal
{
// keep fields
// remove methods
}
internal partial struct __Internal32
{
// remove fields
// insert 32-bit methods
}
internal partial struct __Internal64
{
// remove fields
// insert 64-bit methods
}
public static void Test()
{
if (IntPtr.Size == 4)
{
__Internal32.Test();
}
else
{
__Internal64.Test();
}
}
}
結果效果很好,平台目標可以像往常一樣切換,並且可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.