1

I build my app either as x86 or x64. This app uses external DLL.

I have x64 system (Windows 10) with the same DLL library installed for both platforms - x86 and x64. They are placed in same folders inside appropriate Program Files directory. I can manually set path to either one in environment variable PATH and it woks. But it is a little incovinient to rewrite PATH and reset computer when I switch platform and want to test the other one. Is there any solution, how system automatically loads correct DLL from correct Program Files dir?

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
  • Probably related: [Dynamic-Link Library Search Order](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx). – user7860670 Oct 03 '17 at 08:19

2 Answers2

1

Copy the DLL in the build location next to the executable, for Visual Studio this is typically \Debug or \Release in a architecture dependent sub directory (i.e. x86 or x64). Just make sure the target / output locations are set correctly in the project settings.

You have to copy only once, or more correctly: each time after you 'clean' the solution. To make this easier, many people use a dll-copy script (use batch, ruby or python) and have it run automatically before building or after cleaning. You can execute the script in a pre-build step or post-build step that can be configured in your Visual Studio solution or project settings.

There is also a more robust way to handle build artifacts and peculiarities: I highly recommend the use of CMake to keep the build matrix organized. It is provides a general cross-platform approach to script pre-build and post-build actions such as tracking dependencies, copying files, packaging installers, deployment, version verification, versioning, etc.. it comes with an easy scripting language so you can build macro's and functions to do your copying. It can be a bit of a learning curve to get it right, but once it's there, it provides a robust dependable way to build out your project build pipeline.

StarShine
  • 1,940
  • 1
  • 27
  • 45
1

I have found solution for running apps from Visual Studio: How do I set the path to a DLL file in Visual Studio?

It is working as epxected. If I run app outise Visual Studio, I set PATH variable by myself and is also working.

Martin Perry
  • 9,232
  • 8
  • 46
  • 114
  • You shouldn't need to set PATH, that's a legacy technology and should be a last resort. If you don't want to the put the DLL in the same directory as the executable, the program itself should know how to find it. (I suppose setting PATH may be a useful shortcut in a development environment. But the release version shouldn't depend on it.) See also [Don’t use global state to manage a local problem](https://blogs.msdn.microsoft.com/oldnewthing/20081211-00/?p=19873). – Harry Johnston Oct 03 '17 at 20:45