本文介绍了如何将 out/ref extern 参数转换为 F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的 C# extern 声明:

I've got a C# extern declaration that goes like this:

    [DllImport("something.dll")]
    public static extern ReturnCode GetParent(IntPtr inRef, out IntPtr outParentRef);

如何将其翻译成 F#?

How to translate that to F#?

推荐答案

您可以尝试类似下面的代码.我不知道 ReturnCode 是什么,所以下面的代码期望它是一个整数.对于任何更复杂的类型,您需要使用 [] 属性,如 A-Dubb 引用的答案中所述.

You can try something like the code below. I don't know what ReturnCode is, so the code below expects it is an integer. For any more complex type, you'll need to use [<Struct>] attribute as in the answer referenced by A-Dubb.

type ReturnCode = int

[<System.Runtime.InteropServices.DllImport("something.dll")>]
extern ReturnCode GetParent(System.IntPtr inRef, System.IntPtr& outParentRef);

要调用该函数,您可以这样写:

To call the function, you'd write something like this:

let mutable v = nativeint 10
let n = GetParent(nativeint 0, &v)

顺便说一句:您能否也发布一个示例 C 代码来实现 something.dll 中的功能?如果是,我们可以在发送答案之前尝试运行该解决方案...

BTW: Could you also post a sample C code that implements the function in something.dll? If yes, we could try running the solution before sending an answer...

这篇关于如何将 out/ref extern 参数转换为 F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 21:45