简体   繁体   English

将SWIG与C#一起使用时的最佳做法是什么?

[英]What are the best practices when using SWIG with C#?

Has anybody out there used the SWIG library with C#? 有没有人在那里使用带有C#的SWIG库? If you have, what pitfalls did you find and what is the best way to use the library? 如果你有,你找到了什么陷阱以及使用该库的最佳方式是什么? I am thinking about using it as a wrapper for a program that was written in C and I want to wrap the header files where I can use them in my .NET application. 我正在考虑将它用作用C编写的程序的包装器,我想将头文件包装在我可以在我的.NET应用程序中使用它们。

Edit: Some clarification on target OS's. 编辑:对目标操作系统的一些澄清。

I plan on running the application on Linux and Windows, therefore the reason I am looking into SWIG. 我计划在Linux和Windows上运行应用程序,因此我正在研究SWIG。 P/Invoke is not an option. P / Invoke不是一个选项。

For my last project, here's the entire C# SWIG configuration file: 对于我的上一个项目,这是整个C#SWIG配置文件:

%module mdProject

%{
#include "mdProject.h"
%}

I compiled it in SWIG with: 我在SWIG编译它:

swig -csharp -c++ -I../../Include mdProject.i

This generated a Project.cxx which I compiled and linked directly into the 'main' DLL, so I didn't need a second C++ 'helper' DLL. 这生成了一个Project.cxx,我编译并直接链接到'main'DLL,所以我不需要第二个C ++'helper'DLL。 SWIG also generated a bunch of C# files which I compiled into a .NET DLL. SWIG还生成了一堆C#文件,我将其编译成.NET DLL。 My other wrappers (Java, PHP, etc) do use a helper DLL. 我的其他包装器(Java,PHP等)确实使用了辅助DLL。

As @patrick mentioned, SWIG uses P/Invoke, so if you have a problem with that, you'll need to find another solution. 正如@patrick所提到的,SWIG使用P / Invoke,所以如果你遇到问题,你需要找到另一个解决方案。

If you use types that stray from the ordinary (voids, structures, etc), you will have to do some extra work to get it right, but for the average API using int's, char*'s etc, it's fine. 如果你使用偏离普通类型(空洞,结构等)的类型,你将不得不做一些额外的工作来使它正确,但对于使用int,char *等的普通API,它没关系。

I think the mistake the earlier posters did was read the docs and not look at the examples. 我认为早期海报所犯的错误是阅读文档而不是看例子。

A few hours ago I needed to interface some C++ classes to C#. 几个小时前,我需要将一些C ++类与C#接口。 I looked in my Swig dir (I already had it for other work), found the directory Examples/csharp/class , browsed the code, loaded the solution, grokked it, copied it, put in my code, it worked, my job was done. 我查看了我的Swig目录(我已将其用于其他工作),找到目录Examples/csharp/class ,浏览代码,加载解决方案,grokked它,复制它,放入我的代码,它工作,我的工作是完成。

With that said, generated P/Invoke code isn't a solution for all needs. 话虽如此,生成的P / Invoke代码并不是满足所有需求的解决方案。 Depending on your project, it may be just as simple to write some simple API wrappers yourself or write managed C++ (Look up SlimDX for a superb example of this). 根据您的项目,自己编写一些简单的API包装器或编写托管C ++可能同样简单(查找SlimDX以获得极好的示例)。

For my needs, it was simple and easy - I had mystuff.dll , and now in addition I can ship mystuffnet.dll . 根据我的需要,它简单易行 - 我有mystuff.dll ,现在另外我可以发送mystuffnet.dll I'll agree that the doc is difficult to get into. 我同意该文档很难进入。

Edit: I noticed the OP only mentioned C. For that, you don't really need Swig, just use the usual C#/C DLLImport interop syntax . 编辑:我注意到OP只提到了C.为此,你真的不需要Swig,只需使用通常的C#/ C DLLImport互操作语法 Swig becomes useful when you want to let C++ classes be invoked from C#. 当您想要从C#调用C ++类时,Swig会很有用。

I did attempt to use SWIG to wrap a project C++ for using in .NET a few years ago. 几年前,我确实尝试使用SWIG包装项目C ++,以便在.NET中使用。

I didn't get very far as it was a massive giant pain to produce the configuration that SWIG required. 我没有走得太远,因为产生SWIG所需的配置是一个巨大的巨大痛苦。 At the time I just wanted a solution, not to learn another language/api/etc. 当时我只想要一个解决方案,而不是学习另一种语言/ api /等。 SWIG may be easier to use these days, I couldn't tell you. 这些天SWIG可能更容易使用,我无法告诉你。

We ended up using Managed C++ to wrap the C++ project. 我们最终使用托管C ++来包装C ++项目。 It worked really well. 它运作得很好。

If you're just invoking functions straight out of a dll, I'd suggest not worrying about either of the above, and just using P/Invoke 如果您只是直接从dll调用函数,我建议不要担心上述任何一个,只是使用P / Invoke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中使用@的最佳实践 - Best practices for using @ in C# C#-使用数组创建自定义列表时的最佳做法是什么 - C# - What are the best practices when creating custom lists using arrays C# 使用,=null, Count > 0. and .Any() 时的最佳实践 - C# Best Practices when using !=null, Count > 0, and .Any() 在C#中处理异常的最佳实践是什么? - What are best practices for handling exceptions in C#? c#+ WebForms + static - 最佳实践是什么? - c# + WebForms + static — what are the best practices? 使用swig for C#时的TypeInitializationException - TypeInitializationException when using swig for C# 使用C#Intellisense注释的最佳实践 - Best Practices on using C# Intellisense Comments 将c#webapi部署到AWS beantalk中时,存储连接字符串的最佳实践是什么? - What is the best practices for storing connections string when deploying c# webapi into aws beanstalk? 在C#中使用ADO.NET格式化内联sql的最佳实践是什么? - What are the best practices on formatting inline sql using ADO.NET in C# 使用事务和错误处理从 C# 在 oracle 表中上传批量数据的最佳实践是什么? - What are the best practices to upload bulk data in oracle table from C# using transaction and error handling?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM