从C#调用C++函数?

huangapple go评论53阅读模式
英文:

Call C++ function from C#?

问题

在C#文件中调用C++文件中定义的函数是否可能?我正在使用Visual Studio 2022的WinForm模板作为简单示例。

在我的C++文件中,我定义了一个名为test的测试函数:

#include <iostream>

void test()
{
    std::cout << "test\n";
}

该文件位于我的C# WinForm项目的根文件夹中。

我想在我的C# WinForm文件中实现类似以下的操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fun_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 在这里调用C++函数
        }
    }
}

在C#中,使用Visual Studio 2022是否可能实现类似这样的操作?

感激任何指导。

英文:

I'm attempting to find out whether it's possible to call a function defined in a C++ file from a C# file. I'm using the WinForm template in Visual Studio 2022 as a simple example.

I have a single test function defined in my C++ file:

#include &lt;iostream&gt;

void test()
{
    std::cout &lt;&lt; &quot;test\n&quot;;
}

And that file is located in the root folder of my C# WinForm project.

I was aiming to do something like this in my C# WinForm file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fun_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //test();   &lt;- call C++ function from here
        }
    }
}

Is something like this even possible to do in C# with VS 2022?

Any guidance is appreciated.

答案1

得分: 3

你可以使用P/Invoke从C#中调用C风格函数。但是这个函数需要被导出,编译成一个dll,并且具有兼容的签名。有各种规则来说明诸如字符串、数组和委托等内容如何转换为它们的C等效项。

因此,你不能只是将一个C++函数放入你的C#项目中,并希望一切都能正常工作。你仍然需要一个单独的项目来编译该函数为dll。

然后还有C++/CLI,它是一种介于.Net和C++之间的混合形式。但你仍然需要一个单独的项目才能使用它。

但使用本地互操作的主要原因是当某个库在.Net中不可用时。你很少会根据目的来拆分代码库。可能会有一些情况,可以通过性能来推动,但首先应该尝试尽可能优化C#代码。

英文:

You can call a C-style function from C# using P/Invoke. But the function need to be exported, be compiled to a dll, and have a compatible signature. There all sorts of rules on how things like strings, arrays and delegates are converted to their C equivalents.

So you cannot just plop down a C++ function in your C# project and hope anything will work. You still need a separate project to compile the function to a dll.

Then there is C++/CLI that is a kind of hybrid between .Net and c++. But you still need a separate project for it to work.

But the main reason to use native interop is when some library is not available in .Net. You would rarely split the codebase by purpose. There might be some cases where it could be motivated by performance, but then you should first try all the tricks possible to optimize the C# code first.

huangapple
  • 本文由 发表于 2023年3月9日 15:08:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681414.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定