问题
how do I convert delegate to F#?
the delegate:
delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);
Edited
What I'm doing is to do Low Level Keyboard Hook using managed API from c# in F#
Code:
[<DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)>]
extern bool UnhookWindowsHookEx(System.IntPtr hhk);
[<DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)>]
extern System.IntPtr CallNextHookEx(System.IntPtr hhk, int nCode,System.IntPtr wParam, System.IntPtr lParam);
[<DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)>]
extern System.IntPtr GetModuleHandle(string lpModuleName);
[<DllImport("user32.dll")>]
extern System.IntPtr SetWindowsHookEx(int code, HookProc func, System.IntPtr hInstance, int threadID);
Delegate:
type HookProc = delegate of (int * nativeint * nativeint) -> nativeint
Functions
let HookCallback(nCode:int,wParam:System.IntPtr,lParam:System.IntPtr) =
let t = (int)wParam
if t = WM_KEYUP then
let vkCode:int = Marshal.ReadInt32(lParam)
printfn "%A The Pressed key code is : " vkCode
CallNextHookEx(_hookID, nCode, wParam, lParam)
let HookProcF = new HookProc(HookCallback)
let SetHook() =
let curProcess = Process.GetCurrentProcess()
let curModule = curProcess.MainModule
let t =0
let h = GetModuleHandle(curModule.ModuleName)
SetWindowsHookEx(WH_KEYBOARD_LL, HookProcF, h, t);
let HookMoniter() =
let _hookID = SetHook()
UnhookWindowsHookEx(_hookID);
回答1:
Try the following
type HookProc = delegate of int * nativeint * nativeint -> nativeint
Note that nativeint
is just F# name for IntPtr
回答2:
Do you mean call a C# function by delegate with F#?
See the following:
Using c# delegates with f# functions
http://msdn.microsoft.com/en-us/library/dd233206.aspx
来源:https://stackoverflow.com/questions/20691096/convert-c-sharp-delegate-to-f