1. 程式人生 > >C# 自適應呼叫64位 32位DLL

C# 自適應呼叫64位 32位DLL

參考網上的經驗交流,加上自己的實踐總結,分享給大家,紀念兩天的奮鬥歷程。

1、標準.NET的DLL,檢查編譯方式,確保為 ANY CPU,即可自適應。

2、標準SDK的DLL

    編譯兩個DLL,一個WIN32,一個X64
    用別名方式聲明後在C#中封裝

    static bool is64bit = (IntPtr.Size == 8);
            [DllImport("t64.dll", EntryPoint = "test")]
    static extern int test64(byte[] contextArray);
    [DllImport("t32.dll", EntryPoint = "test")]
    static extern int test32(byte[] contextArray);
    public static int test(byte[] contextArray)
    {
        return is64bit ? test64(contextArray) : test32(contextArray);
    }

3、混合型編譯的DLL

    一個外殼DLL,兩個具體平臺的DLL
    以最常見的 system.data.sqlite.dll 為例

    按以下目錄結構釋出即可
    system.data.sqlite.dll
        x64\SQLite.Interop.dll
        x86\SQLite.Interop.dll