P/Invoke 函数重载
我正在尝试从 F# P/Invoke C 库,但遇到了一个特殊问题。我有一个包含所有 extern
函数的模块。底层 C 库有两个同名但参数不同的函数。当然,这在 F# 模块中是不允许的。
module C =
open System.Runtime.InteropServices
[<DllImport("libc", CallingConvention = CallingConvention.Cdecl)>]
extern int setValue(nativeint source, int value)
[<DllImport("libc", CallingConvention = CallingConvention.Cdecl)>]
extern int setValue(nativeint source, string value)
// the previous function declaration cause the following compile-time error:
// Duplicate definition of value 'setValue'
有什么特殊的方法可以解决这个问题吗?我无法更改 C 库。
I'm attempting to P/Invoke a C library from F#, and have encountered a peculiar issue. I have a module containing all my extern
functions. The underlying C library has two functions with the same name, but different arguments. This, of course, is not allowed in an F# module.
module C =
open System.Runtime.InteropServices
[<DllImport("libc", CallingConvention = CallingConvention.Cdecl)>]
extern int setValue(nativeint source, int value)
[<DllImport("libc", CallingConvention = CallingConvention.Cdecl)>]
extern int setValue(nativeint source, string value)
// the previous function declaration cause the following compile-time error:
// Duplicate definition of value 'setValue'
Is there some special way to work around this? I can not alter the C library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EntryPoint
<如果 MSDN 可信(尚未在 F# 中测试),/a> 属性应该有效(例如,使用序数)。为导入的函数命名,例如setValueInt()
和setValueString()
。The
EntryPoint
attribute should work (e.g. with an ordinal), if MSDN can be trusted (haven't tested in F#). Name your imported functions e.g.setValueInt()
andsetValueString()
.