11

I have created a NuGet package from .NET4.0 DLLs which include mixed (Managed and native) code.

The Native code is packaged up inside the .NET4.0 DLL but has a dependency on the Visual C++ 2013 Redistributable

I'm trying to brainstorm ways to either package the redist with the NuGet package and/or warn the user that they need it, but I'm drawing a blank.

Anyone got any ideas?

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • Pretty doubtful that it is a good idea to hide this. It isn't just the CRT, that's easy to solve, you also have a rock-hard dependency on the client project's Platform target setting. Very hard to hide if you don't have a pure wrapper. Basic advice [in this post](http://stackoverflow.com/a/28771413/17034). – Hans Passant Feb 15 '16 at 14:32
  • I don't necessarily want to hide, but I do want to at least inform. Otherwise I get users hitting tech-support with 'Y U NO Work' type questions – Dr. Andrew Burnett-Thompson Feb 15 '16 at 18:08

1 Answers1

2

I actually kind'of solved this myself. While I couldn't find a solution to include the VCpp Runtime as a dependency to a NuGet package, I did find a solution to warn the user that the Visual C++ 2013 Runtime was needed.

I run this code once, statically, at startup of the component/library that requires the VC++ Runtime:

    private static void AssertVcppDependencies()
    {
        var system32Path = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86);
        var system64Path = Environment.GetFolderPath(Environment.SpecialFolder.System);

        string platform = Environment.Is64BitProcess ? "x64 and x86" : "x86";
        bool success = File.Exists(Path.Combine(system32Path, MSVCP120DllName));

        if (Environment.Is64BitProcess)
        {
            success &= File.Exists(Path.Combine(system64Path, MSVCP120DllName));
        }            

        if (!success)
        {
            throw new InvalidOperationException("This Application Requires the Visual C++ 2013 " + platform + 
                " Runtime to be installed on this computer. Please download and install it from https://www.microsoft.com/en-GB/download/details.aspx?id=40784");
        }
    }

This should alert any developers that are consuming your NuGet package that they need to install the runtime.

Dr. Andrew Burnett-Thompson
  • 20,980
  • 8
  • 88
  • 178
  • 1
    Just to add, you could check the current application's platform by testing `bool is64bit = IntPtr.Size == 8` and only test for one dll. – Patrick Stalph Aug 03 '18 at 11:11
  • Another way I solved it. I C++ library source which is included in our native DLLs. In this case I was able to compile against the VC++ Runtime statically to avoid the need for installing this on client PCs. Only advisable when its a.) your C++ library code and b.) Not talking to any other C++ libraries that may have different versions of the runtime linked. – Dr. Andrew Burnett-Thompson Mar 21 '19 at 16:51