Has anyone solved this problem?
Yes, Microsoft. They had to solve this same problem for their SQL Server Compact package. It has a single managed assembly, System.Data.SqlServerCe.dll, acting as an adapter for the managed program and a bunch of native DLLs that implement the real database engine. The way they bind to local DLL entry points is not what I ever recommend, I will work on the assumption that you are using pinvoke. I recommend using this method so that the client program can start AnyCPU.
Echoing the gist of this post, you want to execute this code somewhere in an initialization method or static constructor:
public static void SetupDatabaseBinaries() { var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); path = Path.Combine(path, IntPtr.Size == 8 ? "amd64" : "x86"); bool ok = SetDllDirectory(path); if (!ok) throw new System.ComponentModel.Win32Exception(); } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool SetDllDirectory(string path);
The only non-trivial step is to force the user project to copy its own DLLs into the amd64 / x86 project directories. This requires a post-assembly step, as the Compact package does. You will want to download the Nuget package to see how they did it, this is not very trivial. Launch VS, create a dummy project in console mode and get the Nuget package for Sql Server Compact. After installing it, use the tab "Projects + Properties", "Build Events" and note how she added this event after assembly:
if not exist "$(TargetDir)x86" md "$(TargetDir)x86" xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\x86\*.*" "$(TargetDir)x86" if not exist "$(TargetDir)amd64" md "$(TargetDir)amd64" xcopy /s /y "$(SolutionDir)packages\Microsoft.SqlServer.Compact.4.0.8876.1\NativeBinaries\amd64\*.*" "$(TargetDir)amd64"
Nothing special, it does nothing but create the x86 and amd64 subdirectories and copies the DLLs into them. The tricky part is that your Nuget package adds this build event. Look at the package folder for the magic that did it. You probably want to follow your example as close as possible to avoid mistakes. Look at the \ Microsoft.SqlServer.Compact.4.0.8876.1 \ tools directory, it contains two PowerShell script files that do this. Install.ps1 starts at the end of the installation. The function that creates the post-build step is in VS.psm1, Add-PostBuildEvent. Good luck to you.
Hans passant
source share