code_junkie

How do I get F12 to break into the debugger on Vista x64 with VS 2008?

Nov 13th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7.  
  8. namespace KeyboardHook
  9. {
  10.  
  11. public sealed class SimpleKeyboardHook : IDisposable
  12. {
  13. public SimpleKeyboardHook(Action<Keys> handler)
  14. {
  15. if (null == handler) { throw new ArgumentNullException("handler"); }
  16. this._handler = handler;
  17. var t = new Thread(this.ListenerThread) { IsBackground = true, Name = "KeyboardListener" };
  18. t.Start();
  19. }
  20.  
  21. public void Dispose()
  22. {
  23. if (!this._disposed)
  24. {
  25. UnhookWindowsHookEx(this._id);
  26. this._disposed = true;
  27. GC.SuppressFinalize(this);
  28. }
  29. }
  30.  
  31. public static void BreakOnF12(Keys keys)
  32. {
  33. if (keys == Keys.F12)
  34. {
  35. Debugger.Break();
  36. }
  37. }
  38.  
  39. private void ListenerThread()
  40. {
  41. using (var currentProcess = Process.GetCurrentProcess())
  42. using (var mainModule = currentProcess.MainModule)
  43. {
  44. if (null == mainModule) { throw new InvalidOperationException("Unable to determine main module for the current process"); }
  45.  
  46. this._id = SetWindowsHookEx(
  47. WH_KEYBOARD_LL,
  48. this.HookCallback,
  49. GetModuleHandle(mainModule.ModuleName), 0);
  50. }
  51.  
  52. Application.Run();
  53. }
  54.  
  55. private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  56. {
  57. if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
  58. {
  59. var vkCode = Marshal.ReadInt32(lParam);
  60. this._handler((Keys)vkCode);
  61. }
  62. return CallNextHookEx(this._id, nCode, wParam, lParam);
  63. }
  64.  
  65. private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
  66.  
  67. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  68. private static extern IntPtr SetWindowsHookEx(int idHook,
  69. LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
  70.  
  71. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  72. [return: MarshalAs(UnmanagedType.Bool)]
  73. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  74.  
  75. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  76. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
  77. IntPtr wParam, IntPtr lParam);
  78.  
  79. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  80. private static extern IntPtr GetModuleHandle(string lpModuleName);
  81.  
  82. private const int WH_KEYBOARD_LL = 13;
  83. private const int WM_KEYDOWN = 0x0100;
  84. private IntPtr _id;
  85. private readonly Action<Keys> _handler;
  86. private volatile bool _disposed;
  87. }
  88.  
  89.  
  90. static class Program
  91. {
  92. private static void GoToSleep(object sender, EventArgs args)
  93. {
  94. Thread.Sleep(10000);
  95. }
  96.  
  97. [STAThread]
  98. static void Main()
  99. {
  100. using (new SimpleKeyboardHook(SimpleKeyboardHook.BreakOnF12))
  101. {
  102. Application.EnableVisualStyles();
  103. Application.SetCompatibleTextRenderingDefault(false);
  104.  
  105. var form = new Form { Text = "Sleepy form", Size = new Size(160,80), Padding = new Padding(6) };
  106. var btn = new Button { Dock = DockStyle.Fill, Text = "Sleep", Location = new Point(10, 10) };
  107. btn.Click += GoToSleep;
  108. form.Controls.Add(btn);
  109. Application.Run(form);
  110. }
  111. }
  112. }
  113. }
Add Comment
Please, Sign In to add comment