【发布时间】:2016-12-23 00:26:13
【问题描述】:
在 F# 中,我使用的是外部 DLL(在本例中为 SDL 图形库),我正在导入我需要的方法,如下所示...
[<DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl)>]
extern int SDL_QueryTexture(nativeint texture, uint32& format, int& access, int& w, int& h)
这很好用,我可以使用以下方法成功调用该方法...
let result = SDLDefs.SDL_QueryTexture(textTexture, &format, &access, &w, &h)
问题在于本机 SDL 方法接受许多指针参数的空值。这在某些情况下是必需的(其功能类似于重载方法)。我找不到从 F# 传递空值调用这些方法的任何方法。
例如,这会因 “没有 null 作为正确值”而失败
let result = SDLDefs.SDL_QueryTexture(textTexture, &format, null, &w, &h)
我阅读了有关属性 [AllowNullLiteral] 的信息,但似乎我只能将其应用于我定义的类型,而不是在我导入的 DLL 中使用的预定义类型。
有什么办法可以做到吗?
【问题讨论】:
-
对于那些需要为
null的指针,使用nativeptr而不是int&,然后使用NativePtr.read/NativePtr.write来获取/设置值。 -
@Fyodor 你能举个例子吗?
extern int SDL_QueryTexture(nativeint texture, uint32& format, nativeptr<int> access, int& w, int& h)无法编译。 -
发布了答案。抱歉耽搁了很长时间,有点忙。