Skip to content
This repository has been archived by the owner on Oct 25, 2021. It is now read-only.

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

Open
salunkhesagar1386 opened this issue Sep 5, 2019 · 2 comments

Comments

@salunkhesagar1386
Copy link

salunkhesagar1386 commented Sep 5, 2019

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");
    }
}
  1. Build the library. (The .dll file will be created with MonoTestDLL.dll)
  2. 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.

@chamons chamons added this to the Future milestone Sep 6, 2019
@chamons
Copy link
Contributor

chamons commented Sep 6, 2019

Similar / Duplicate of #740 ?

@m-carrasco
Copy link

m-carrasco commented Jul 12, 2020

I'm having a very similar issue on my first attempt to use Embeddinator.

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

Which MonoTestDLL.dll do you mean? If I follow you, there are two options. One is generated from your C# project. The other one is Embeddinator's result. Either way, I have no luck even trying both of them.

@salunkhesagar1386 I've just noticed that this open issue is not so new. Could you solve your issue?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants