简体   繁体   English

从C#.NET应用程序调用C / C ++代码

[英]Calling C/C++ Code from C# .NET application

I have a c++ application, i need to call it's class methods from C# app 我有一个C ++应用程序,我需要从C#应用程序中调用它的类方法

I have followed the following tutorial and created a DLL file for the C++ app 我已按照以下教程进行操作,并为C ++应用程序创建了一个DLL文件。

http://msdn.microsoft.com/en-us/library/ms235636.aspx http://msdn.microsoft.com/en-us/library/ms235636.aspx

When i tried to add a reference for the DLL file it gives me error message 当我尝试为DLL文件添加引用时,出现错误消息

a reference to could not be added please make sure that the file is accessible and that it is a valid assembky or COM component

You cannot add an unmanaged DLL as a reference. 您不能添加非托管DLL作为参考。 What's more, you cannot consume C++ classes from an unmanaged DLL in C#. 而且,您不能使用C#中非托管DLL消耗C ++类。 Here are your options: 这是您的选择:

  1. Compile the C++ code into a mixed mode C++/CLI assembly, and wrap the unmanaged C++ classes in ref classes. 将C ++代码编译成混合模式C ++ / CLI程序集,并将非托管C ++类包装在ref类中。 These can be consumed by C# in the usual way by adding the mixed mode C++/CLI assembly as a reference. 通过添加混合模式C ++ / CLI程序集作为参考,这些可以由C#以常规方式使用。
  2. Wrap the unmanaged C++ classes with COM wrappers, and consume these from C# as you would any other COM library. 用COM包装器包装非托管C ++类,并像使用其他COM库一样从C#中使用它们。
  3. Wrap the C++ classes with C style functions and consume them from C# using p/invoke. 用C样式函数包装C ++类,并使用p / invoke从C#中使用它们。

Of these options, the first is usually to be recommended. 在这些选项中,通常推荐第一个。

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

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