C++/CLI 或 C# P/Invoke 用于大型 C 库,调用外部函数

发布于 2024-12-04 23:54:46 字数 332 浏览 0 评论 0原文

现在我知道有很多与此相关的问题,但一般来说它们指的是 C++ 库。

我有一个相当重要的 C dll(大约 2800 个函数),我需要为其中大约 70 个可在 C# 中调用的 API 创建一个 API。我一开始使用 P/Invoke,但它非常乏味,因为我有复杂的结构要从 C 代码传递和返回(带有指向其他指针的结构、双指针等),并且它花了我很多时间来获得我需要的东西。

现在我根本不懂 C++/CLI,但我有一点 C++ 经验(虽然不多)。

我想知道是否值得为我的项目努力学习这一点。使用 C++ 包装器是否能让我不必费力处理编组结构、在全局堆中分配指针等?...

非常感谢这个伟大的社区

Now I know there are quite a few questions regarding this but in general they refer to c++ libraries.

I have a fairly important C dll (approximately 2800 functions) and I need to make an API for about 70 of these callable in C#. I started off using P/Invoke but it's pretty tedious because I have complex structures to pass and return from the C code (structures with pointers to others, double pointers, etc) and it's taking me alot of time to get what I need.

Now I don't know C++/CLI at all but I have a bit of experience with C++ (not a lot though).

I am wondering if it's worth making the effort to learn this for my project. Will using a C++ wrapper enable me not to have to struggle with marshalling structures, allocating pointers in global heap, etc or not?....

Thanks a lot and big ups to this great community

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把时间冻结 2024-12-11 23:54:46

这是相当大的表面积。我认为 C++/CLI 会比 P/invoke 更容易。您不需要使用任何 C++ 功能,您可以编写本质上是 C 的内容,并使用 C++/CLI 编译和导出它。

That's quite a large surface area. I think C++/CLI will be easier than P/invoke. You don't need to use any C++ features, you can write what is essentially C and compile and export it with C++/CLI.

救赎№ 2024-12-11 23:54:46

处理 C# 中的本机结构很困难。因此编写 C++/CLI 包装器更容易。您可以轻松地将这些结构转换/包装为ref 类,并在 C# 中使用它们。

void some_function(Some_Struct * ss)
{
  //...
}

struct Some_Struct
{
  int intVal;
  double dblVal;
}

//C++/ClI
public ref class Wrapper
{
  public:
    void CallSomeFunction(int intval, double doubleval)
    {
      Some_Struct * ss = new Some_Struct(); // maybe malloc is more appropriate.
      ss->intVal = intval;
      ss->dblVal = doubleval;
      some_function(ss);
      delete ss; // or Free(ss);
    }
}

//C#
Wrapper wrapper = new Wrapper();
wrapper.CallSomeFunction(10,23.3);

It is hard to deal with native structs from C#. So writing a C++/CLI wrapper is easier. You can convert/wrap those structs to ref classes without too much trouble and use them in C#.

void some_function(Some_Struct * ss)
{
  //...
}

struct Some_Struct
{
  int intVal;
  double dblVal;
}

//C++/ClI
public ref class Wrapper
{
  public:
    void CallSomeFunction(int intval, double doubleval)
    {
      Some_Struct * ss = new Some_Struct(); // maybe malloc is more appropriate.
      ss->intVal = intval;
      ss->dblVal = doubleval;
      some_function(ss);
      delete ss; // or Free(ss);
    }
}

//C#
Wrapper wrapper = new Wrapper();
wrapper.CallSomeFunction(10,23.3);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文