跨 C/C++ 共享通用定义(非托管)和托管 C# 代码

发布于 2024-08-16 04:57:04 字数 187 浏览 8 评论 0原文

我有一组由 C# 托管组件和非托管 C/C++ 组件使用的结构定义。目前,相同的结构定义分别存在于 C/C++ 和 C# 代码中 - 导致重复和相关的混乱。维护可在 C# 和 C/C++ 中使用的单一定义的最佳方法是什么? 谢谢! Amit

P.S.:我是一名 C/C++ 人员,因此如果有一种明显的方法可以做到这一点,我可能会完全错过它!

I have a set of struct definitions that are used by both C# managed components and unmanaged C/C++ components. Right now, the identical struct definitions exist separately in C/C++ and C# code - causing duplication and related chaos. What's the best way to maintain single definitions that can be used from both C# and C/C++?
Thanks!
Amit

P.S.: I'm a C/C++ guy so if there's an obvious way to do this, I might be missing it completely!

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

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

发布评论

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

评论(3

偷得浮生 2024-08-23 04:57:05

显然,我不熟悉您的项目,但是您是否考虑过在 C++/CLI?通过 C++/CLI 编译器为您提供的“It Just Works”黑客功能,您很多时候将能够与本机代码来回编组和共享托管类型。

再说一遍,如果没有更多细节,我不知道它是否适合您,但可能值得研究一下。

I'm not familiar with your project(s), obviously, but have you considered building a managed bridge for your library in C++/CLI? With the "It Just Works" hackering the C++/CLI compiler does for you, many times you'll be able to marshal and share managed types with native code and back and forth.

Again, I don't know if it's right for you without more specifics, but it might be worth looking into.

淡笑忘祈一世凡恋 2024-08-23 04:57:05

您需要 IDL(接口定义语言),请尝试谷歌搜索:

  1. protocol buffers。
  2. ICE(互联网通信引擎)。
  3. 也许是微软COM?
  4. --编辑:新条目 -- 微软似乎有一个 IDL 编译器

这完全取决于你想要什么。上述所有技术都具有 IDL 元素,并且都有自己的一套包袱。 我个人会保留低级 C/C++ :D。 因此,我会 Google “Imatix GSL” 并使用提到的技术在 XML 中对问题进行建模并生成数据结构在任何编程语言中——这项技术都非常简单和微妙,需要经验丰富的程序员,所以如果它没有意义,你应该坚持使用 IDL。

-- 编辑:编程技术 --

如果你愿意,你可以通过纯技术来解决问题。当工程的严谨性被打破时,混乱就会随之而来。如果您决定使用防火墙并将问题封装到纯 C/C++ 代码中,您将不必担心接口在您的依赖代码中崩溃——这是因为任何有用的语言都可以与 ABI 您的平台(简单的 C 函数:P)。关键不是公开内部结构,而是公开具有不透明类型的接口,例如表示可以在您的类型上执行的对象和函数的数字句柄。

you need a IDL (interface definition language) try googling:

  1. protocol buffers.
  2. ICE (internet communication engine).
  3. Perhaps Microsoft COM ?.
  4. --edit: new entry -- it appears microsoft has an IDL compiler.

It all depends on what you want. all the above technologies have an IDL element to them, and come with their own set of baggage. I personally would stay low level C/C++ :D. So I would Google "Imatix GSL" and use the mentioned technology to model the problem in XML and generate the data structures in any programming language -- this technology is very simple and subtle and requires an experience programmer so if it doesn't make sense you should stick with an IDL.

-- edit: programming technique --

You can solve the problem by pure technique if you like. Chaos ensues when the rigor of engineering breaks down. If you make a decision to firewall and encapsulate the problem into pure C/C++ code you won't have to worry about the interface falling appart in your dependant code -- this is because any usefull language can interface with the ABI of your platform (simple C functions :P). The crux is not to expose internals, but just an interface with opaque types, such as numeric handles that represent objects and functions that may be performed on your types.

你是我的挚爱i 2024-08-23 04:57:05

我曾经想在我的一个项目中这样做,该项目在 C# 代码和 C 代码之间存在严格的分离。理想情况下,C# 代码会从 C 代码借用头文件,但是:

  • 由于 C# 不支持包含,我不知道如何共享结构的定义并将其包含在 C# 或 C/C++ 代码中
  • 无论如何,将结构定义放在单独的标头中并不方便,
  • 我不想依赖 IDL 或自定义“解析 C 标头并仅提取结构定义”预处理步骤

I once wanted to do so in one of my projects that had a hard separation between C# code and C code. Ideally, the C# code would have borrowed header files from the C code but:

  • since C# doesn't support include, I don't see how you could share the definition of a structure and include it both in your C# or C/C++ code
  • having my structure definitions in separate headers wasn't convenient anyway
  • I didn't want to rely on IDL or custom "parse the C headers and only extract structures definitions" preprocessing step
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文