提示使用C库从C# [英] Tips for using a C library from C#
问题描述
我有一个在C库,我想从C#中使用它。
I've got a library in C which I'd like to use from C#.
从我收集到了互联网,一个想法是包裹在一个C ++ DLL,和那的DllImport
From what I've gleaned off the internet, one idea is to wrap it in a C++ dll, and DllImport that.
问题是,我要调用的函数有一个相当讨厌的参数设置:包括对函数的引用(这将是一个.NET函数),一对数组(有的写,有的读)
The problem is that the function I want to call has a fairly nasty parameter set: including a reference to a function (which will be a .NET function), and a couple of arrays (some write, some read).
int lmdif(int (*fcn)(int, int, double*, double*, int*),
int m, int n, double* x, double ftol, double xtol, double gtol,
int maxfev, double epsfcn, double factor)
由于像这样的接口,什么是我应该寻找出的脏东西? (和解决方案也请)
Given an interface like this, what are the nasties I should be looking out for? (And solutions too, please)
<子>你为什么不...
<子> - 重写在C#中?我开始,但是它已经机器翻译从FORTRAN,我不太喜欢编码东西,我无法理解。
<子> - 使用现有的.NET库?现在我正在努力,但结果并不完全相同
- Use an existing .NET library? I'm trying it right now, but results are not exactly the same
<子> - 重新编译C ++?我在想这件事,但它看起来像一个很大的痛苦
推荐答案
这是如何我通常用C的DLL从C#进行交互:
This is how I typically interact with C DLLs from C#:
public static unsafe class WrapCDll {
private const string DllName = "c_functions.dll";
[DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void do_stuff_in_c(byte* arg);
}
没有必要用C ++包裹,你会得到所需要的调用约定。
No need to wrap in C++ and you get the needed calling convention.
这篇关于提示使用C库从C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!