简体   繁体   English

将C#类库导入Visual C ++

[英]Importing a C# class library into Visual C++

I have a C# class library that contains methods that need to be used with an external application. 我有一个C#类库,其中包含需要与外部应用程序一起使用的方法。 Unfortunately this external application only supports external APIs in C/C++. 不幸的是,此外部应用程序仅支持C / C ++中的外部API。

Suppose I have a takeIntReturnDoubleArray method in this C# library that takes an integer and returns an array of doubles. 假设我在这个C#库中有一个takeIntReturnDoubleArray方法,该方法采用一个整数并返回一个双精度数组。 All I need to do is have a C++ method that takes an integer, calls the C# library and returns an array of doubles to the calling application. 我需要做的就是拥有一个采用整数的C ++方法,调用C#库并将双精度数组返回给调用应用程序。

So in essence the C++ library is just acting as an intermediary between the C# wrapper and the external application. 因此,从本质上讲,C ++库只是充当C#包装程序和外部应用程序之间的中介。

Is there an easy way to do this? 是否有捷径可寻? Do I have to do anything special on the C# side to allow it to be imported into C++ easily? 我是否需要在C#方面做一些特殊的事情才能将其轻松导入C ++? I have seen some talk of using the #import statement but I really have no idea what I am doing when it comes to C++. 我已经看到过一些关于使用#import语句的讨论,但是我真的不知道在C ++中我在做什么。

What is the approach I should be taking here? 我在这里应该采取什么方法?

You have two main options here: 您在这里有两个主要选择:

  1. C++\\CLI - this allows you to have both managed and unmanaged code in the same source file. C ++ \\ CLI-这使您可以在同一源文件中同时拥有托管和非托管代码。 The managed portion can then call the C# code. 然后,受管部分可以调用C#代码。
  2. COM Interop - expose your .NET type as a COM interface and matching coclass which you can easily use from unmanaged C++. COM Interop-将.NET类型公开为COM接口和匹配的coclass,您可以从非托管C ++轻松使用。

COM Interop is one way to approach this problem. COM Interop是解决此问题的一种方法。 You'll have to have a COM layer for the C# library function(s) you want to expose to the C++ application. 您必须具有要暴露给C ++应用程序的C#库函数的COM层。 This ( http://msdn.microsoft.com/en-us/library/aa302324.aspx#usingframeworktools_topic10 ) might be of interest. 这个( http://msdn.microsoft.com/en-us/library/aa302324.aspx#usingframeworktools_topic10 )可能很有趣。

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

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