C #: access to 32-bit / 64-bit DLL depending on the platform

we use a self-written 32-bit C ++ DLL from our C # applications. Now we noticed that when C # applications run on a 64-bit system, the 64-bit runtime is automatically used, and, of course, the 32-bit DLL cannot be accessed from the 64-bit version.

My question is: is there a way to use a 32 bit DLL? If not, if I created a 64-bit version of the DLL, would it be easy to let the application choose which one is P / Invoke?

I am going to create two helper classes in C #: one that imports functions from a 32-bit DLL and imports from a 64-bit DLL, and then creates a wrapper class with one function for each imported function that calls either the 32-bit importer or 64-bit importer, depending on the "bitty" OS. Will this work?

Or is there another easy way to do something?

+5
source share
4 answers

You need to make sure that you only use P / Invoke calls for the 64-bit DLL when compiling on the 64-bit version.

- "" ( ) 2 , 32- 64-. factory IntPtr.

"AnyCPU" , , , DLL P/Invoke .

+6

, 64-, 32- DLLS, IntPtr.Size, , .

if (IntPtr.Size == 8)
{
    Helper.SomeMethod64();
}
else
{
    Helper.SomeMethod32();
}
+3

.Net x86,

+1

, Unrar.dll 32 64 .

:

a)

#if x64
// ... define all x64 imports here
#else
// ... define all x86 imports here
#endif

32 64 .

b) Another way is to create an interface for import and implement 32-bit and 64-bit versions separately.

If 32 bits, create an instance of the 32-bit implementation, still create the implementation of the 64-bit version.

+1
source

All Articles