Skip to main content
 首页 » 编程设计

c#之[DllImport()] 可以处理静态 C 库吗

2024年08月12日55jirigala

我可以导入带有 .lib 扩展名的静态 C 库吗?

我有头文件:extern "C"__declspec(dllexport)

我可以使用 P/Invoke 助手从 header 源代码生成必要的代码,但问题仍然存在,

[DllImport()] 可以处理编译后的 C/C++ lib 文件吗?

请您参考如下方法:

DllImport 通过引用模块 (.DLL) 导出表中定义的条目来工作。 .LIB 文件不是模块,也没有按照 .DLL 的导出表。

来自 MSDN:

The DllImportAttribute attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point. - Tell me more

您的问题:

Can [DllImport()] handle static c libraries?

没有。

但您始终可以将 .LIB 链接到 .DLL(假设您了解 c/c++)并 p 调用生成的 DLL,但这是一种间接解决方案,并不意味着 "is" 你的问题。