open System
open System.Diagnostics
open System.Runtime.InteropServices
module PInvoke =
type EnumThreadDelegate= delegate of (IntPtr * IntPtr) -> bool
type ComArrayList() =
inherit System.Collections.ArrayList()
[<DllImport("user32.dll")>]
extern [<return: MarshalAs(UnmanagedType.Bool)>] bool private EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
let getThreadWindows (threadId:int) : _ list =
let items = ResizeArray()
let withData (hWnd:IntPtr, lParam:IntPtr) =
let _ = items.Add(hWnd,lParam)
true
let f = EnumThreadDelegate withData
EnumThreadWindows (threadId, f, IntPtr.Zero) |> ignore<bool>
items
|> Seq.cast<IntPtr*IntPtr>
|> List.ofSeq
let lp = Process.GetProcesses() |> Seq.filter(fun p -> p.ProcessName.StartsWith("L")) |> Seq.minBy(fun p -> p.StartTime)
lp.Threads
|> Seq.cast<ProcessThread>
|> Seq.map (fun t -> t.Id)
|> Seq.map PInvoke.getThreadWindows
|> List.ofSeq
给出System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #1': Generic types cannot be marshaled
但是这个编译并运行:
open System
open System.Diagnostics
open System.Runtime.InteropServices
module PInvoke =
type EnumThreadDelegate= delegate of IntPtr * IntPtr -> bool
type ComArrayList() =
inherit System.Collections.ArrayList()
[<DllImport("user32.dll")>]
extern [<return: MarshalAs(UnmanagedType.Bool)>] bool private EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
let getThreadWindows (threadId:int) : _ list =
let items = ResizeArray()
let withData (hWnd:IntPtr) (lParam:IntPtr) =
let _ = items.Add(hWnd,lParam)
true
let f = EnumThreadDelegate withData
EnumThreadWindows (threadId, f, IntPtr.Zero) |> ignore<bool>
items
|> Seq.cast<IntPtr*IntPtr>
|> List.ofSeq
let lp = Process.GetProcesses() |> Seq.filter(fun p -> p.ProcessName.StartsWith("L")) |> Seq.minBy(fun p -> p.StartTime)
lp.Threads
|> Seq.cast<ProcessThread>
|> Seq.map (fun t -> t.Id)
|> Seq.map PInvoke.getThreadWindows
|> List.ofSeq
为什么两个都编译,但有一个例外?
delegate of (IntPtr*IntPtr) -> bool
和delegate of IntPtr*IntPtr->bool
之间的区别是什么?
它们不应该是同一个东西吗? (IntPtr*IntPtr)
与IntPtr*IntPtr
不一样吗?
答案 0 :(得分:4)
我认为
delegate of (IntPtr*IntPtr) -> bool
是一个单参数委托,它采用Tuple<IntPtr,IntPtr>
,而
delegate of IntPtr*IntPtr -> bool
是一个双参数委托,它将两个IntPtr
作为它的两个参数。