Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Access 32-bit/64-bit DLL depending on platform

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

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

I'm thinking of creating two helper classes in C#: One that imports the functions from the 32bit DLL and one that imports from the 64bit DLL, then creating a wrapper class with one function for each imported function that calls either the 32bit importer or the 64bit importer depending on the "bittyness" of the OS. Would that work?

Or is there another easy way to do things?

like image 552
Thorsten Dittmar Avatar asked Mar 04 '10 10:03

Thorsten Dittmar


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming languagegeneral-purpose programming languageIn computer software, a general-purpose programming language (GPL) is a programming language designed to be used for building software in a wide variety of application domains, across a multitude of hardware configurations and operating systems.https://en.wikipedia.org › wiki › General-purpose_programmi...General-purpose programming language - Wikipedia created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Labbell LabNokia Bell Labs, originally named Bell Telephone Laboratories (1925–1984), then AT&T Bell Laboratories (1984–1996) and Bell Labs Innovations (1996–2007), is an American industrial research and scientific development company owned by multinational company Nokia.https://en.wikipedia.org › wiki › Bell_LabsBell Labs - Wikipedia. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


3 Answers

You need to make sure that you're only using P/Invoke calls against a 64bit DLL when compiling in 64bit.

One option is to move all of your "methods" into a standard interface (or abstract base class), and provide 2 implementations, one 32bit and one 64bit. You can have a factory method construct the appropriate instance of the class depending on the size of IntPtr.

This allows an "AnyCPU" app to correctly, at runtime, determine which DLL to P/Invoke into, and does work.

like image 194
Reed Copsey Avatar answered Oct 04 '22 13:10

Reed Copsey


Create a helper class that wraps both 64bit and 32bit DLLS, and use IntPtr.Size to determine which to call.

if (IntPtr.Size == 8)
{
    Helper.SomeMethod64();
}
else
{
    Helper.SomeMethod32();
}
like image 26
Chris Almond Avatar answered Oct 04 '22 11:10

Chris Almond


You can flag the .Net app to only target x86 architecture

like image 31
Rowland Shaw Avatar answered Oct 04 '22 13:10

Rowland Shaw