c# - Target 32 Bit or 64 Bit native DLL depending on environment -


I have a native DLL that comes in both 32-bit and 64-bit versions (x86). I want to create a cover that works on both architecture (any CPU) and loads the correct version of DLL based on the current environment (32 bit or 64 bit at runtime). This process should be done automatically so that my DLL users do not need to target a specific architecture.

Is there any best practice to do this? Any example that can guide me?

I have found a possible solution that uses a managed proxy for each architecture and then uses the Assembly.Resolve event to load the correct code. However, for this I need 3 managed assemblies in addition to 2 unmanaged libraries, which seems a bit more.

Is there any other solution?

Here's a solution I used on many projects:

  • 32-bit assembly "32-bit oriented name" For example, MyAssembly.Native.x86.dll
  • Name 64-bit assembly with "64-bit oriented name" For example, MyAssembly.Native.x64.dll
  • Collect all the assembled assemblies in the form of 'any CPU'
  • Ship everything in the same way

    Here I declare P / invention methods: / P>

      [DllImport ("MyAssembly.Native.x86.dll", entrypoint = "MyTest")] Personal static extension zero MyTest86 (MyType myArg); [DllImport ("MyAssembly.Native.x64.dll", EntryPoint = "MyTest")] Private static extension Zero MyTest 64 (MyType MyRG);   

    And here is the same 'MyTest' function which I will always use (others here are just for right biting). It has the same signature as compared to other P / Inocoids:

      Public Static Zero MyType myArg {if (IntPtr.Size == 8} {MyTest64 (myArg); Return; } MyTest86 (myArg); }   

    The advantages are:

    • You can ship all binary (DLL, XES, ...) on the same path
    • You support 32-bit and 64-bit processes and OS with the same file layout
    • You do not have support of Win32 apis to change the Dell load path

      There are inconveniences:

      • You have 3 method announcements for 1 'real' method
      • You will loose some CPU cycles due to beta testing
      • Based on your context, Sometimes you can not change the original DLL names, so you can not do it

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -