Ben*_*Ben 9 pinvoke winapi f# delegates
我正在尝试在F#应用程序中使用P/Invoke设置一个低级键盘钩子.Win32函数SetWindowsHookEx接受HOOKPROC第二个参数,我将其表示为委托(int * IntPtr * IntPtr) -> IntPtr,类似于在C#中处理它的方式.在调用方法时,我得到一个MarshalDirectiveException声明,委托参数无法封送,因为
通用类型无法编组
我不确定如何涉及泛型,因为具体指定了所有类型.任何人都可以对此有所了解吗?代码如下.
编辑
这可能与F#编译器处理类型签名的方式有关 - Reflector指示委托LowLevelKeyboardProc实现为接受一个类型的参数的方法Tuple<int, IntPtr, IntPtr>- 并且将存在不可编组的泛型类型.有没有办法解决这个问题,或者F#函数是不是能够被编组到本机函数指针?
let WH_KEYBOARD_LL = 13
type LowLevelKeyboardProc = delegate of (int * IntPtr * IntPtr) -> IntPtr
[<DllImport("user32.dll")>]
extern IntPtr SetWindowsHookEx(int idhook, LowLevelKeyboardProc proc, IntPtr hMod, UInt32 threadId)
[<DllImport("kernel32.dll")>]
extern IntPtr GetModuleHandle(string lpModuleName)
let SetHook (proc: LowLevelKeyboardProc) =
use curProc = Process.GetCurrentProcess ()
use curMod = curProc.MainModule
SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curMod.ModuleName), 0u)
Run Code Online (Sandbox Code Playgroud)
ild*_*arn 13
你的LowLevelKeyboardProc定义是错误的.改变
type LowLevelKeyboardProc = delegate of (int * IntPtr * IntPtr) -> IntPtr
Run Code Online (Sandbox Code Playgroud)
至
type LowLevelKeyboardProc = delegate of int * IntPtr * IntPtr -> IntPtr
Run Code Online (Sandbox Code Playgroud)
还是更好
type LowLevelKeyboardProc = delegate of int * nativeint * nativeint -> nativeint
Run Code Online (Sandbox Code Playgroud)
还是更好
[<StructLayout(LayoutKind.Sequential)>]
type KBDLLHOOKSTRUCT =
val vkCode : uint32
val scanCode : uint32
val flags : uint32
val time : uint32
val dwExtraInfo : nativeint
type LowLevelKeyboardProc =
delegate of int * nativeint * KBDLLHOOKSTRUCT -> nativeint
Run Code Online (Sandbox Code Playgroud)
在上述所有情况下,proc都需要使用curried形式而不是tupled形式.
另外请注意,您应该添加SetLastError = true到所有的extern编辑功能,其文档中说调用GetLastError失败后(这是情况下GetModuleHandle,SetWindowsHookEx和UnhookWindowsHookEx).这样,如果有任何失败(你应该检查返回值...),你可以简单地提出Win32Exception或调用Marshal.GetLastWin32Error以获得适当的诊断.
编辑:为了清楚起见,这里是我在本地成功测试的所有P/Invoke签名:
[<Literal>]
let WH_KEYBOARD_LL = 13
[<StructLayout(LayoutKind.Sequential)>]
type KBDLLHOOKSTRUCT =
val vkCode : uint32
val scanCode : uint32
val flags : uint32
val time : uint32
val dwExtraInfo : nativeint
type LowLevelKeyboardProc = delegate of int * nativeint * KBDLLHOOKSTRUCT -> nativeint
[<DllImport("kernel32.dll")>]
extern uint32 GetCurrentThreadId()
[<DllImport("kernel32.dll", SetLastError = true)>]
extern nativeint GetModuleHandle(string lpModuleName)
[<DllImport("user32.dll", SetLastError = true)>]
extern bool UnhookWindowsHookEx(nativeint hhk)
[<DllImport("user32.dll", SetLastError = true)>]
extern nativeint SetWindowsHookEx(int idhook, LowLevelKeyboardProc proc, nativeint hMod, uint32 threadId)
Run Code Online (Sandbox Code Playgroud)
另请注意,如果您更喜欢值语义,这也可以同样工作KBDLLHOOKSTRUCT:
[<Struct; StructLayout(LayoutKind.Sequential)>]
type KBDLLHOOKSTRUCT =
val vkCode : uint32
val scanCode : uint32
val flags : uint32
val time : uint32
val dwExtraInfo : nativeint
type LowLevelKeyboardProc = delegate of int * nativeint * byref<KBDLLHOOKSTRUCT> -> nativeint
Run Code Online (Sandbox Code Playgroud)