2

我正在编写涉及使用 unrar 的代码(WinForms C# NET 3.5)。

    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchive(ref RAROpenArchiveData archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARCloseArchive(IntPtr hArcData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARProcessFile(IntPtr hArcData, int operation, [MarshalAs(UnmanagedType.LPStr)] string destPath, [MarshalAs(UnmanagedType.LPStr)] string destName);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetCallback(IntPtr hArcData, UNRARCallback callback, int userData);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetPassword(IntPtr hArcData, [MarshalAs(UnmanagedType.LPStr)] string password);

因为我希望代码同时在 32BIT 和 64BIT 上工作,所以我想根据系统位数的检查通过字符串 unrarDll 分配 UNRAR64.DLL 或 UNRAR.DLL。

    private void DllChoice() {
        if (SystemIs64Bit()) {
            sevenZipDll = "7z-x64.dll";
            unrarDll = "unrar.dll";
        } else {
            sevenZipDll = "7x-x32.dll";
            unrarDll = "unrar64.dll";
        }
    } 
    private static bool SystemIs64Bit() {
        return (IntPtr.Size == 8);
    }

抛出错误:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

有没有简单的方法解决这个问题?这样做的正确方法是什么?

4

2 回答 2

7

不 :-) 它是规范的一部分……每个平台(x86/x64)必须有两个单独的构建。您可以做的只是定义一个预处理器指令,然后执行类似的操作

#if x64
// ... define all x64 imports here
#else
// ... define all x86 imports here
#endif
于 2010-02-23T19:37:10.083 回答
4

为 unrar 导入创建一个接口,分别实现 32 位和 64 位版本。如果是 32 位,则实例化 32 位 impl,否则实例化 64 位 impl。

于 2010-02-23T19:40:50.500 回答