在C#库和C ++(CLR)之间传递对象 [英] Passing objects between C# library and C++ (CLR)
问题描述
如何将对象从C#库传递到C ++.
How to pass objects from C# library to C++.
我可以调用一个返回void
或int
的函数,而不会出现任何问题.
I can call a function which returns void
or int
without any issue.
现在考虑在C#中使用以下功能,
Now consider the following function in C#,
List<CSharpClass> CSharpFunction(string Input)
我的C#类包含的位置
public class CSharpClass
{
string mystring = string.Empty;
byte[] bytearray = null;
public byte[] bytearray
{
get { return bytearray ; }
set { bytearray = value; }
}
public string mystring
{
get { return mystring ; }
set { mystring = value; }
}
}
现在,我想在我的C ++中使用此列表.所以我创建了,
Now, I want use this List in my C++. So I have created,
typedef std::vector<class CSharpClass> MyDetailList;
这是正确的方法吗?如果不是,我需要在C ++中使用什么?
Is it the right way ?? If not what I need to use in C++?
推荐答案
If you want to call a C# dll from C++ code, you can follow this article.
In a nutshell, you're going to have to:
- Write a Managed DLL containing your CSharpClass
- Register the Managed DLL for Use with COM or with Native C++
- Call the Managed DLL from Native C++ Code
此SO问题也很重要,并且包含替代解决方案如果要避免使用COM
This SO question is also relevant, and contains alternative solutions if you want to avoid using COM
最初的误导答案:
您可以查看这篇文章以获得不错的教程.
简而言之,您将必须:
-从您的C ++代码中编译一个DLL
-在C#和C ++之间进行marshall(翻译")您的类
-使用DllImport声明从C#加载dll
-调用导入的方法
You can check this article for a fairly good tutorial.
In a nutshell, you're going to have to:
- Compile a dll from your c++ code
- marshall ("translate") your class between C# and C++
- load the dll from C#, by using DllImport declaration
- call the imported method
这篇关于在C#库和C ++(CLR)之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!