如何使用现有的 C++ .NET 中的代码 (C#)

发布于 2024-07-12 01:31:58 字数 70 浏览 6 评论 0原文

我想创建一个 C# 项目并实现现有的本机 (C++) 代码。 有谁知道有关它的任何好的教程吗?

谢谢!

I would like to create a C# project and implement the existing native (C++) code. Does anyone know about any good tutorial about it?

Thanks!

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

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

发布评论

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

评论(6

泅渡 2024-07-19 01:31:58

您可以使用 P/Invoke,并且可以调用非托管方法。
我将向您发布一些示例:
是对 MSDN 官方文档的引用。
这是一个社区维护的网站,其中包含大多数常见的 Windows 非托管库以及方法签名和示例

You can use P/Invoke and you can call unmanaged methods.
I'll post you some examples:
This is a reference to MSDN official documentation.
This is a community mantained website with most of the common Windows unmanaged libraries along with method signatures and examples

奶茶白久 2024-07-19 01:31:58

如果您的 C++ 代码是托管的(如您所说),那么我建议您将其保留为 C++,除非您有充分的理由——未来的 C#(等)程序集应该能够仅“按原样”引用它。

If your C++ code is managed (as you say), then I'd suggest that you leave it as C++ unless you have a very good reason to -- future C# (etc.) assemblies should be able to just reference it 'as is'.

眼前雾蒙蒙 2024-07-19 01:31:58

我假设“实现现有代码”意味着您希望从 C# 调用它,并对 C++ 代码进行最少的更改。

我是 C++/CLI in Action 这本书的粉丝和最近购买者,其中有一些内容在线有用的示例章节。

CodeProject 简介是一个很好的起点。

《C++/CLI in Action》的作者在 CodeProject 上发表了许多文章,请向下滚动到 他的索引

关于 P/Invoke 的维基百科文章有很多原因说明您可能不想使用它方法,我同意:

  • 编译器失去类型支持
  • 可能的数据类型或对齐问题,因为您必须手动映射类型
  • 需要固定垃圾收集的对象

MSDN 上的最佳起点是 摘要文章

I assume by implement the existing code you mean you want to call it from C#, with minimal changes to the C++ code.

I'm a fan and recent purchaser of the book C++/CLI in Action which has a couple of useful sample chapters online.

This intro on CodeProject is a good starting point.

The author of C++/CLI in Action has a number of articles on CodeProject, scroll down to the C++/CLI section on his index.

The Wikipedia article on P/Invoke has a number of reasons why you might not want to use that approach, with which I agree:

  • loss of typing support by the compiler
  • possible data type or alignment issues as you have to map types by hand
  • need to pin garbage-collected objects

The best starting point on MSDN is the summary article.

酒中人 2024-07-19 01:31:58

最简单的方法可能是创建一个 C++/CLI 项目,这将使您能够混合托管和非托管 C++。 然后,您可以使用 C# 代码可以直接调用的托管 .Net 包装器来包装现有的非托管 C++ 类。 MSDN 杂志有一篇关于 C++/CLI 的很好的介绍性文章

互操作仅对调用 C 风格函数真正有用。 您不能真正使用它与纯 C++ 类交互。

Probably the easiest way to do this is to create a C++/CLI project, which will enable you to mix managed and unmanaged C++. You can then wrap your existing unmanaged C++ classes with a managed .Net wrapper that your C# code can call directly. MSDN magazine has a good introductory article on C++/CLI.

Interop will only really be useful for calling C-style functions. You can't really use it to interact with pure C++ classes.

饮惑 2024-07-19 01:31:58

我通过实践学到了这一点,所以我没有一个好的教程,但有几件事您需要注意 - 托管 C++ 和 C++/CLI 都与非托管代码集成,几乎没有丑陋的接缝。 C# 的情况并非如此。 在托管 C++ 中,很可能会执行这样的操作:

dotNetForm->Text = S"My Project"; // regular managed code
::SetWindowText(dotNetForm->Handle, "Your Project"); // mixed managed/unmanaged

而在 C# 中,您必须 P/Invoke 到 SetWindowText 中,但实际上并非如此:这只是一个简单的示例,但您需要注意托管 C++ 编译器是一种奇怪的组合,既透明地为您执行大量互操作,同时又不为您执行任何操作(即,它不会执行隐式编组)。

I learned this by doing it, so I don't have a good tutorial, but there are a couple things you need to be aware of - managed C++ and C++/CLI both integrate with unmanaged code with very few ugly seams. This is not the case with C#. In managed C++, it's quite possible to do something like this:

dotNetForm->Text = S"My Project"; // regular managed code
::SetWindowText(dotNetForm->Handle, "Your Project"); // mixed managed/unmanaged

whereas in C# you would have to P/Invoke into SetWindowText--not that you really would: it's just a simple example, but you need to be aware that the managed C++ compiler is a weird combination of doing a lot of interop for you transparently and doing nothing for you at the same time (ie, it will do no implicit marashalling).

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