使用 MarshalAs 的 P/Invoke 签名的 F# 语法 [英] F# syntax for P/Invoke signature using MarshalAs

查看:19
本文介绍了使用 MarshalAs 的 P/Invoke 签名的 F# 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定它的语法.我正在尝试将此 C# 代码转换为 F#.

I'm unsure of the syntax for this. I'm trying to translate this C# code into F#.

struct LASTINPUTINFO
{
    public uint cbSize;
    public uint dwTime;
}

public class IdleTimer
{
    [DllImport("User32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}

这是我目前所拥有的.

type LASTINPUTINFO = {
    cbSize : UInt32;
    dwTime : UInt32;
}

type IdleTimer =
    [<DllImport("User32.dll")>]
    [<return: MarshalAs(UnmanagedType.Bool)>]
    extern GetLastInputInfo(plii : LASTINPUTINFO ref)

推荐答案

除了 Brian 的评论之外,可能值得指出的是 F# extern 签名相当忠实地反映了 C 签名,因此而不是使用 [<In>][<Out>] 引用的属性,您可能只需将参数声明为 LASTINPUTINFO* plii,然后使用 & 将引用传递给本地实例.&调用函数时的操作符.

In addition to Brian's comment, it may be worth pointing out that F# extern signatures reflect C signatures fairly faithfully, so that rather than using the [<In>][<Out>] attributes on the reference you can probably just declare the parameter as LASTINPUTINFO* plii, and then pass a reference to a local instance using the && operator when calling the function.

这篇关于使用 MarshalAs 的 P/Invoke 签名的 F# 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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