C # and PInvoke in 32/64 bit DLL

I am creating a class library that I hope to end up wearing in NuGet. It is currently targeting AnyCPU.

Now I want to PInvoke in a DLL that comes in both 32 and 64 bit. I originally thought I was just using the 32 bit version, but this threw a BadImageFormatException. I changed the library to target X86 only, and while this works, it requires the caller to be a 32-bit process too. This will naturally not work for a NuGet project.

Appreciate any thoughts on how to work with 32/64 bit versions of the native library and how to package them in NuGet (prefer not to have 2 different assemblies).


source to share

1 answer

You can check the platform at runtime and PInvoke in different DLLs.

static void NativeFuncWrapper(){
        NativeFuncWrapper64(); //this calls 64-bit dll
        NativeFuncWrapper32(); //this calls 32-bit dll


If you want it to work without Environment.Is64BitProcess

, read How to find out a process 32-bit or 64-bit programmatic option for alternate methods.



All Articles