Hier ist ein kleines Beispiel vielleicht hilft es dir etwas weiter.
Imports System.Runtime.InteropServices
Public Class CHook
Public Event OnCallBack(ByVal x As Integer, ByVal y As Integer)
Private Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As _
IntPtr, ByVal lParam As IntPtr) As Integer
Private WH_MOUSE As Integer = 7
Private Shared hHook As Integer = 0
Private hookID As CallBack
<DllImport("User32.dll", CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Private Overloads Shared Function SetWindowsHookEx _
(ByVal idHook As Integer, ByVal HookProc As CallBack, _
ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Private Overloads Shared Function CallNextHookEx _
(ByVal idHook As Integer, ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Private Overloads Shared Function UnhookWindowsHookEx _
(ByVal idHook As Integer) As Boolean
End Function
<DllImport("User32.dll", CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Private Overloads Shared Function GetWindowThreadProcessId(ByVal hWnd As _
IntPtr, ByVal lpdwProcessId As Integer) As Integer
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, _
CallingConvention:=CallingConvention.StdCall)> _
Private Overloads Shared Function GetModuleHandle(ByVal lpModuleName As _
String) As Integer
End Function
<StructLayout(LayoutKind.Sequential)> Private Structure Point
Public x As Integer
Public y As Integer
End Structure
Private Structure MouseHook
Public ptPos As Point
Public handle As Integer
Public Code As Integer
Public eInfo As Integer
End Structure
Public Sub SetHook(ByVal ProcessID As Integer)
Dim ThreadID As Integer
Dim None As Integer
Dim MHandle As Integer = Process.GetProcessById( _
ProcessID).MainWindowHandle()
Dim DLLHandle As IntPtr = GetModuleHandle("Hook.dll")
ThreadID = GetWindowThreadProcessId(MHandle, None)
If hHook.Equals(0) Then
hookID = AddressOf MouseHookProc
hHook = SetWindowsHookEx(WH_MOUSE, hookID, DLLHandle, ThreadID)
If hHook.Equals(0) Then
MsgBox("FEHLER !!", MsgBoxStyle.Information)
Return
End If
Else
Dim ret As Boolean = UnhookWindowsHookEx(hHook)
If ret.Equals(False) Then
MsgBox("Hook konnte nicht Entfernt werden !!", _
MsgBoxStyle.Information)
Return
Else
hHook = 0
End If
End If
End Sub
Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As _
IntPtr, ByVal lParam As IntPtr) As Integer
Dim MyHook As New MouseHook()
If (nCode < 0) Then
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End If
MyHook = CType(Marshal.PtrToStructure(lParam, MyHook.GetType()), _
MouseHook)
Run(MyHook.ptPos.x, MyHook.ptPos.y)
Return CallNextHookEx(hHook, nCode, wParam, lParam)
End Function
Private Sub Run(ByVal x As Integer, ByVal y As Integer)
RaiseEvent OnCallBack(x, y)
End Sub Das ist die DLL die ich mir gebastelt habe. Nur wie ich schon sagte, stürzen andere Anwendungen ab, wenn ich den Hook setze. |