Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C# AnyCPU project include a platform specific dll

Our product is based on a bunch of C++ projects but we are now using C# projects for the front end. We are also now doing a 64 bit version.
Our plan is to build all of the C# dlls as AnyCPU. The C# projects will have references to the C++ dlls in a common bin folder. When building x64 the bin folder will contain x64 versions of our c++ dlls and when building Win32 the bin folder will contain 32 bit versions of our C++ dlls. So the C# projects will be building AnyCPU but including either an x64 or Win32 c++ dll.
My question is, will this work? At runtime everything should be either all 32 or all 64 depending on which exe we are running, but can compile time handle a project targeting AnyCPU that includes a platform specific dll? Or will we have to make platform specific versions of all our C# dlls? Thanks

like image 899
John C Avatar asked Jan 18 '11 14:01

John C


People also ask

Do AC recharge cans work?

Are Air Conditioning Recharge Kits Worth It? No, they are not because they don't fix broken AC systems. Instead, they simply recharge refrigerant and leave the cause of the problem unattended. So while a recharge may get cool air blowing again, it masks the real issue as it worsens.

Do AC recharge cans have oil?

If your car is particularly old or new, it's possible your car is incompatible with DIY A/C recharge canisters commonly found at auto parts stores. Those cans typically use a mixture of R134a refrigerant, compressor oil and a leak stopping compound.

Can AC be used for heat?

The reality is that many air conditioning units can also be used to heat rooms. There are air conditioning systems on the market which can keep rooms warm in the winter, and cooler in the summer.

Are all AC recharge kits the same?

AC recharge kits come in a variety of prices and capabilities. Some are just refrigerant in a can with a hose and a gauge. Others include supplies to diagnose issues beyond a low refrigerant pressure and make further repairs. Some of those can even be used to diagnose and recharge home HVAC systems.


2 Answers

This is primarily a deployment problem, getting the right DLLs selected for the right operating system. Pretty straight-forward if you create two setup projects, one for x86 and another for x64.

Making it work transparently is possible too. You'd create, say, an x86 and an x64 sub-directory in the directory that contains your EXE and put respectively the 32-bit and the 64-bit builds of the DLLs in those sub-directories. At startup, check IntPtr.Size to know the bit-ness of your process. Then pinvoke SetDllDirectory accordingly so that Windows will find the correct DLL. Like this:

using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
...
        public static void SetupDllDirectory() {
            string path = Assembly.GetEntryAssembly().Location;
            path = Path.Combine(path, IntPtr.Size == 8 ? "x64" : "x86");
            bool ok = SetDllDirectory(path);
            if (!ok) throw new System.ComponentModel.Win32Exception();
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetDllDirectory(string path);

Use a post build event to copy the DLLs. Using Environment.SetEnvironmentVariable() to append the directory to the PATH environment variable is yet another approach.

like image 78
Hans Passant Avatar answered Oct 18 '22 09:10

Hans Passant


As far as I remember, this works.

I had only 32bit DLLs, c# compiled and it crashed on startup. So if you put there 64bit DLLs, I think you don't need to recompile C#.

like image 45
Al Kepp Avatar answered Oct 18 '22 08:10

Al Kepp