How do I get F12 to enter debugger on Vista x64 with VS 2008?

F12 is a miracle for tracking UI blocking operations, but I cannot figure out how to make it work with VS 2008 and managed code.

Help! Or not...

Edit: it turns out that it does not work in VS 2005 or on Vista x64, so I assume that it either expands or narrows it, depending on your perspective :(

Msn

+5
source share
3 answers

, F12 , "", Visual Studio " " .

, , F12. , , DEBUG, F12 .

, ; . "". , 10 (Thread.Sleep), "biting".

F12, "", F12 - , . (Vista x64) F12:

F12 http://img242.imageshack.us/img242/5189/f12threadsre9.png

, - "GoToSleep", .

.

:

using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace KeyboardHook
{

    public sealed class SimpleKeyboardHook : IDisposable
    {
        public SimpleKeyboardHook(Action<Keys> handler)
        {
            if (null == handler) { throw new ArgumentNullException("handler"); }
            this._handler = handler;
            var t = new Thread(this.ListenerThread) { IsBackground = true, Name = "KeyboardListener" };
            t.Start();
        }

        public void Dispose()
        {
            if (!this._disposed)
            {
                UnhookWindowsHookEx(this._id);
                this._disposed = true;
                GC.SuppressFinalize(this);
            }
        }

        public static void BreakOnF12(Keys keys)
        {
            if (keys == Keys.F12)
            {
                Debugger.Break();
            }
        }

        private void ListenerThread()
        {
            using (var currentProcess = Process.GetCurrentProcess())
            using (var mainModule = currentProcess.MainModule)
            {
                if (null == mainModule) { throw new InvalidOperationException("Unable to determine main module for the current process"); }

                this._id = SetWindowsHookEx(
                    WH_KEYBOARD_LL,
                    this.HookCallback,
                    GetModuleHandle(mainModule.ModuleName), 0);
            }

            Application.Run();
        }

        private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                var vkCode = Marshal.ReadInt32(lParam);
                this._handler((Keys)vkCode);
            }
            return CallNextHookEx(this._id, nCode, wParam, lParam);
        }

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private IntPtr _id;
        private readonly Action<Keys> _handler;
        private volatile bool _disposed;
    }


    static class Program
    {
        private static void GoToSleep(object sender, EventArgs args)
        {
            Thread.Sleep(10000);
        }

        [STAThread]
        static void Main()
        {
            using (new SimpleKeyboardHook(SimpleKeyboardHook.BreakOnF12))
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                var form = new Form { Text = "Sleepy form", Size = new Size(160,80), Padding = new Padding(6) };
                var btn = new Button { Dock = DockStyle.Fill, Text = "Sleep", Location = new Point(10, 10) };
                btn.Click += GoToSleep;
                form.Controls.Add(btn);
                Application.Run(form);
            }
        }
    }
}
+1

F12 x86 Windows XP, , ( Windows x86, x64).

-, Visual Studio (2008, 2005, 2003... , , , WinDbg, Windows)

"" " ...":

http://img297.imageshack.us/img297/2369/attachmenuhe7.png

. Visual Studio:

http://img440.imageshack.us/img440/1232/vstoolbarmainaw4.png

" ". ( , " " " " ). , , "":

http://img231.imageshack.us/img231/8415/vsdialogattachoq5.png

. , , , ( Internet Explorer) T-SQL WF. " " , .

, Visual Studio , , Debug → Break All, :

http://img297.imageshack.us/img297/8579/vsmenubreakallrb1.png

:

http://img440.imageshack.us/img440/248/vstoolbardebugwt4.png

, , , .. x64- , F12, .

0

, . Visual Studio , F12. , - VS, , VS, .

" Windows", Software Development Kit (SDK). (, , Windows (WDK), .) , " ". , , ; .

, (KD) NTKD WinDbg. x86 x64.

: Windows.

0
source

All Articles