4

I have written a portable program in C# with certain dependencies (.NET Framework, Visual C++ redistributable, etc) that will run on Windows XP SP3 and up.

Because of that, the program needs a launcher that will run every time before the actual program does, checking that all the required dependencies are installed. If any of the dependencies are missing, an option to download and install that dependency, will be offered. If there are no missing dependencies, then the actual program is executed.

The launcher itself is relatively simple, consisting of some registry checkup and some WinAPI calls to verify the installed dependencies.

The file structure in the end will look something like this:

C#_compiled_portable_program.exe
C++_compiled_launcher.exe // executes on any system as low as a clean Windows XP SP3 install

The problem is that I have no idea how to compile a C++ code in Visual Studio 2013 that will run with absolute bare minimum dependencies (running on the runtime libraries that come with Windows XP SP3, at least).

Take for instance the absolute simplest C++ code:

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    printf("Hello world!");
    return 0;
}

If I compile this with Visual Studio 2013 with the default configurations, it will not execute on a machine that doesn't have VC++ 2013 installed, showing some nasty errors.

I looked around for similar questions and the closest I could find was Visual Studio 2010 MSVCR dependency removal?, but the answers are either incomplete or outdated.

So, just like an installer, is it possible to compile a C++ project in Visual Studio 2013 that will run pretty much on any system?

Community
  • 1
  • 1
Chris
  • 1,213
  • 1
  • 21
  • 38

1 Answers1

1

This is not perfect, but will do for now.

This is what I did to make a C++ project, compiled in Visual Studio 2013, execute ona system that doesn't have VC++ 2013 installed.

I created a new C++ project in Visual Studio 2013, File>New>Project>Visual C++>Win32 Console Application

Then in Solution Explorer right click the project and select Properties.
Click the Configuration drop down menu and select All Configurations.
In Configuration Properties>General, set Platform Toolset to Visual Studio 2013 - Windows XP (v120_xp).

With Dependency Walker determine what modules are imported by the compiled exe (the release build, not the debug one). The imported modules should be:

  • c:\windows\system32\KERNEL32.DLL
  • c:\windows\system32\MSVCR120.DLL

KERNEL32.DLL is a system file so we don't have to worry about that, and MSVCR120.DLL is the Visual C++ 2013 Runtime Library and we need to distribute this file along with the release build. When the executable needs to load a module, it first looks at its current location for that file and then in PATH (System32, etc). If we copy MSVCR120.DLL at same location the release executable is, then the program will run even on systems without VC++ 2013 installed.

Since the project is a 32-bit application, download VC++ 2013 Redistributable x86, install it on a 32-bit version of Windows (I installed it on a fresh Windows XP virtual machine), and copy c:\windows\system32\MSVCR120.DLL.

Update: Never mind. You don't have to distribute a copy of VC++ Runtime DLL file, you can just configure the project to link statically to the runtime library.
Here is explained how to do it. You'll still have to change the Platform Toolset though, if you plan on executing on Windows XP.

Community
  • 1
  • 1
Chris
  • 1,213
  • 1
  • 21
  • 38