现想在VB.net中实现,曾参考过使用SetWindowsHookEx的例子,但在手工用PostMessage
向该窗口发消息时,经常报错,不知何解..谁能提供一份完整的例子参考??
小弟初学.net,往各位高手赐教!
14 个解决方案
#1
可能是调用api细节的问题,你可以去www.pinvoke.net查找相关的vb.net例子
#2
至于hook,参看:
http://www.codeproject.com/info/search.asp?cats=3,5,6&searchkw=hook&sd=15%20Nov%201999&ed=1%20Aug%202006&Page=1
http://www.codeproject.com/info/search.asp?cats=3,5,6&searchkw=hook&sd=15%20Nov%201999&ed=1%20Aug%202006&Page=1
#3
谢谢 Knight94(愚翁)
出错消息是这样
"对 PInvoke 函数“..\Module1::PostMessage”的调用导致堆栈不对称。原因可能是托管的 Invoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。"
能帮我分析一下具体原因吗?再次谢谢!
出错消息是这样
"对 PInvoke 函数“..\Module1::PostMessage”的调用导致堆栈不对称。原因可能是托管的 Invoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。"
能帮我分析一下具体原因吗?再次谢谢!
#4
这是网上找到的例子,这种方法,能处理PostMessage向它发送的消息吗??
Module Module1
Public frm1 As New Form1() ‘这个的作用,最后再说
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, _
ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, _
ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Const PM_KEY_SPACE = &H20
Public Enum HookType
WH_KEYBOARD = 2
End Enum
Public Sub UnHook() ‘解Hook
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook() ‘设置Hook
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD, AddressOf MyKeyboardProc, 0, GetCurrentThreadId())
End Function
Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyKeyboardProc = 0
If nCode < 0 Then
MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = PM_KEY_SPACE Then
MyKeyboardProc = 1
‘写入你自己的代码
frm1.textbox1.text=”HOOK成功!”
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
Module Module1
Public frm1 As New Form1() ‘这个的作用,最后再说
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, _
ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, _
ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Const PM_KEY_SPACE = &H20
Public Enum HookType
WH_KEYBOARD = 2
End Enum
Public Sub UnHook() ‘解Hook
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook() ‘设置Hook
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD, AddressOf MyKeyboardProc, 0, GetCurrentThreadId())
End Function
Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyKeyboardProc = 0
If nCode < 0 Then
MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = PM_KEY_SPACE Then
MyKeyboardProc = 1
‘写入你自己的代码
frm1.textbox1.text=”HOOK成功!”
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
#5
再顶一次,如果没人回答就结贴了
#6
调用哪个函数出的错
#7
PostMessage
#8
MARK
#9
你把发消息这段贴出来。
#10
to Knight94(愚翁)
我写得乱七八糟就不贴了,你看上面的例子就行了,调试时是对的.例子接收的是空格键的消息,现在的问题是,如果我用PostMessage要给该窗体发自定义消息时,该如何写?函数HOOKPro(nCode,wParam ,lParam)与PostMessage(hwnd,nMsg,wParam,lParam)中的参数不一致怎么办?
我写得乱七八糟就不贴了,你看上面的例子就行了,调试时是对的.例子接收的是空格键的消息,现在的问题是,如果我用PostMessage要给该窗体发自定义消息时,该如何写?函数HOOKPro(nCode,wParam ,lParam)与PostMessage(hwnd,nMsg,wParam,lParam)中的参数不一致怎么办?
#11
to 函数HOOKPro(nCode,wParam ,lParam)与PostMessage(hwnd,nMsg,wParam,lParam)中的参数不一致怎么办
PostMessage函数的参数,
第一个是窗体handle,
第二个是消息类型,
后两个可以沿用HookPro中的后两个。
PostMessage函数的参数,
第一个是窗体handle,
第二个是消息类型,
后两个可以沿用HookPro中的后两个。
#12
调用 PostMessage(frm1.Handle, MSG_YB_INTEMP, 1, 1)'自定义消息
出错:
"对 PInvoke 函数“..\Module1::PostMessage”的调用导致堆栈不对称。原因可能是托管的 Invoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。"
#13
mark
#14
结贴了
#1
可能是调用api细节的问题,你可以去www.pinvoke.net查找相关的vb.net例子
#2
至于hook,参看:
http://www.codeproject.com/info/search.asp?cats=3,5,6&searchkw=hook&sd=15%20Nov%201999&ed=1%20Aug%202006&Page=1
http://www.codeproject.com/info/search.asp?cats=3,5,6&searchkw=hook&sd=15%20Nov%201999&ed=1%20Aug%202006&Page=1
#3
谢谢 Knight94(愚翁)
出错消息是这样
"对 PInvoke 函数“..\Module1::PostMessage”的调用导致堆栈不对称。原因可能是托管的 Invoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。"
能帮我分析一下具体原因吗?再次谢谢!
出错消息是这样
"对 PInvoke 函数“..\Module1::PostMessage”的调用导致堆栈不对称。原因可能是托管的 Invoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。"
能帮我分析一下具体原因吗?再次谢谢!
#4
这是网上找到的例子,这种方法,能处理PostMessage向它发送的消息吗??
Module Module1
Public frm1 As New Form1() ‘这个的作用,最后再说
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, _
ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, _
ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Const PM_KEY_SPACE = &H20
Public Enum HookType
WH_KEYBOARD = 2
End Enum
Public Sub UnHook() ‘解Hook
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook() ‘设置Hook
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD, AddressOf MyKeyboardProc, 0, GetCurrentThreadId())
End Function
Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyKeyboardProc = 0
If nCode < 0 Then
MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = PM_KEY_SPACE Then
MyKeyboardProc = 1
‘写入你自己的代码
frm1.textbox1.text=”HOOK成功!”
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
Module Module1
Public frm1 As New Form1() ‘这个的作用,最后再说
Declare Function GetCurrentThreadId Lib "kernel32" Alias "GetCurrentThreadId" () As Integer
Declare Function SetWindowsHookEx Lib "user32" Alias _
"SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HOOKPROC, _
ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Integer) As Integer
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Integer, _
ByVal ncode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Public hnexthookproc As Integer
Public Const PM_KEY_SPACE = &H20
Public Enum HookType
WH_KEYBOARD = 2
End Enum
Public Sub UnHook() ‘解Hook
If hnexthookproc <> 0 Then
UnhookWindowsHookEx(hnexthookproc)
hnexthookproc = 0
End If
End Sub
Public Function SetHook() ‘设置Hook
If hnexthookproc <> 0 Then
Exit Function
End If
hnexthookproc = SetWindowsHookEx(HookType.WH_KEYBOARD, AddressOf MyKeyboardProc, 0, GetCurrentThreadId())
End Function
Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
MyKeyboardProc = 0
If nCode < 0 Then
MyKeyboardProc = CallNextHookEx(hnexthookproc, nCode, wParam, lParam)
Exit Function
End If
If wParam = PM_KEY_SPACE Then
MyKeyboardProc = 1
‘写入你自己的代码
frm1.textbox1.text=”HOOK成功!”
End If
End Function
Sub main()
Application.Run(frm1)
End Sub
End Module
#5
再顶一次,如果没人回答就结贴了
#6
调用哪个函数出的错
#7
PostMessage
#8
MARK
#9
你把发消息这段贴出来。
#10
to Knight94(愚翁)
我写得乱七八糟就不贴了,你看上面的例子就行了,调试时是对的.例子接收的是空格键的消息,现在的问题是,如果我用PostMessage要给该窗体发自定义消息时,该如何写?函数HOOKPro(nCode,wParam ,lParam)与PostMessage(hwnd,nMsg,wParam,lParam)中的参数不一致怎么办?
我写得乱七八糟就不贴了,你看上面的例子就行了,调试时是对的.例子接收的是空格键的消息,现在的问题是,如果我用PostMessage要给该窗体发自定义消息时,该如何写?函数HOOKPro(nCode,wParam ,lParam)与PostMessage(hwnd,nMsg,wParam,lParam)中的参数不一致怎么办?
#11
to 函数HOOKPro(nCode,wParam ,lParam)与PostMessage(hwnd,nMsg,wParam,lParam)中的参数不一致怎么办
PostMessage函数的参数,
第一个是窗体handle,
第二个是消息类型,
后两个可以沿用HookPro中的后两个。
PostMessage函数的参数,
第一个是窗体handle,
第二个是消息类型,
后两个可以沿用HookPro中的后两个。
#12
调用 PostMessage(frm1.Handle, MSG_YB_INTEMP, 1, 1)'自定义消息
出错:
"对 PInvoke 函数“..\Module1::PostMessage”的调用导致堆栈不对称。原因可能是托管的 Invoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。"
#13
mark
#14
结贴了