Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
So im trying to embed C# mono in C++
i expect the msbuild to work fine(as it does now) and instead of an exception , i should get the text "Bark!!!" in the console , but i get Unhandled exception at 0x75FFEDDB (ucrtbase.dll) in CppMonoTest.exe: Fatal program exit requested. (at line comment in code) and * Assertion at ..\mono\metadata\class.c:3355, condition `is_ok (error)' not met, function:mono_class_try_load_from_name, Could not load runtime critical type Main.Program, due to Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
#include <windows.h>
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>
#include <direct.h>
#include <filesystem>
#pragma comment(lib, "mono-2.0-boehm.lib") // replaced from mono-2.0.lib
#pragma comment(lib, "mono-2.0-sgen.lib") // It is new with GC code library GC from Gnu Compilication
#pragma comment(lib , "MonoPosixHelper.lib")
std::string get_working_path()
char temp[260]; // max windows path length
return (_getcwd(temp, sizeof(temp)) ? std::string(temp) : std::string(""));
int main(int argc, char* argv[])
#pragma region Load and compile the script
//Compile the script
std::string Command = std::string("cd ") +
std::filesystem::path(get_working_path()).parent_path().string() + R"(\CMonoTest && msbuild)";
system(Command.c_str());
#pragma endregion
#pragma region Init mono runtime
mono_set_dirs("C:\\Program Files (x86)\\Mono\\lib",
"C:\\Program Files (x86)\\Mono\\etc");
//Init a domain
MonoDomain* domain;
domain = mono_jit_init("MonoScriptTry");
if (!domain)
std::cout << "mono_jit_init failed" << std::endl;
system("pause");
return 1;
//Open a assembly in the domain
MonoAssembly* assembly;
std::string assemblyPath = std::filesystem::path(get_working_path()).parent_path().string()
+ R"(\CMonoTest\bin\Debug\net6.0\CMonoTest.dll)";
assembly = mono_domain_assembly_open(domain, assemblyPath.c_str());
if (!assembly)
std::cout << "mono_domain_assembly_open failed" << std::endl;
system("pause");
return 1;
//Get a image from the assembly
MonoImage* image;
image = mono_assembly_get_image(assembly);
if (!image)
std::cout << "mono_assembly_get_image failed" << std::endl;
system("pause");
return 1;
#pragma endregion
#pragma region Run a static method
//Build a method description object
MonoMethodDesc* TypeMethodDesc;
const char* TypeMethodDescStr = "Main.Program:StartWebRegister(string)";
TypeMethodDesc = mono_method_desc_new(TypeMethodDescStr, NULL);
if (!TypeMethodDesc)
std::cout << "mono_method_desc_new failed" << std::endl;
system("pause");
return 1;
//Search the method in the image
MonoMethod* method;
method = mono_method_desc_search_in_image(TypeMethodDesc, image); //----> Unhandled
//exception (ucrtbase.dll)
if (!method)
std::cout << "mono_method_desc_search_in_image failed" << std::endl;
system("pause");
return 1;
void* args[1];
std::string barkTimes = "Bark!!!";
args[0] = &barkTimes;
//run the method
std::cout << "Running the static method: " << TypeMethodDescStr << std::endl;
mono_runtime_invoke(method, nullptr, args, nullptr);
#pragma endregion
return 0;
this is the C++ code and a small script example in C#
namespace Main
class Program
static public int StartWebRegister(string email)
Console.WriteLine(email);
return 0;
I installed mono x86 at C:\Program Files (x86)\Mono , and my build config is x86 debug and i use msbuild to build the code as xbuild is deprecated ( note: i used msc and it worked fine , but i need to use some packages from nuget , and building from msbuild is easier as you need just the .csproj)...
So is there something im doing wrong or mono is not compatibile with msbuild assemblies?
___ 1. I checked the .mono lib folder for System.Runtime and it has it in there ...
___ 2. Could not load assembly System when using C++ and embedded mono to call to C# DLL
checked this but i have the path right as mono_set_dirs("C:\Program Files (x86)\Mono\lib", "C:\Program Files (x86)\Mono\etc");
___ 3. downgraded system.runtime from 4.3.1 to 4.0.0 in nuget manager in vs and the same (as in https://stackoverflow.com/a/54568734/16785067)
How do i fix these errors? If msbuild is not compatibile with mono , how do i add nuget packages to msc?
Note: packages im trying to use (from .csproj after downgrading system.runtime)
<ItemGroup>
<PackageReference Include="DotNetSeleniumExtras.WaitHelpers" Version="3.11.0" />
<PackageReference Include="NAudio" Version="2.1.0" />
<PackageReference Include="Selenium.Chrome.WebDriver" Version="85.0.0" />
<PackageReference Include="Selenium.Support" Version="4.4.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.4.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="104.0.5112.7900" />
<PackageReference Include="System.Runtime" Version="4.0.0" />
<PackageReference Include="System.Speech" Version="6.0.0" />
</ItemGroup>
Here is the full console log : https://pastebin.com/DXtZ1C2k
So the problem was that the legacy mono , from mono-project.com isnt compatibile with msbuild net6.0 and net5.0 as the modern .net is actually .net core ... so if you want to build it with msbuild and use mono you should use .net 4.8.1 or older... Else if you want to use modern .net check this example
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.