使用C ++ / CLI作为“中间件”的本机C ++中的.NET类

时间:2022-06-01 19:34:26

I have to use a class/assembly made in C# .NET from native C++ application. I suppose I need to make a wrapper class in C++/CLI, that would expose native methods in header files, but use .NET class as needed. Thing that's unclear to me is how to convert data types from .NET to standard C++ types.

我必须使用来自本机C ++应用程序的C#.NET中的类/程序集。我想我需要在C ++ / CLI中创建一个包装类,它会在头文件中公开本机方法,但是根据需要使用.NET类。我不清楚的是如何将数据类型从.NET转换为标准C ++类型。

Does anybody have some sample code to learn from?

有没有人可以学习一些示例代码?

Only text I have found on this is: http://msdn.microsoft.com/en-us/magazine/cc300632.aspx

我发现的文字只有:http://msdn.microsoft.com/en-us/magazine/cc300632.aspx

But text is very old (using Managed C++, not C++/CLI), and lot of things remains unclear

但文本很老(使用托管C ++,而不是C ++ / CLI),很多事情仍然不清楚

1 个解决方案

#1


Yes you need to build a wrapper with C++/CLI around your managed assembly.

是的,您需要在托管程序集周围构建一个包含C ++ / CLI的包装器。

For mixing native and managed types you may want to check this article for sample codes.

对于混合本机和托管类型,您可能需要查看本文以获取示例代码。

Primitive types has int, float, byte are converted for you. But other types has String must be done yourself. For example, if you have a native pointer on a char*, then your C++/CLI class will have to convert it to a String in order to be passed to your Managed C# assembly.

原始类型有int,float,byte为你转换。但其他类型的String必须自己完成。例如,如果char *上有本机指针,那么C ++ / CLI类必须将其转换为String才能传递给Managed C#程序集。

#1


Yes you need to build a wrapper with C++/CLI around your managed assembly.

是的,您需要在托管程序集周围构建一个包含C ++ / CLI的包装器。

For mixing native and managed types you may want to check this article for sample codes.

对于混合本机和托管类型,您可能需要查看本文以获取示例代码。

Primitive types has int, float, byte are converted for you. But other types has String must be done yourself. For example, if you have a native pointer on a char*, then your C++/CLI class will have to convert it to a String in order to be passed to your Managed C# assembly.

原始类型有int,float,byte为你转换。但其他类型的String必须自己完成。例如,如果char *上有本机指针,那么C ++ / CLI类必须将其转换为String才能传递给Managed C#程序集。