Relative paths to DLL's for the built app - visual-studio

I wonder if it's possible to make something like this...
I have a project in VS2012 that uses two referenced VS projects (DLLs are being created during compilation) and some other DLLs (external libraries etc.). Now I want to clean up my compilation and place DLLs in two folders: e.g. Internals and Externals.
How to make it possible? The problem is that my compiled .exe app file wants the DLLs to be placed in the main folder (near to it) - so if it needs to load the library from DLL it crashes...
I tried to find something in the web, but ppl only ask about copying DLLs from the reference folders into the output folder. But that's not what I want to do:/

Ok, I have found a solution. I need to add a new item app.config and specify privatePaths:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="InternalsFolder;ExternalsFolder" />
</assemblyBinding>
</runtime>
</configuration>

Related

Visual Studio: How to tell exe where to look for dll's?

I have a .lib included in my project (Linker->Input->Additional Dependencies) that makes my .exe build require a corresponding .dll to be provided with the .exe. Naturally, I would put the .dll in the same folder as .exe and that works fine, but I want to have only the .exe in the main folder and all other files, along with this .dll, in a subdirectory. How do I make the .exe look for it in that directory?
Just for clarification, I don't need VS to look for the .dll, but the program(.exe), after it has been built. Sorry if this was already asked, but I just can't find the right answer.
Temporary solution:
Apparently this is much harder than I thought, just like Hans said. However, I have stumbled upon a neat trick which can somewhat make it look like this, and that is to create an internal .exe and put it into a subfolder with the .dll's, while it is just executed from a main .exe. ShellExecute command can be used to do this. Anyway, if anyone know how to actually do the real thing, please write it down :)
As #Hans Passant commented, I've solved it in my project (C#/WPF/vs2019) by editing app.config to:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="External"/>
</assemblyBinding>
</runtime>
</configuration>
Note that my output dir tree is like:
bin/
|--<myapp>.exe
|--External
|--nuget1.dll
|--nuget2.dll
You can specify multiple paths by delimiting them with semicolon.
For more information, see MS Docs.

What is the meaning of the folder names in Winsxs folder (windows run time assemblies directory)

Background:
Windows shared run-time libraries located at C:\windows\Winsxs folder
Inside Winsxs, there are two important sub folders also located as policies and Manifests
Other than that, there are plenty of run-time assemblies located in side each other sub folders.
All the sub-folders inside Winsxs and policies having same naming format.
Eg Folder Names:
Run times: x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e
Policies: x86_policy.9.0.Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_x-ww_b7353f75
As I know first part of the name (x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1) describe "processorArchitecture"(x86) , "Name" (Microsoft.VC90.CRT), "publicKeyToken" (1fc8b3b9a1e18e3b) and "Version"(9.0.30729.1) of the assembly or policy.
Question:
What is the last part of the assembly(x-ww_6f74963e) or policy(x-ww_b7353f75) folder name describes?
Ok here is the original issue (but quite long story). I deployed my C++ MFC application in windows XP computer that previously installed some of C++ redistribute packages and some security patches of run-time assemblies. So these pre-installed C++ redistribute packages automatically deployed some run-time policies in Winsxs/policies. those policies force to use new run-time assemblies instead of the one uses and deployed by my application. But some times these newer DLLs not there because of some other application removal or assemblies can be corrupted. So I'm finding a way to deploy run time assemblies specifically use for my application (it means my app must use the once deployed by it and ignore the policies). So I think this last part of the sub directory name associate with the identity of application. I need to find it.
If you can't trust global cache (and on WinXP it is super easy to corrupt it), you might have to install private copies of assemblies and override them in your application config.
Here is a hack I am using to override some assemblies for debugging purposes:
In your exe folder, drop the file named yourexename.exe.config with policy information redirecting real assembly version to something that will never exist in global cache. For example:
<configuration>
<windows>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity type="win32" name="someassemblyname" processorArchitecture="x86" publicKeyToken="sometoken"/>
<bindingRedirect oldVersion="1.0.0.0-1.0.999.998" newVersion="1.0.999.999"/>
</dependentAssembly>
</assemblyBinding>
</windows>
</configuration>
Take contents of the assembly that you want to use, drop it into the same folder and edit manifest to have the version you used above. For example
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<noInheritable></noInheritable>
<assemblyIdentity type="win32" name="someassemblyname" version="1.0.999.999" processorArchitecture="x86" publicKeyToken="sometoken"></assemblyIdentity>
<file name="somedll.dll"/>
</assembly>
In the end you will get following files in your install folder:
yourexename.exe
yourexename.exe.config
somedll.dll
someassemblyname.manifest
And your executable will pick up private copy of the dll.
More info here: Application Configuration Files
EDIT: if you have problems like "the application failed to initialize properly" or "side by side configuration is incorrect" (and you will have them once you start playing with sxs), use sxstrace tool for diagnostics (not available on Windows XP though). That will tell you exactly which assembly causes problems.

Post Build event that puts all extra .dlls in bin directory using probing

I have WPF assembly with a bunch of other dlls in my project
I want to move everything except the main assembly and the app.config from the build directory to a subdirectory called bin
this is easy when I add the probing tag to my app.config and do it manually (a cut and paste job)
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="..." />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin"/>
</assemblyBinding>
</runtime>
</configuration>
What i want to do now is automatically move everything that's not the assembly into the bin directory at the end of my build.
so from this
App.exe
App.config
Domain.dll
Application.dll
Framework.dll
to this
App.exe
App.config
bin\Domain.dll
bin\Application.dll
bin\Framework.dll
I'm guessing I could use a bat files with a bunch of move commands in it but i was hoping that there was something a bit more reusable and intelligent than that out there.
For anyone interested what I ended up doing was this
a Post Build Event that looks like this
move $(TargetDir)$(TargetName).* .\..
and I set the build directory to be the actual sub directory so it looked like
[Path to Project]\bin\Release\bin
So instead of working out what to move down a directory (which could be a whole bunch of things) I just took the bits I know I want up a directory.
Big smiley face for me Hooray!

Visual Studio: DLL locations for release

I have a project that I am working on that references a "Common Library (DLL)". In the DevEnv it works fine, however if I build and try to organize my files it doesn't work. Basically if I have things setup like so:
C:\Program Files\MyApp\MyApp.exe
C:\Program Files\MyApp\Common\WPF Commons.dll
C:\Program Files\MyApp\Modules\SomeModule.dll
etc
MyApp.exe doesn't work. It tries to look only in the current directory for the DLL files. So how do I set it up in Visual Studio so that when I build the application knows to look for the DLLs in those other folders?
Oh, and I realize that it does work in Dev because all the DLLs are put in the same folder. I don't want to do that for release though :-/
What you need to do is add a private probing path into the application configuration file. This tells the CLR which directories to look in for extra assemblies.
http://msdn.microsoft.com/en-us/library/823z9h8w.aspx
Sample app.config
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="Common;Modules"/>
</assemblyBinding>
</runtime>
</configuration>

Change Dll Folder In Visual Studio 2008 C#

I have 2 projects in my solution. The first one is a console application, and the second is a dll, used (referenced) by the console. When I build my solution (release) I get one EXE file and one DLL file, because copy local is true (if I set it to false, it doesn't run).
How can I store that DLL file in a subdirectory? If my output folder is C:\123\, and there's the EXE file, I want all my DLLs to be in C:\123\Dll\.
You should be able to use the probing element to specify which paths your application should use when attempting to resolve dll dependencies.
Example:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="123;123\dill"/>
</assemblyBinding>
</runtime>
</configuration>
Also you can use the Post-build event command line to copy your dll's to a particular directory.

Resources