Target 32 Bit or 64 Bit native DLL depending on environment

前端 未结 3 1926
一整个雨季
一整个雨季 2020-12-02 00:53

I have a native DLL which comes in both 32 bit and 64 bit versions (x86). I want to create a wrapper which works on both architectures (Any CPU) and loads the correct versio

3条回答
  •  猫巷女王i
    2020-12-02 01:40

    The way I do it is to p/invoke a call to LoadLibrary before calling any of the p/invokes to the library.

    • Use the bitness of the executing assembly to work out which version of the unmanaged DLL to load.
    • Then call LoadLibrary to load it passing the full path to the DLL.
    • Then when you call the p/invokes, the correct DLL is already loaded into the process and the p/invokes bind to it.

    This relies on the unmanaged DLL having the same name for both 32 and 64 bit. If that's not the case then you are in trouble. In that scenario you may need to bind explicitly to the DLL by p/invoking GetProcAddress. This is no fun at all. Or you implement the sort of scaffolding that Simon describes in his answer.

提交回复
热议问题