从 C# 代码使用 C 库

发布于 2024-12-31 22:15:22 字数 147 浏览 1 评论 0原文

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

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

I have a library in C-language. is it possible to use it in C sharp.

http://zbar.sourceforge.net/ is the link of library i want to use

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2025-01-07 22:15:22

可以使用平台调用从C# 调用为Windows 编译的C 库。

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

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

上面调用了函数Kernel32.dll 中发出蜂鸣声,传入参数频率和持续时间。更复杂的调用可能会传入结构体和指向数组的指针、返回值等...

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

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

C Libraries compiled for Windows can be called from C# using Platform Invoke.

From MSDN, the syntax of making a C function call is as follows:

[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...

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:

#define DllExport   __declspec( dllexport )
DllExport bool Beep(unsigned int frequency, unsigned int duration)
{
    // C Body of Beep function
}
白芷 2025-01-07 22:15:22

您可以使用 Swig 为 C 代码创建包装器,以便在 c# 中使用它

You can use Swig to create wrapper for C code in order to use it in c#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文