简体   繁体   中英

Importing C# dll library within native C++ Project

I'm trying to include C# dll library to Native C++ console application. I'm using this article: https://support.microsoft.com/en-us/kb/828736#bookmark-7

However the article is written for Visual Studio 2015 and I'm using Visual Studio 2015.

First I have checked a couple of answers here on Stackoverflow, but none of this questions was exactly what I'm looking for.

The question that is closest to my is here: C# library to native C++ application

So, I have create a C# library project, with the following code inside:

namespace Testing_Library
{

    // Interface declaration
    public interface iCalculator
    {
        int Add(int number1, int number2);
    }

    public class ManagedClass:iCalculator
    {
        public int Add(int number1, int number2)
        {
            return number1 + number2;
        }
    }
}

Then I have compiled the project to dll file, and then I have registered the dll file via RegAsm.exe. Now I have two output files:

Testing_Library.dll

and

Testing_Library.tlb

Also I have created Native C++ Windows Console Application Project. Inside this project I have one .cpp file with the following code:

// Import the type library.
#import "..\Testing_Library\bin\Debug\Testing_Library.tlb" raw_interfaces_only

using namespace std;
using namespace Testing_Library;

void main()
{
    // Initialize COM.
    HRESULT hr = CoInitialize(NULL);

    // Create the interface pointer.
    iCalculatorPtr pICalc(__uuidof(ManagedClass));

    long lResult = 0;

    // Call the Add method.
    pICalc->Add(5, 10, &lResult);

    wprintf(L"The result is %d", lResult);

    // Uninitialize COM.
    CoUninitialize();

}

And when I try to run the project I receive the following error screen:

在此处输入图片说明

Currently I have no idea why am I receiving this error Windows and I can not find information on the internet. Could you please help me? Here you can find the whole Solution with all projects: Onedrive folder

I'm trying to compile and run currently:

Test_me

and

Testing_Library

Please change the Platform Target in Project Properties to x86 for your .NET assembly.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM