简体   繁体   中英

Change C# DllImport target code depending on x64/x86

I have an external c++ dll to import using DLLImport. If my application is compiling in x64 I need to import the x64 version of this dll, if it is an x86 build, I need the x86 dll.

What is the best way to achieve this?

Ideally, I'd like some preprocessor directive, but I understand this doesn't work in c#?

More info: the DLL is being imported by a project which is set to AnyCPU. A parent project is the one which determines whether the application compiles as x64 or x86. We compile both versions for different customers - and I want to share the child project in both versions.

This is primarily a deployment problem, just have your installer copy the right DLL based on the Windows version on the target machine.

But nobody ever likes to do that. Dynamically pinvoking the correct DLL's function is enormously painfully, you have to write delegate types for every exported function and use LoadLibrary + GetProcAddress + Marshal.GetDelegateForFunctionPointer to create the delegate object.

But nobody ever likes to do that. The less painful tack is to declare the function twice, giving it different names and using the EntryPoint property in the [DllImport] attribute to specify the real name. Then test at runtime which you want to call.

But nobody ever likes to do that. The most effective trick is steer Windows into loading the correct DLL for you. First thing you have to do is copy the DLL into a directory where Windows will not look for it. Best way is to create an "x86" and an "x64" subdirectory in your build directory and copy the appropriate DLL into each. Do so by writing a post-build event that creates the directories and copies the DLLs.

Then tell Windows about it by pinvoking SetDllDirectory(). The path you specify will be added to the directories that Windows searches for a DLL. Like this:

using System;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;

class Program {
    static void Main(string[] args) {
        var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        path = Path.Combine(path, IntPtr.Size == 8 ? "x64" : "x86");
        bool ok = SetDllDirectory(path);
        if (!ok) throw new System.ComponentModel.Win32Exception();
        //etc..
    }
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool SetDllDirectory(string path);
}

Do consider if having the code run in 64-bit mode is actually useful to you. It is pretty rare to need the giant virtual memory address space you get from it, the only real benefit. You still need to support the 32-bit version that needs to operate correctly in the 2 gigabyte case.

Add both the x86 and the x86_64 DLL imports with different names, then you can conditionally invoke them depending on the architecture at runtime by checking the value of Environment.Is64BitProcess (or IntPtr.size if you're using < .Net 4). This will work regardless of whether the project is built as x86, x86_64 or AnyCPU

Alternatively, set up 2 different build configurations - one that only does x86 and one that only does x86_64, give each one a conditional compilation symbol and use an #ifdef on your custom symbol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM