简体   繁体   English

在C ++中调用C#dll

[英]Calling C# dll in C++

My current requirement is to call some methods of C# dll in c++ console application. 我当前的要求是在c ++控制台应用程序中调用C#dll的某些方法。 I have a sample C# console application code which is calling the methods from the c# dll. 我有一个示例C#控制台应用程序代码,该代码从c#dll中调用方法。

C# dll was added as a reference into the C# console application project. C#dll已作为参考添加到C#控制台应用程序项目中。

using CSharp;

void fn()
{
string retVal;
CSharpApi obj = new CSharpApi("Something");
retVal = obj.Invoke("Something");
obj.Dispose();
....
....
}

The definition of CSharpApi CSharpApi的定义

namespace CSharp
{
    public class CSharpApi : IDisposable
    {
        public CSharpApi();
        public CSharpApi(string param1);
        public string Invoke(string param1);
        public void Dispose();
    }
}

I need to write a c++ console application equivalent to the above c# console application but not sure how to achieve it. 我需要编写一个与上述c#控制台应用程序等效的c ++控制台应用程序,但不确定如何实现。 Is is possible to call C# dll methods using LoadLibrary and GetProcAddress APIs? 是否可以使用LoadLibrary和GetProcAddress API调用C#dll方法? I would be very grateful if you could give me some examples. 请给我一些例子,我将不胜感激。

正如Joachim在他的评论中提到的那样,最简单的方法是使用C ++ / CLI托管的.NET应用程序 ,它为您提供了两全其美的方法,您仍然可以以非托管的方式编写大部分代码,并且调用托管代码的相对简单的方法。

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

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