Share the ideas and thoughts, become united ...

Friday, May 27, 2011

Understanding & Solving the "Side by Side Configuration" (DLL Hell) Problem - C++

Many developer has seen the "Side by Side Configuration Error" problem. They build their executable in one system and after transferring the application to another system they got this error. Just by seeing that error one might think - "WTH, does C++ application need dependencies?". The answer is yes if you build Win32 application.


 In win32 application we use the header file to use the function in our application and the library file to successfully link the application to external functions (library functions). This problem occurs due to MS's dynamic link feature. When you build the application the VS linker perform a shared dynamic link to the system library. This linking reduces the size of the executable because the library resides in the OS. Now when you transfer the application to another system & try to run it, you have a high chance of getting the "Side by Side Configuration Error" due to mismatched version of system library resided in the target OS.

The easiest way to solve this problem is to reinstall the MSVC redist package. Most of the time it may save your day. But sometime it may not.

Lets jump into the heart of the problem. Usually the VC redist resides in "%windir%\winsxs". In that folder you will see all the required library to run Win32 C++ application. 

Now the question is which library do we need? Lets get back to our MSVC project. I have created a sample test Win32 application and build it. Now if you go to the debug folder you will see many files. Among them you will see a ".manifest" file. Now open it with a text editor. You will see that that is an xml file. The important part of this file is -
<dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.VC90.DebugCRT" version="9.0.21022.8" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
    </dependentAssembly>
  </dependency>
This section tells us which library & which version we will need to run the program. In the winsxs folder you will get the library in this format -
processorArchitecture_name_publicKeyToken_version
 That folder contains the required library files. I hope you have understood the dependency search. Now I am going to share another working solution. 

In MSVC you can link the required library as static. To do so go to solution's properties --> Expand the Configuration Properties --> General. You will see use of MFC, ATL configuration entries under the project defaults. 


The default value for the entry is "Use standard windows library". Change it to "Use MFC in a static library". You should set this option in ATL configuration entry if you use ATL. Now build the solution and thats it. Now your executable will be bigger but it won't require the windows library from winsxs folder.

The "Side by Side Configuration" error snapshot is by courtesy of google :). And the later snapshot is taken from MSVS 2008.

Hope this article will help you. If you have any question or queries please leave a comment.

No comments:

Post a Comment