How to run a project using a C++/CLI assembly on another computer?

576 views Asked by At

I have a 3rd-party C++ assembly, which I need to use in my C# project. Hence I wrote a C++/CLI wrapper for the C++ assembly. Everything compiles and runs fine on my development machine.

But when I copy the content of my bin directory to one of our other machines, it crashes. The reason is, that it cannot load my C++/CLI-Wrapper assembly. It gives the typical message: FileNotFoundException: Could not load file or assembly 'MyWrapperAssembly' or one of its dependencies.

So I did some investigations: I made sure, that MyWrapperAssembly could not be found, and added an event handler to the assembly resolve event. Within that handler I loaded MyWrapperAssembly for reflection only. I then loaded all the referenced assemblies. The reflection only loading of MyWrapperAssembly works. So I am sure, that the file could be found. But when I try to load it

Assembly.LoadFrom("hiddenDirectory\MyWrapperAssembly.dll");

It crashes with the above mentioned error FileNotFoundException: Could not load file or assembly 'MyWrapperAssembly' or one of its dependencies.

Since the reflection only load worked, I was certain that there must be something wrong in the C++ layer. I compared the installed versions of the C++ Redistributable on my development machine and on the test machine and noticed, that there are some packages missing. So I downloaded and installed these packages on the test machine:

And it did not work. Then I tested 6 different PCs. My programm runs on 3 PCs and crashes on the other 3. The only thing that differentiates the PCs is the Microsoft Visual C++ 2010 Runtime. When it is installed, it seems to work.

But I can't find an installer for that package in the internet. So I installed Visual Studio 2013 on one PC where my programm crashed. After I installed Visual Studio (and with that the C++ Runtime), my program runs.

So I assume that I need that C++ runtime. It is no option to require Visual Studio on our customers PC. So I need to deploy the C++ runtime. But the question is:

Where do I get the Visual C++ Runtime from?

Or does anybody know if I am missing another thing?

2

There are 2 answers

4
David Yaw On

If you're using Visual Studio 2013, you need the 2013 runtime, not the 2010 runtime.

Visual C++ Redistributable Packages for Visual Studio 2013

0
Lucas Trzesniewski On

You can get the latest runtimes from here: The latest supported Visual C++ downloads.

You'll need at least the one matching the VS version you used to build the C++/CLI project (so if you use VS2010 you'll need the VC++ 2010 runtime).

Then, if the C++ library you're wrapping also needs a VC++ runtime, you'll have to install that one as well. The library provider should have told you the dependencies.

Note that if you linked to a third-party .lib file, you should use the same VS version that they used to compile it. Otherwise you'll end up using two different runtimes simultaneously, which could cause issues. C++/CLI will have to use a manged-compatible runtime, but it's less likely to cause trouble if the versions are matched.