mono/Embeddinator-4000

c++ dll created for .NET dll using Embeddinator 4000, fails to load in C++ console application

Open

#741 opened on Sep 5, 2019

View on GitHub
 (2 comments) (0 reactions) (0 assignees)C# (91 forks)github user discovery
bugchelp wanted

Repository metrics

Stars
 (758 stars)
PR merge metrics
 (PR metrics pending)

Description

I am trying to use the C++ library (.dll) created for a .NET library using Embeddinator-4000.exe in Windows C++ console app. However, when I am using executing the windows C++ console app, application shows following error dialog

image

Steps to Reproduce

  1. Create a 'Class Library (.NET Standard)' project in VS2017 with name 'MonoTestDLL'
  2. Add following code to the class which is default created template

.

public class Class1
{

    public Class1()
    {
        System.Console.WriteLine("Class1 .NET Initialzed Successfully");
    }

    public void PrintMessage()
    {
        System.Console.WriteLine("Class1 .NET In PrintMessage");
    }

    public void PrintMessage(string strStringToPrint)
    {
        System.Console.WriteLine(strStringToPrint);
    }

    public int Add(int iNum1, int iNum2)
    {
        return iNum1 + iNum2;
    }

    static void Main(string[] args)
    {
        System.Console.WriteLine("Class1 .NET In main");
    }
}

3. Build the library. (The .dll file will be created with MonoTestDLL.dll) 4. Now, build the C++ libraries for this .NET dll using Embeddinator-4000 using following command,

Embeddinator-4000.exe --gen=c --platform=Windows --compile --target=shared --outdir="C:\Temp\MonoLibs" "${SolutionDir}\bin\Debug\netstandard2.0\MonoTestDLL.dll"

Note: You can replace the path of dll per solution location

  1. Embeddinator will generate following files along with the Mono's library files, MonoTestDLL.h MonoTestDLL.c MonoTestDLL.lib MonoTestDLL.dll MonoTestDLL.exp

  2. Now create a new 'Windows Console Application' in Visual C++ using Visual Studio 2017 with name 'TestCLIApp'.

  3. Add following code in 'TestCLIApp.cpp' file which is created by default in the template.

#include "../TP/MonoTestDLL.h"
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	MonoTestDLL_Class1* object = new MonoTestDLL_Class1();

	if (object != nullptr)
	{
		int result = MonoTestDLL_Class1_Add(object, 10, 20);

		cout << result;
	}
	else
	{
		cout << "Failed to initialize the library";
	}

	getchar();
}
  1. Add following path in 'Include Directories' option by selecting the Project Properties, C:\Program Files (x86)\Mono\include\mono-2.0;

  2. Add 'MonoTestDLL.lib' in Linker>>Input. (make sure you add proper library path in 'Library Directories' option)

  3. Copy the 'MonoTestDLL.dll' at the path where 'TestCLIApp.exe' resides

  4. Build and execute the C++ console application.

Expected Behavior

Command execution shall work fine and I am expecting 'MonoTestDLL.lib' and wrapper files to be generated by the Embeddinator tool which I can further use in my C++ executable or library.

Actual Behavior

Application fails to load with above mentioned error dialog

Environment

Windows Installed Mono and Embeddinator using VS2017 NuGet package.

Set paths of Mono and Embeddinator executable folder in environment variables using %PATH% on command prompt to avoid errors.

Contributor guide