gpt4 book ai didi

f# - F# 中的枚举 Windows

转载 作者:行者123 更新时间:2023-12-02 00:29:33 25 4
gpt4 key购买 nike

我尝试从 F# 调用 EnumWindows 并遇到以下异常:

System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #1': Generic types cannot be marshaled.

我使用的代码:

module Win32 =
open System
open System.Runtime.InteropServices
type EnumWindowsProc = delegate of (IntPtr * IntPtr) -> bool
[<DllImport("user32.dll")>]
extern bool EnumWindows(EnumWindowsProc callback, IntPtr lParam)

let EnumTopWindows() =
let callback = new EnumWindowsProc(fun (hwnd, lparam) -> true)
EnumWindows(callback, IntPtr.Zero)

module Test =
printfn "%A" (Win32.EnumTopWindows())

最佳答案

这有点微妙,但是当您在委托(delegate)定义中使用括号时,您明确地告诉编译器创建一个采用元组的委托(delegate) - 然后互操作会失败,因为它无法处理元组。如果没有括号,委托(delegate)将被创建为带有两个参数的普通 .NET 委托(delegate):

type EnumWindowsProc = delegate of IntPtr * IntPtr -> bool

然后您还必须更改使用它的方式(因为它现在被视为二参数函数):

let EnumTopWindows() =
let callback = new EnumWindowsProc(fun hwnd lparam -> true)
EnumWindows(callback, IntPtr.Zero)

关于f# - F# 中的枚举 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25435616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com