gpt4 book ai didi

f# - 将 C# 委托(delegate)转换为 f#

转载 作者:行者123 更新时间:2023-12-02 00:02:10 27 4
gpt4 key购买 nike

如何将委托(delegate)转换为 F#?

委托(delegate):

delegate IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam);

已编辑

我正在做的是在 F# 中使用来自 c# 的托管 API 执行低级键盘 Hook

代码:

[<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

函数

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);

最佳答案

尝试以下操作

type HookProc = delegate of int * nativeint * nativeint -> nativeint

请注意,nativeint 只是 IntPtr 的 F# 名称

关于f# - 将 C# 委托(delegate)转换为 f#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20691096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com