Some of the Boost libraries must not be linked statically with the C++ CLI code otherwise the compiler can generate an incompatible image for some of the Windows versions.
As far as I am concerned, I had a hard time figuring out the issue when statically building with Boost 1.64 x86 thread library with VC++ 2017 under windows 10.
The binary worked fine under Windows 10 but raised a System.BadImageFormatException under Windows 7.
The issue was located in the Boost thread library which I initially statically linked to my C++ CLI assembly.
Here is a short code which easily reproduces the problem:
testcli.h - C++ CLI assembly FAILURE code
#pragma comment(lib, "libboost_thread-vc141-mt-1_64.lib") // static link makes the program crash under W7
namespace testcli
{
public ref class TestClass { public: static void Test(){} };
}
Program.cs - C# client code loading 'testcli'
using testcli;
namespace Cli
{
class Program { static void Main(string[] args) { new TestClass(); } } // instanciate C++ CLI Boost class
}
The code above returns by raising a System.BadImageFormatException (the exception can be found in the Application Event Viewer).
If testcli is changed so that the Boost thread library is now linked dynamically:
testcli.h - C++ CLI assembly SUCCESSFUL code
#pragma comment(lib, "boost_thread-vc141-mt-1_64.lib") // dynamic link works fine under any Windows
namespace testcli
{
public ref class TestClass { public: static void Test(){} };
}
The code now returns successfully.
Note that you can define BOOST_THREAD_DYN_LINK instead of BOOST_ALL_DYN_LINK as explained here: http://www.boost.org/doc/libs/1_64_0/doc/html/thread/build.html
Doing so, you will not have to package all the Boost dynamic libraries with your application.