跨 C/C++ 共享通用定义(非托管)和托管 C# 代码
我有一组由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然,我不熟悉您的项目,但是您是否考虑过在 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.
您需要 IDL(接口定义语言),请尝试谷歌搜索:
这完全取决于你想要什么。上述所有技术都具有 IDL 元素,并且都有自己的一套包袱。 我个人会保留低级 C/C++ :D。 因此,我会 Google “Imatix GSL” 并使用提到的技术在 XML 中对问题进行建模并生成数据结构在任何编程语言中——这项技术都非常简单和微妙,需要经验丰富的程序员,所以如果它没有意义,你应该坚持使用 IDL。
-- 编辑:编程技术 --
如果你愿意,你可以通过纯技术来解决问题。当工程的严谨性被打破时,混乱就会随之而来。如果您决定使用防火墙并将问题封装到纯 C/C++ 代码中,您将不必担心接口在您的依赖代码中崩溃——这是因为任何有用的语言都可以与 ABI 您的平台(简单的 C 函数:P)。关键不是公开内部结构,而是公开具有不透明类型的接口,例如表示可以在您的类型上执行的对象和函数的数字句柄。
you need a IDL (interface definition language) try googling:
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.
我曾经想在我的一个项目中这样做,该项目在 C# 代码和 C 代码之间存在严格的分离。理想情况下,C# 代码会从 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: