0

I have a class that has a long list of definitions like these:

[DllImport(NativeLibraryName, EntryPoint = "FunctionName", CallingConvention = CallingConvention.Cdecl)]
public static extern void FunctionName();

The parameter "NativeLibraryName" is set by a compiler switch. However, I would like to set this parameter at run-time. My problem is that there are two different DLLs I need to consider: one is for 32-bit systems and one is for 64-bit systems and they are different in name. What I would like to achieve is to determine whether the application is run with 64- or 32-bit and use the proper DLL. I need to load the correct DLL or otherwise I run into BadImageFormatExceptions.

Switching to Delegates

I could use Reflection to generate a file that does the same as the file that is given using delegates like this. Here, I could provide the path to the correct DLL at run-time.

Renaming DLLs

I could also rename the two DLLs and have them the same name, but put them in different directories. I could then select the folder I want to load the DLL from.

Both options would work fine, but the project I am working on is a fork of an existing project and I want to change as little as possible so that I can easily merge updates from the original project into mine.

I am glad for any of your ideas.

This thread taggles a similar problem, but it does not provide a solution that is applicable to my problem and is actually one of the solutions I came up with myself and I am looking for better ways to do it.

GameDevAlien
  • 195
  • 1
  • 17
  • You could have multiple DllImport attributes, selecting one based on conditional compilation symbols. Or your could deploy the entire application and its dependencies twice, once for each bitness? – CodeCaster Nov 13 '21 at 15:03
  • This question tackles pretty much the same problem, if that can give some more insight: https://stackoverflow.com/questions/2411736/p-invoke-dynamic-dll-search-path – Dan Byström Nov 13 '21 at 15:18

0 Answers0