C++/CLI – Use Managed C++ DLL Without Installing Microsoft Visual C++ Redistributable

Artem Razin
July 16, 2008

If your .NET application uses components written in Managed C++, you typically need to distribute the Microsoft Visual C++ Redistributable with it.
Otherwise, the user may see this error when starting the application:

“This application has failed to start because the application configuration is incorrect.”

Let's see why this happens — and how to fix it without installing the redistributable — using BoxedApp SDK.


1. Why It Happens

A DLL written in Managed C++ depends on the Microsoft Visual C++ Runtime.
Those runtime DLLs cannot be linked statically, which is why they normally must be installed system‑wide.

The standard solution is to include the Microsoft Visual C++ Redistributable installer with your setup.
However, with BoxedApp SDK, we can emulate the runtime's presence instead.


2. Emulating the Visual C++ Runtime

Right after the application starts — before any Managed C++ DLL is used — create virtual files for the runtime DLLs your component depends on.

Here's an example in C#:

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            BoxedAppSDK.NativeMethods.BoxedAppSDK_Init();
 
            string pathOfWinSxS =
                Directory.GetParent(Environment.SystemDirectory).FullName +
                @"\WinSxS";
 
            if (!Directory.Exists(pathOfWinSxS))
                // Windows 2000 fallback
                pathOfWinSxS = Application.StartupPath;
 
            Stream fromStream =
                Assembly.GetExecutingAssembly()
                    .GetManifestResourceStream("WindowsApplication1.res.msvcm80d.dll");
 
            CreateDLLInMemory(
                pathOfWinSxS +
                @"\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcm80d.dll",
                fromStream);
 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
 
            BoxedAppSDK.NativeMethods.BoxedAppSDK_Exit();
        }
 
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool CloseHandle(IntPtr hObject);
 
        static void CreateDLLInMemory(string strVirtualPath, Stream stream)
        {
            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];
 
            IntPtr hHandle =
                BoxedAppSDK.NativeMethods.BoxedAppSDK_CreateVirtualFile(
                    strVirtualPath,
                    BoxedAppSDK.NativeMethods.EFileAccess.GenericWrite,
                    BoxedAppSDK.NativeMethods.EFileShare.Read,
                    IntPtr.Zero,
                    BoxedAppSDK.NativeMethods.ECreationDisposition.New,
                    BoxedAppSDK.NativeMethods.EFileAttributes.Normal,
                    IntPtr.Zero);
            CloseHandle(hHandle);
 
            int nReadBytes;
            using (FileStream virtualFileStream = new FileStream(strVirtualPath, FileMode.Open))
            {
                while ((nReadBytes = stream.Read(buffer, 0, BufferSize)) > 0)
                    virtualFileStream.Write(buffer, 0, nReadBytes);
            }
        }
    }
}

3. Finding the DLL Dependencies

To determine which DLLs your Managed C++ component depends on, use depends.exe (Dependency Walker) — included with Visual Studio tools.
Open your Managed C++ DLL in Dependency Walker to see all required VC++ runtime components.


4. Summary

With BoxedApp SDK, you can make your application's Managed C++ DLLs run even if Microsoft Visual C++ Redistributable is not installed.
By creating virtual files for the dependent DLLs, your app emulates the presence of the runtime — no installer required.


Related Links