从 C 标头自动创建 C# 包装器?
有没有办法从 ac 标头自动为 .net 创建 p/invoke 包装器?
当然,我可以手动创建它们,但维护它们会很痛苦,而且我可能会在某个地方犯错误,导致难以调试的崩溃。
我尝试了 SWIG,但它创建了完整的类,其中简单的结构就足够了。 SWIG 的另一个问题是它需要在 c 端添加额外的互操作代码。
我希望输出也能在单声道上工作,但这不是必需的。
我可以使用的另一件事是一个程序,它解析 c 标头,并以良好的中间格式(如 xml)创建输出,我可以从中创建自己的 C# 包装器。
编辑:
PInvoke Interop Assistant 正是我所需要的。
但它有一些小问题:
* 它将“unsigned char*”翻译为我更喜欢 IntPtr 的字符串
* 假设size_t=int=long=32bit。目前这对我来说是正确的,但可能不适用于每个平台。
有没有一种干净的方法来解决这个问题?否则,在转换之前,我将在 C 代码上使用一些查找和替换。
Is there a way to automatically create p/invoke wrappers for .net from a c header?
Of course I could create them by hand, but maintaining them would be painful, and I'd probably make a mistake somewhere resulting in hard to debug crashes.
I tried SWIG, but it created full classes where simple structs would be sufficient.
Another problem with SWIG is that it requires additional interop code on the c side.
I'd prefer if the output worked on mono too, but that is not necessary.
Another thing I could work with is a program which parses the c header, and creates an output in a nice intermediate format like xml, from which I can create the C# wrapper myself.
Edit:
PInvoke Interop Assistant is what I needed.
There are some small issues with it though:
* It translates "unsigned char*" to string where I'd prefer IntPtr
* It assumes that size_t=int=long=32bit. This is currently true for me, but might not true on every platform.
Is there a clean way to fix that? Else I'll use a bit of find and replace on the c code before converting it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PInvoke Interop Assistant 应该更适合您,它是专门为使用 C 代码。
请注意,没有任何工具可以为您提供 100% 有保证的解决方案,C 声明方式太模糊,无法保证完全无故障的结果。由指针引起的问题在 C 代码中普遍存在。无法判断指针是用于读取还是写入内存。或者两者兼而有之。或者谁负责释放所指向的内存。
这也是静态代码分析器的一个主要障碍,如果他们不知道如何使用指针,他们就无法做好工作。他们只能从用法来推断,但这是一个先有鸡还是先有蛋的问题,用法可能是错误的。 Microsoft 通过 SAL 注释和额外标记在自己的标头中解决了该问题这对编译器来说是中性的,但可以被代码分析器解析。它们明确说明了指针的预期用途。
Pinvoke Interop Assistant 也使用它,这就是为什么它可以更好地处理 winapi 声明。但这当然只适用于 Microsoft 标头,这些 SAL 注释通常在忙碌的 C 程序员编写的代码中丢失。
The PInvoke Interop Assistant ought to be a better fit for you, it was specifically designed to work with C code.
Just beware that no tool gives you a 100% guaranteed solution, C declarations are way too ambiguous to guarantee a completely trouble-free result. Trouble caused by pointers, ubiquitous in C code. There's no way to tell that a pointer is used to read or write memory. Or both. Or who is responsible for releasing the memory that is being pointed-to.
This is a major hangup for static code analyzers as well, they can't do a decent job if they don't know how a pointer is used. They can only infer it from usage, but that's a chicken-and-egg problem, the usage might be wrong. Microsoft addressed the problem in their own headers with SAL annotations, extra markup that's neutral to a compiler but can be parsed by a code analyzer. They explicitly state the intended use a pointer.
Also used by the Pinvoke Interop Assistant which is why it can do a better job on winapi declarations. But that of course only works on Microsoft headers, these SAL annotations are normally missing on code written by a busy C programmer.
如果我正确理解你的问题,你基本上是在问是否有办法做 SWIG 所做的事情。
当然,您希望采取一些不同的方式,因此一种选择是采用 SWIG 代码并将其更改为您想要的工作方式。
If I understand your question correctly, you're basically asking if there is a way to do what SWIG does.
Of course, you want to do it a bit differently, so one option would be to take the SWIG code and change it to work the way you'd like.