3
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
と同じではありませんか?