C ++项目如何调用C#对象? [英] How the C++ project to invoke C# object ?

查看:83
本文介绍了C ++项目如何调用C#对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个通用的解决方案中,有两个项目,一个是c ++项目,另一个是c#项目。

c#项目中有一个类,如下:



 命名空间 CSLib 
{
Class Person
{
Private string PersonName = string .Empty;

公共人员(字符串 personName)
{
PersonName = personName;
}

公共字符串 GetPersonName()
{
返回PersonName;
}
}

人mPerson = 人( 杰克);
}





那么,c ++工程如何调用mPerson.GetPersonName来获取Jack



我尝试过:



  #using.. \ debug \CSLib.dll
使用 命名空间 CSLib;





我只知道如何通过c ++调用c#类,但不是调用他的对象方法的方法

解决方案

你想要的是 interop ,见文章这里: C ++ Interop:如何从本机C ++调用C#类,这个类是非静态的? - 堆栈溢出 [ ^ ]

这里: C#的实用教程 - 使用(主要)标准C ++的C ++ Interop Stephan Garbin [ ^ ]

另一个选项可能是 IPC (进程间通信),请参阅:GitHub - Microsoft / IPC:IPC是一个C ++库,使用Windows上的共享内存提供进程间通信。提供.NET包装器,允许与C ++交互。 [ ^

In a common solution, there are 2 projects, one is the c++ project and the other is the c# project.
There is a class in the c# project, as follows:

namespace CSLib
{
Class Person
{
     Private string PersonName = string.Empty;
     
     Public Person (string personName)
     {
          PersonName = personName;
     }
     
     Public string GetPersonName ()
     {
         Return PersonName;
     }
}

Person mPerson = new Person("Jack");
}



So, how c++ engineering calls mPerson.GetPersonName to get Jack

What I have tried:

#using "..\debug\CSLib.dll"
using namespace CSLib;



I only know how to call the class of c# through c++, but not the method of calling his object method

解决方案

What you want is called "interop", see article here: C++ Interop: How do I call a C# class from native C++, with the twist the class is non-static? - Stack Overflow[^]
And here: A Practical Tutorial on C# – C++ Interop using (mostly) Standard C++ | Stephan Garbin[^]
Another option might be IPC (Inter Process Communication), see: GitHub - Microsoft/IPC: IPC is a C++ library that provides inter-process communication using shared memory on Windows. A .NET wrapper is available which allows interaction with C++ as well.[^]


这篇关于C ++项目如何调用C#对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆