Here is the solution I used in many projects:
- name the 32-bit assembly with the β32-bit oriented nameβ. For example, MyAssembly.Native.x86.dll
- name the 64-bit assembly with the "64-bit oriented name". For example, MyAssembly.Native.x64.dll
- compile the managed assembly as "Any Cpu"
- send everything in one way
This is how I declare P / Invoke methods:
[DllImport("MyAssembly.Native.x86.dll", EntryPoint = "MyTest")] private static extern void MyTest86(MyType myArg); [DllImport("MyAssembly.Native.x64.dll", EntryPoint = "MyTest")] private static extern void MyTest64(MyType myArg);
And here is the corresponding function "MyTest", which I will always use (others are here only for the correct binding of the bit). It has the same signature as other P / Invoke:
public static void MyTest(MyType myArg) { if (IntPtr.Size == 8) { MyTest64(myArg); return; } MyTest86(myArg); }
Benefits:
- you can send all binaries (dll, exe, ...) to the same path
- you support 32-bit and 64-bit processes and operating systems with the same file location
- You do not need to resort to Win32 apis to change the dll boot path.
Disadvantages:
- you will have 3 method declarations for 1 'real' method
- you will lose some processor cycles due to a bit test
- depending on your context, sometimes you cannot change the names of the native DLLs, so you simply cannot do this.
Simon Mourier Apr 22 '14 at 10:24 2014-04-22 10:24
source share