MATLAB: Do I receive an “application has failed to start” error message when loading DLL into MATLAB with LOADLIBRARY

clibraryMATLABsharedwin32win64

I have a C/C++ shared library that I compiled using Visual Studio 2005. When I load the library with MATLAB's Generic DLL function LOADLIBRARY on a Windows machine that does not have Visual Studio installed, I encounter the following error:
ERROR: Application failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.
I encounter this error even if the Microsoft Visual C++ 2005 Redistributable Package (Visual C++ Runtime Libraries) is installed on that machine.

I also get the same error when trying to load a DLL compiled with Visual Studio 2008 in MATLAB on my Windows machine, which only has Visual Studio 2005 installed. I have installed the 2008 version of the Microsoft Visual C++ Redistributable Package on my machine, though.

Best Answer

In order to properly load a DLL compiled using Visual Studio or Visual C++ on a machine that does not have the program, two conditions must be met during the deployment process:
(1) The end user must install the version of the Microsoft Visual C++ Redistributable Package that matches the version of Visual Studio from which the DLL was installed.
(2) The DLL must be built in Release configuration in Visual Studio/C++, not in Debug configuration. The Release configuration embeds manifests into the binary file that describe the DLL's dependencies on the Visual C++ runtime libraries. These manifests are required to properly link the DLL to the runtime libraries.
If the DLL fails to load on a machine that already has the matching version of the Microsoft Visual C++ Redistributable Package installed, recompile the DLL in Visual Studio using the Release configuration.
For more information, consult the following Microsoft Help page:
Related Question