2

I try to develope an C# Interface for using an USB Hardware Device. I access the API DLL Via PInvoke pattern from the manufacturer. There are two DLLs with the same name. But one is for 32Bit Systems and the other one for 64Bit Systems. I want that my application uses the right API for each system.

So i start checking wich platfrom is in use:

bool is64Bit = System.Environment.Is64BitOperatingSystem

I defined a string variable and set the name of recommend .DLL.

like this:

string dll;
if (is64bit)
{
    dll = "APINAME64.DLL";
}
else
{
    dll = "APINAME32.DLL"
}
[DllImport(dll, SetLastError=true)]
public static extern bool ImmConfigureIME();

But this is still not working. The Compiler wants an const string for Pinvoke.

Does anybody has an idea how to solve that?

REMberry
  • 172
  • 1
  • 2
  • 12
  • What have you attempted to do after you check if its 64bit? – Yuval Itzchakov Sep 12 '14 at 07:09
  • Im not sure i understand your problem. If you know the bitness of your current system, what's stopping you from loading the proper dll? – Yuval Itzchakov Sep 12 '14 at 07:13
  • Some discussion here: http://stackoverflow.com/questions/1573724/cpu-architecture-independent-p-invoke-can-the-dllname-or-path-be-dynamic/1867081#1867081 – Matthew Watson Sep 12 '14 at 07:42
  • 1
    Give them the same name so your [DllImport] declaration doesn't have to change. Just rename the file. Then you just need to help Windows to find the correct DLL. The subject of many questions here. – Hans Passant Sep 12 '14 at 08:18

1 Answers1

2

You can declare both DLLs inside your code and give them different names. Then, use the EntryName property to make sure they have the right entry point for the native dll:

[DllImport("APINAME64.dll", EntryName="ImmConfigureIME" SetLastError=true)]
public static extern bool ImmConfigureIME64();

[DllImport("APINAME32.dll", EntryName="ImmConfigureIME" SetLastError=true)]
public static extern bool ImmConfigureIME32();

bool is64Bit = System.Environment.Is64BitOperatingSystem;
if (is64bit)
{
    ImmConfigureIME64();
}
else
{
    ImmConfigureIME32();
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Thanks for your solution. This is a good idea. But i use more than 100 functions in the unmanaged DLL. So i need a lot of additionally lines of code for my implementation. – REMberry Sep 12 '14 at 07:33
  • 1
    Attributes are static metadata. You cant give them a run-time variable as a value. Why not simply declare all your classes inside a `NativeMethods` class, encapsulating all of them? – Yuval Itzchakov Sep 12 '14 at 07:34
  • If you absolutely have to do this at run-time. You can call `LoadLibrary` yourself. Take a look at [this](http://stackoverflow.com/questions/8836093/how-can-i-specify-a-dllimport-path-at-runtime) question. – Yuval Itzchakov Sep 12 '14 at 07:42
  • 1
    Isn't it better to call `Environment.Is64BitProcess`? The OS can be 64-bit, but the process 32-bit. – Shahin Dohan Sep 07 '20 at 07:30
  • @ShahinDohan OP wanted to load the right DLL depending on the underlying operating system support, not the current process. – Yuval Itzchakov Sep 07 '20 at 11:22