Adding a Configuration file for a VC++ DLL



A VC++ application will fail to run if the required Visual C++ runtime libraries are not present on the target operating system (machine).  It’s the manifest of the executable used by the operating system loader to load assemblies that your application depends on.  Any C/C++ program built by Visual C++ 2005/2008 has to include a manifest describing its dependencies on Visual C++ libraries.


If the loader fails to resolve the correct assemblies the applications either fails at the start or might crash later. Manifest can either be embedded inside the binary as a resource, or saved as an external file in the application's local folder.  To deploy VC++ runtime libraries with the executable there are some ways described in these links . The xCopy deployment of VC++ runtime libraries works, when a target machine doesn’t have the required VC++ runtime in the WinSxS folder. 



Briefly, “app config file” is an XML configuration file that redirects a specific application (or modules which have their own manifest) from using one version of a side-by-side assembly to another version of the same assembly.  “App Config file” has an element bindingRedirect which does the redirection it has the name and versions of the assembly to be redirected. For more on “App Config files” their pro and cons please visit the link.


The Problem


You would create an app config file for executable if its manifest shows dependency on a version V1 for a VC++ runtime assembly; however there is a higher version V2 of the same assembly available in the application folder, so you would like your application to load the higher version V2 and config file does that.  


At runtime the operating system loader reads the config file present for the exe and loads the V.2 assembly as mentioned in the bindingRedirect element.

Also note, if the V.2 version of the same assembly is present in the WinSxS folder, the application would not require the “app config file”, it’s the “Policy File” present in the WinSxS folder for VC++ runtime libraries which did this binding redirection automatically.


Now comes the highlight of this write-up. Similar to the executables, Dlls may also have their own configuration files for the binding redirection, see below how.


Imagine you have an executable which uses a 3rd party VC++ DLL (built with msvcr80.dll version 8.0.50727.42) and the exe is being built with version 8.0.50727.762. This can be seen in the manifest information of exe and dll.



If you run this application on a machine where 8.0.50727.762 version of msvcr80.dll is present in the WinSxS folder it will run just fine, application will load 8.0.50727.762 of msvcr80 or a higher version if that  present. The Policy file does this auto redirection for exe and dll both, as long as WinSxS has 8.0.50727.762 version.


Also on the development machine this application would run but on a machine where you Xcopy the 8.0.50727.762 version of msvcr80.dll in the executable’s directory it will fail to start with a side by side error, as the loader will not be able to find  8.0.50727.42 version of msvcr80.dll for the dll.


The second problem which can happen is –


Suppose you have a VC++ 2005 built application and you also have the latest ATL security patch updates (8.050727.4053) on your build machine. This same application depends on a third party dll which is built with the SP1 version (8.050727.762) of VC++ runtime.  On the development machine everything works.

But also imagine on the target machines you don’t have the latest security patch dlls (8.050727.4053) in the WinSxS folder, but you have 8.050727.762 version of runtime present there.


So XCopy(application local deployment) strategy to distribute the newer runtime (8.050727.4053) will strike and you deploy the  8.050727.4053 version in the application folder. Since the third party libraries depends on the 8.050727.762 version of CRT you thought of supplying an application configuration for the Executable doing the redirection of VC runtime to 8.050727.4053 for the dll.


Now the application will start on this machine but if you see in Procmon  you will see two different version of CRT library, versions 8.050727.4053 and 8.050727.762 loaded in the process space, which will lead to application crash in some scenario.


Why didn’t the config file work in the second scenario?  It’s WinSxS folder which takes the precedence over the app local directory while loader searches for VC++ runtime. That’s the reason the loader finds the .762 version in the WinSxS folder and .4053 in the local folder.


The Workaround:


In both the above two scenarios you can have a “Configuration File” for your dll for the   redirection from 8.0.50727.762 to 8.050727.4053. Add a configuration file for the exe and also add the configuration for you 3rd party Dll. A “foo.dll” will have its configuration file named as “foo.dll.2.config”


In this link under “File Name Syntax” the syntax “example.exe.<resource ID>.config “ is given it works  for executables , a similar syntax works for the dlls as well.


-Bhupendra Dhyani

Comments (1)

  1. Mark Gammon says:

    This is a REALLY enlightening article. Explains a lot of the mystery of how to deliver multi-version CRT apps.

    I have found one problem though with the workaround. I have the situation where a 3rd party dll lives in a sub-folder from the .exe. I have a sub folder with the 8.0.50727.4053 CRT dlls in the same folder as the .exe, and a app.exe.config file to redirect to 4053. In the 3rd party dll's sub-folder i have a foo.dll.2.config to redirect to the 3rd party from its 762 version to 4053, and a local sub-folder with the 8.0.50727.4053 CRT dlls.

    When I run the exe it crashes. With sysinternal procmon I can see that TWO versions of the msvcr80.dll are being loaded, but both are the desired 4053 version. So I've solved the redirection from an older crt, but now I'm getting two versions of the same msvcr80.dll loaded and I'm guessing that my crash is because of this. I sort of proved this by temporarily moving my exec+.config file into the 3rd party folder and running from there. With this I see only the ONE version of msvcr80.dll gets loaded, and my exec doesn't crash.

    I've tried removing the CRT sub-folder from the 3rd party folder thinking that as my .exe has already loaded a 4053 msvcr80.dll, then the redirect for the 3rd party dll won't try to load another. But this simply fails to start with the "failed to initialise properly" error.

    So it looks like its almost there but no cigar.

    Any suggestions would be really appreciated 🙂

Skip to main content