使用C#代码中的C库

我有一个C语言的库。 是否可以在C sharp中使用它。

http://zbar.sourceforge.net/是我想要使用的库的链接

可以使用Platform Invoke从C# 调用为Windows编译的C库。

从MSDN ,进行C函数调用的语法如下:

[DllImport("Kernel32.dll", SetLastError=true)] static extern Boolean Beep(UInt32 frequency, UInt32 duration); 

上面调用Kernel32.dll中的Beep函数,传递参数频率和持续时间。 更复杂的调用可能会传递结构和指向数组,返回值等…

您需要确保C库可用的C函数被适当导出 ,例如,Beep函数可能声明如下:

 #define DllExport __declspec( dllexport ) DllExport bool Beep(unsigned int frequency, unsigned int duration) { // C Body of Beep function }