使用 C# 代码中的 C 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9093292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use a C library from C# code
提问by murmansk
I have a library in C-language. is it possible to use it in C sharp.
我有一个 C 语言库。是否可以在 C 锐利中使用它。
http://zbar.sourceforge.net/is the link of library i want to use
http://zbar.sourceforge.net/是我想使用的库的链接
采纳答案by Dr. ABT
C Libraries compiled for Windows can be called from C# using Platform Invoke.
可以使用Platform Invoke从 C#调用为 Windows 编译的 C 库。
From MSDN, the syntax of making a C function call is as follows:
从MSDN,进行 C 函数调用的语法如下:
[DllImport("Kernel32.dll", SetLastError=true)]
static extern Boolean Beep(UInt32 frequency, UInt32 duration);
The above calls the function Beep in Kernel32.dll, passing in the arguments frequency and duration. More complex calls are possible passing in structs and pointers to arrays, return values etc...
以上调用Kernel32.dll中的Beep函数,传入参数频率和持续时间。更复杂的调用可能会传递结构和指向数组的指针、返回值等......
You will need to ensure that the C functions available by the C library are exported appropriately, e.g. the Beep function is likely declared like this:
您需要确保 C 库可用的 C 函数被适当地导出,例如 Beep 函数可能声明如下:
#define DllExport __declspec( dllexport )
DllExport bool Beep(unsigned int frequency, unsigned int duration)
{
// C Body of Beep function
}

