[英]How can I pass an F# delegate to a P/Invoke method expecting a function pointer?
I'm attempting to set up a low-level keyboard hook using P/Invoke in an F# application. 我正在尝试在F#应用程序中使用P / Invoke设置一个低级键盘钩子。 The Win32 function SetWindowsHookEx
takes a HOOKPROC
for its second argument, which I've represented as a delegate of (int * IntPtr * IntPtr) -> IntPtr
, similar to how this would be handled in C#. Win32函数SetWindowsHookEx
为其第二个参数采用HOOKPROC
,我将其表示为(int * IntPtr * IntPtr) -> IntPtr
的委托,类似于在C#中处理它的方式。 When calling the method, I get a MarshalDirectiveException
stating that the delegate parameter cannot be marshaled because 调用方法时,我得到一个MarshalDirectiveException
指出委托参数不能被封送,因为
Generic types cannot be marshaled 通用类型无法编组
I'm not sure how generics are involved, as all types are concretely specified. 我不确定如何涉及泛型,因为具体指定了所有类型。 Can anyone shed some light on this? 任何人都可以对此有所了解吗? Code follows. 代码如下。
EDIT 编辑
This may have to do with the way the F# compiler deals with type signatures - Reflector indicates that the delegate LowLevelKeyboardProc
is implemented as a method which accepts one argument of type Tuple<int, IntPtr, IntPtr>
- and there would be the un-marshalable generic type. 这可能与F#编译器处理类型签名的方式有关 - Reflector指示委托LowLevelKeyboardProc
被实现为接受一个类型为Tuple<int, IntPtr, IntPtr>
参数的方法 - 并且将存在不可编组的通用类型。 Is there a way around this somehow, or are F# functions simply not capable of being marshaled to native function pointers? 有没有办法解决这个问题,或者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)
Your LowLevelKeyboardProc
definition is wrong. 您的LowLevelKeyboardProc
定义是错误的。 Change from 改变
type LowLevelKeyboardProc = delegate of (int * IntPtr * IntPtr) -> IntPtr
to 至
type LowLevelKeyboardProc = delegate of int * IntPtr * IntPtr -> IntPtr
or better yet 还是更好
type LowLevelKeyboardProc = delegate of int * nativeint * nativeint -> nativeint
or better yet 还是更好
[<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
In all of the above cases, proc
will need to use curried form rather than tupled form. 在上述所有情况中, proc
都需要使用curry形式而不是tupled形式。
Also note that you should add SetLastError = true
to all of the extern
ed functions whose documentation says to call GetLastError
upon failure (which is the case for GetModuleHandle
, SetWindowsHookEx
, and UnhookWindowsHookEx
). 另请注意,您应该将SetLastError = true
添加到所有extern
ed函数,这些函数的文档说失败时调用GetLastError
( GetModuleHandle
, SetWindowsHookEx
和UnhookWindowsHookEx
就是这种情况)。 That way if any fail (and you should be checking the return values...), you can simply raise a Win32Exception
or call Marshal.GetLastWin32Error
to get proper diagnostics. 这样如果有任何失败(你应该检查返回值......),你可以简单地引发Win32Exception
或调用Marshal.GetLastWin32Error
来获得适当的诊断。
EDIT : Just for the sake of clarity, here are all the P/Invoke signatures I successfully tested locally: 编辑 :为了清楚起见,这里是我在本地成功测试的所有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)
Also note that this would work equally as well, if you prefer value semantics for KBDLLHOOKSTRUCT
: 另请注意,如果您更喜欢KBDLLHOOKSTRUCT
值语义,这也同样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
Have you tried to use managed C++ with this. 您是否尝试过使用托管C ++? It can make a lot of the translation pretty seamless. 它可以使很多翻译非常无缝。 You won't need P/Invoke then. 那么你不需要P / Invoke。
EDIT: I'd like to note one fairly important thing: compiler will do more type-checking for you. 编辑:我想注意一个相当重要的事情:编译器会为你做更多的类型检查。 I am sure you love your type-checking, since you use F# for the rest of the app (hopefully). 我相信你喜欢你的类型检查,因为你在应用程序的其余部分使用F#(希望如此)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.