3

I am creating a class library, which I hope to put on NuGet eventually. Right now, it is targeting AnyCPU.

I now want to PInvoke into a DLL, which comes in both 32 and 64 bit versions. Initially, I thought I'd just use 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 naturally wouldn't work for a NuGet project.

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

Saqib
  • 7,242
  • 7
  • 41
  • 55

1 Answers1

3

You can check platform at runtime and PInvoke into different DLLs.

static void NativeFuncWrapper(){
    if(Environment.Is64BitProcess){
        NativeFuncWrapper64(); //this calls 64-bit dll
    }else{
        NativeFuncWrapper32(); //this calls 32-bit dll
    }
}

If you want it to work without Environment.Is64BitProcess, read How to know a process is 32-bit or 64-bit programmatically for alternative methods.

Community
  • 1
  • 1
cshu
  • 5,654
  • 28
  • 44