如何将out/ref外部参数转换为F# [英] How to convert out/ref extern parameters to F#

查看:93
本文介绍了如何将out/ref外部参数转换为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#?

推荐答案

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

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外部参数转换为F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆