F# 从F枚举窗口#

F# 从F枚举窗口#,f#,dllimport,F#,Dllimport,我试图从F#调用EnumWindows,但出现以下异常: System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #1': Generic types cannot be marshaled. 我使用的代码: module Win32 = open System open System.Runtime.InteropServices type EnumWind

我试图从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())
模块Win32=
开放系统
open System.Runtime.InteropServices
类型EnumWindowsProc=的委托(IntPtr*IntPtr)->bool
[]
extern bool EnumWindows(EnumWindowsProc回调,IntPtr LPRAM)
让EnumTopWindows()=
let callback=new EnumWindowsProc(fun(hwnd,lparam)->true)
枚举窗口(回调,IntPtr.Zero)
模块测试=
printfn“%A”(Win32.EnumTopWindows())

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

type EnumWindowsProc = delegate of IntPtr * IntPtr -> bool
然后您还必须更改使用它的方式(因为它现在被视为双参数函数):


这怎么会是被称为“相同”的问题的重复?如果我在寻找“如何将EnumWindows与F#一起使用”,这就是我的最终目的。我不可能在另一个问题上结束。。
let EnumTopWindows() =
    let callback = new EnumWindowsProc(fun hwnd lparam -> true)
    EnumWindows(callback, IntPtr.Zero)