简体   繁体   English

在 Visual Studio 中将数据从 C# 传递到 C++

[英]Pass Data from C# to C++ in Visual Studio

I'm trying to build aa simple C# application to test passing data between languages.我正在尝试构建一个简单的 C# 应用程序来测试语言之间的传递数据。 I have a simple main in C# calling a function that I wish to pass to C++:我在 C# 中有一个简单的main调用一个 function 我希望传递给 C++:

using System;

namespace CS_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            CPP_Sum(5, 2); //pseudo code
        }
    }
}

and then the function in a C++ project:然后是 C++ 项目中的 function:

CPP_Sum(int x, int y)
{
    return x+y;
}

The problem is, I don't even know where to start on how to pass these between each other.问题是,我什至不知道从哪里开始如何在彼此之间传递这些。

This is being done via two projects, CS_Console and CPP_Console, in the same solution in Visual Studio.这是通过 Visual Studio 中的同一解决方案中的两个项目 CS_Console 和 CPP_Console 完成的。

It has already been mentioned by @steveo314. @steveo314 已经提到过。 The easiest way to call a C++-based function from C# is using PInvoke.从 C# 调用基于 C++ 的 function 的最简单方法是使用 PInvoke。 I assume you can create a DLL without any documentation but this looks like it will give you all the information you need to use your DLL from C#.我假设您可以在没有任何文档的情况下创建 DLL, 但这看起来会给您提供使用 C# 中的 DLL 所需的所有信息。 Basically:基本上:

  1. Put your function as a C++ DLL;将您的 function 作为 C++ DLL; then然后
  2. Use a DllImport attribute to create a function prototype in your C# code.使用DllImport属性在 C# 代码中创建 function 原型。

Make sure you export your C++ function definitions.确保导出C++ function 定义。

Daniel's answer above is also correct, but if you really want to dive into the guts of this, you may want to learn C++/CLI , as that's more or less C++ running on top of the .NET framework, with more precise interop, and depending on where/what your DLL is, communication both ways (P/Invoke is just calling C++, not calling back into .NET... usually).丹尼尔上面的回答也是正确的,但如果你真的想深入了解这个问题,你可能想学习C++/CLI ,因为这或多或少是 C++ 运行在 .NET 框架之上,具有更精确的互操作性,并且取决于关于您的 DLL 的位置/内容,双向通信(P/Invoke 只是调用 C++,而不是回调到 .NET ......通常)。

C++/CLI allows this kind of thing: C++/CLI 允许这种事情:

public class Program
{
  public void main(String [] args)
  {
    NativeWrapper wrapper = new NativeWrapper(); // C++/CLI ref class
    wrapper.doStuff("hey there!");
  }
}

In a C++/CLI DLL, in the.h file:在 C++/CLI DLL 中,在 .h 文件中:

// This is C++/CLI

class NativeClass; // Forward declare
ref class NativeWrapper
{
public:
  NativeWrapper();  // Constructor - MUST be in .cpp file, as size of NativeClass not known here
  ~NativeWrapper(); // Destructor - MUST be in .cpp file, same reason as above
  void doStuff(System::String^ inputString);
private:
  NativeClass* mp_impl;
};

In a C++/CLI.cpp file:在 C++/CLI.cpp 文件中:

class NativeClass // True C++ class, could be defined elsewhere!
{
public:
  void nativeDoStuff(const std::string& inputString)
  {
    std::cout << "Previous input was: '" << cachedResult << "'";
    cachedResult = inputString;
  }
private:
  std::string cachedResult{};
};

NativeWrapper::NativeWrapper()
{
  mp_impl = new NativeClass();
}
NativeWrapper::~NativeWrapper()
{
  delete mp_impl;
  mp_impl = nullptr;
}
// Utility function!
std::string _convertClrString(System::String^ instr)
{
  marshal_context context;
  std::string mystring{ context.marshal_as<const char*>(instr) };
  return mystring;
}
void NativeWrapper::doStuff(System::String^ inputString)
{
  auto native_string = _convertClrString(inputString);
  mp_impl->nativeDoStuff(native_string);
}

It's a toy example (necessary includes removed), but hopefully that gets the idea across of what's possible with C++/CLI.这是一个玩具示例(必要的包括已删除),但希望这能让您了解 C++/CLI 的可能性。 The link I put near the top is the main start of the resources at Microsoft for doing this.我放在顶部附近的链接是 Microsoft 用于执行此操作的资源的主要起点。 Keep in mind what files are compiling as .NET, which aren't (you can do it per-file), and what that all means.请记住,哪些文件正在编译为 .NET,哪些不是(您可以按文件执行),以及这一切意味着什么。

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

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