Skip to content

Commit c99f1f2

Browse files
committed
updated EventHook, no longer crashes outside debugger, but not very stable either.
1 parent 453f940 commit c99f1f2

File tree

5 files changed

+99
-39
lines changed

5 files changed

+99
-39
lines changed

RetailCoder.VBE/App.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ public void Startup()
119119
_appMenus.Initialize();
120120
_appMenus.Localize();
121121

122-
Task.Delay(1000).ContinueWith(t => _parser.State.OnParseRequested(this));
123-
124-
_hooks.HookHotkeys();
125-
_hooks.Attach();
122+
Task.Delay(1000).ContinueWith(t =>
123+
{
124+
_parser.State.OnParseRequested(this);
125+
_hooks.HookHotkeys();
126+
_hooks.Attach();
127+
});
126128
}
127129

128130
#region sink handlers. todo: move to another class
Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,104 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Diagnostics;
34
using EventHook;
45
using EventHook.Hooks;
6+
using Rubberduck.Common.WinAPI;
57

68
namespace Rubberduck.Common
79
{
810
public class MouseHookWrapper : IAttachable
911
{
10-
private void MouseWatcher_OnMouseInput(object sender, MouseEventArgs e)
11-
{
12-
// only handle right-clicks
13-
if (e.Message != MouseMessages.WM_RBUTTONDOWN)
14-
{
15-
return;
16-
}
12+
//private IntPtr _hookId;
1713

18-
var handler = MessageReceived;
19-
if (handler != null)
20-
{
21-
handler.Invoke(this, HookEventArgs.Empty);
22-
}
23-
}
14+
//private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
15+
//{
16+
// if (nCode >= 0 && ((WM)wParam) == WM.RBUTTONDOWN)
17+
// {
18+
// OnMessageReceived();
19+
// }
20+
21+
// return User32.CallNextHookEx(_hookId, nCode, wParam, lParam);
22+
//}
2423

24+
//public bool IsAttached { get; private set; }
25+
//public event EventHandler<HookEventArgs> MessageReceived;
26+
27+
//private static IntPtr SetHook(User32.HookProc callback)
28+
//{
29+
// var hook = User32.SetWindowsHookEx(WindowsHook.MOUSE_LL, callback, Kernel32.GetModuleHandle("user32"), 0);
30+
// if (hook == IntPtr.Zero)
31+
// {
32+
// throw new Win32Exception();
33+
// }
34+
// return hook;
35+
//}
36+
37+
//public void Attach()
38+
//{
39+
// if (IsAttached)
40+
// {
41+
// return;
42+
// }
43+
44+
// _hookId = SetHook(HookCallback);
45+
46+
// IsAttached = true;
47+
// Debug.WriteLine("{0}: {1}", GetType().Name, IsAttached ? "Attached" : "Detached");
48+
//}
49+
50+
//public void Detach()
51+
//{
52+
// if (!IsAttached)
53+
// {
54+
// return;
55+
// }
56+
57+
// User32.UnhookWindowsHookEx(_hookId);
58+
59+
// IsAttached = false;
60+
// Debug.WriteLine("{0}: {1}", GetType().Name, IsAttached ? "Attached" : "Detached");
61+
//}
2562
public bool IsAttached { get; private set; }
2663
public event EventHandler<HookEventArgs> MessageReceived;
27-
2864
public void Attach()
2965
{
30-
MouseWatcher.Start();
66+
if (IsAttached)
67+
{
68+
return;
69+
}
70+
3171
MouseWatcher.OnMouseInput += MouseWatcher_OnMouseInput;
72+
MouseWatcher.Start();
3273
IsAttached = true;
33-
Debug.WriteLine("{0}: {1}", GetType().Name, IsAttached ? "Attached" : "Detached");
3474
}
3575

3676
public void Detach()
3777
{
38-
MouseWatcher.Stop();
78+
if (!IsAttached)
79+
{
80+
return;
81+
}
82+
3983
MouseWatcher.OnMouseInput -= MouseWatcher_OnMouseInput;
40-
IsAttached = false;
41-
Debug.WriteLine("{0}: {1}", GetType().Name, IsAttached ? "Attached" : "Detached");
84+
MouseWatcher.Stop();
85+
}
86+
87+
void MouseWatcher_OnMouseInput(object sender, MouseEventArgs e)
88+
{
89+
if (e.Message == MouseMessages.WM_RBUTTONDOWN)
90+
{
91+
OnMessageReceived();
92+
}
93+
}
94+
95+
private void OnMessageReceived()
96+
{
97+
var handler = MessageReceived;
98+
if (handler != null)
99+
{
100+
handler.Invoke(this, HookEventArgs.Empty);
101+
}
42102
}
43103
}
44104
}

RetailCoder.VBE/Common/RubberduckHooks.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class RubberduckHooks : IRubberduckHooks
1919
private readonly User32.WndProc _oldWndProc;
2020
private User32.WndProc _newWndProc;
2121

22-
private readonly IAttachable _timerHook;
22+
//private readonly IAttachable _timerHook;
2323
private readonly IGeneralConfigService _config;
2424
private readonly IList<IAttachable> _hooks = new List<IAttachable>();
2525

@@ -33,8 +33,8 @@ public RubberduckHooks(VBE vbe, IAttachable timerHook, IGeneralConfigService con
3333
_oldWndProc = (User32.WndProc)Marshal.GetDelegateForFunctionPointer(_oldWndPointer, typeof(User32.WndProc));
3434

3535
_config = config;
36-
_timerHook = timerHook;
37-
_timerHook.MessageReceived += timerHook_MessageReceived;
36+
//_timerHook = timerHook;
37+
//_timerHook.MessageReceived += timerHook_MessageReceived;
3838
}
3939

4040

@@ -141,8 +141,8 @@ private void timerHook_MessageReceived(object sender, EventArgs e)
141141

142142
public void Dispose()
143143
{
144-
_timerHook.MessageReceived -= timerHook_MessageReceived;
145-
_timerHook.Detach();
144+
//_timerHook.MessageReceived -= timerHook_MessageReceived;
145+
//_timerHook.Detach();
146146

147147
Detach();
148148
}
@@ -166,9 +166,9 @@ private IntPtr WindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
166166
}
167167
}
168168

169-
if (!processed)
169+
if (processed)
170170
{
171-
return User32.CallWindowProc(_oldWndProc, hWnd, uMsg, wParam, lParam);
171+
return IntPtr.Zero;
172172
}
173173
}
174174
catch (Exception exception)
@@ -182,11 +182,11 @@ private IntPtr WindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam)
182182
private bool HandleHotkeyMessage(IntPtr wParam)
183183
{
184184
var processed = false;
185-
if (GetWindowThread(User32.GetForegroundWindow()) == GetWindowThread(_mainWindowHandle))
186-
{
185+
if (GetWindowThread(User32.GetForegroundWindow()) == GetWindowThread(_mainWindowHandle))
186+
{
187187
var hook = Hooks.OfType<Hotkey>().SingleOrDefault(k => k.HotkeyInfo.HookId == wParam);
188-
if (hook != null)
189-
{
188+
if (hook != null)
189+
{
190190
hook.OnMessageReceived();
191191
processed = true;
192192
}

RetailCoder.VBE/Common/TimerHook.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ public class TimerHook : IAttachable, IDisposable
1212
private IntPtr _timerId;
1313
private bool _isAttached;
1414

15-
public event EventHandler Tick;
16-
1715
public TimerHook(IntPtr mainWindowHandle)
1816
{
1917
_mainWindowHandle = mainWindowHandle;
@@ -66,10 +64,10 @@ public void Detach()
6664

6765
private void OnTick()
6866
{
69-
var handler = Tick;
67+
var handler = MessageReceived;
7068
if (handler != null)
7169
{
72-
handler.Invoke(this, EventArgs.Empty);
70+
handler.Invoke(this, HookEventArgs.Empty);
7371
}
7472
}
7573

RetailCoder.VBE/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package id="Antlr4" version="4.3.0" targetFramework="net45" />
44
<package id="Antlr4.Runtime" version="4.3.0" targetFramework="net45" />
55
<package id="Castle.Core" version="3.2.0" targetFramework="net45" />
6-
<package id="EventHook" version="1.4.26" targetFramework="net45" />
6+
<package id="EventHook" version="1.4.29" targetFramework="net45" />
77
<package id="Ninject" version="3.2.2.0" targetFramework="net45" />
88
<package id="Ninject.Extensions.Conventions" version="3.2.0.0" targetFramework="net45" />
99
<package id="Ninject.Extensions.Factory" version="3.2.1.0" targetFramework="net45" />

0 commit comments

Comments
 (0)