Dealing with a dynamic 32/64 bit binary

Location of the Prefer 32 bit checkbox

Unchecking the "prefer 32-bit" option makes your application run as 64 bits if the OS and CPU are 64 bits too. It will still run as 32 bit if either the OS or the CPU is 32 bits only. Because of this behavior, you cannot load 32 bit DLL files if the engine decides to run your application in 64 bit mode. In our case this is not a problem as the 32 bit emulation of a 64 bit Windows takes care of selecting the proper DLL file if it's a system DLL. For custom files, you either want to stick with the 32 bit option enabled, or provide 32 and 64 bit files and select at runtime. To determine how your application runs, you can use IntPtr.Size, which will be 4 or 8.

The file name used in [DllImprot("abc.dll")] must be constant. If you want a working 32+64 bit version you can set a custom DLL loading path. You can put all 32 bit DLLs into one folder and all 64 bit DLL files into another folder, then use the SetDllDirectoryW function to set the appropriate path.

.NET will delay load DLL files as soon as you enter a function that calls the DLL function. You have to set the DLL path before that happens.

Wrong:
void Main()
{
    SetDllDirectoryW("...");
    //Other code here that uses external libraries
}
Correct:
void Main()
{
    SetDllDirectoryW("...");
    RealMain();
}

void RealMain()
{
    //Other code here that uses external libraries
}

Copyright © 2021 by Kevin Gut 📧 | More services | Generated for 18.191.176.224