简体   繁体   English

在本机 C++ 项目中导入 C# dll 库

[英]Importing C# dll library within native C++ Project

I'm trying to include C# dll library to Native C++ console application.我正在尝试将 C# dll 库包含到本机 C++ 控制台应用程序中。 I'm using this article: https://support.microsoft.com/en-us/kb/828736#bookmark-7我正在使用这篇文章: 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.但是,这篇文章是为 Visual Studio 2015 编写的,我使用的是 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.首先,我在 Stackoverflow 上检查了几个答案,但这些问题都不是我正在寻找的。

The question that is closest to my is here: C# library to native C++ application最接近我的问题在这里: C# library to native C++ application

So, I have create a C# library project, with the following code inside:因此,我创建了一个 C# 库项目,其中包含以下代码:

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.然后我将项目编译为dll文件,然后我通过RegAsm.exe注册了dll文件。 Now I have two output files:现在我有两个输出文件:

Testing_Library.dll测试库.dll

and

Testing_Library.tlb Testing_Library.tlb

Also I have created Native C++ Windows Console Application Project.我还创建了本机 C++ Windows 控制台应用程序项目。 Inside this project I have one .cpp file with the following code:在这个项目中,我有一个 .cpp 文件,其中包含以下代码:

// 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.目前我不知道为什么我会收到此错误 Windows 并且我无法在互联网上找到信息。 Could you please help me?请你帮助我好吗? Here you can find the whole Solution with all projects: Onedrive folder在这里您可以找到包含所有项目的整个解决方案: Onedrive 文件夹

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.请将 .NET 程序集的项目属性中的平台目标更改为 x86。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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