Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

Self-debugger attach to process

, 30 Sep 2011
Rate this:
Please Sign up or sign in to vote.
I was glad to integrate your code into my C# application.Actually, within the debugger windows, I can't recognize any human-readable reference to the calling process, but the function is working. :)So, for whoever may be interested, the following code is my C# 4 equivalent:using...

I was glad to integrate your code into my C# application.

Actually, within the debugger windows, I can't recognize any human-readable reference to the calling process, but the function is working. Smile | :)

So, for whoever may be interested, the following code is my C# 4 equivalent:

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
 
namespace MyApp.Diagnostics
{
    public static class DebugHelper
    {
        [DllImport("kernel32.dll")]
        static extern bool IsDebuggerPresent();
 
        [DllImport("kernel32.dll")]
        static extern void DebugBreak();
 
        public static bool AttachCurrentProcessToDebugger()
        {
            try
            {
                if (!IsDebuggerPresent())
                {
                    var sExePath = Path.Combine(Environment.SystemDirectory, 
                                   "VSJitDebugger.exe");
 
                    var process = Process.Start(sExePath, " -p " + 
                                  Process.GetCurrentProcess().Id);
                    process.WaitForExit();
                    if (process.ExitCode != 0)
                        return false;
 
                    for (int i = 0; i < 5 * 60; i++)
                    {
                        if (IsDebuggerPresent())
                            break;
 
                        Thread.Sleep(200);
                    }
                }
 
                DebugBreak();
 
                return true;
            }
            catch (Exception ex)
            {
                var sExMessage = (ex.InnerException != null ? 
                    ex.InnerException.Message + "; " : string.Empty)
                    + ex.Message
                    + ((ex.TargetSite != null) ? ex.TargetSite.ToString() : string.Empty);
                Debug.Print(ex.GetType().Name + ": " + sExMessage + ex.StackTrace);
                return false;
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniele Rota Nodari
Software Developer (Senior) FK Group srl
Italy Italy
I am currently busy in the apparel industry developing software for cutting machines.
 
Over the past 15 or more years I did work in this field, initially as a technician.
 
After a ZX Spectrum lit my path in the 80s, I played with many technologies, ranging from DOS to Windows, evolving my interests from a school matter into an hobby, then a passion and finally into a full-time job.
 
Since the 90s I engaged with Basic, Pascal, Assembly, Batch files, C, C++, Visual-Basic, Java(script), VBScript, T-SQL, HTML, ASP, FoxPro, C#.
 
I can claim experience for many kind of contexts, mainly DB, PM and CAD/CAM.
 
I'm information hungry and always try to improve my skills.
Apart from High school graduation, I have no specific certificate or title, but I can count on an enough sharp know-how (at least within the fields I don't worry to step into).
Follow on   Google+

Comments and Discussions

 
GeneralI read an article recently, questioning a Silcon valley CEO ... PinmemberRon Richins3-Oct-11 15:09 
GeneralRe: What are you on? PinmemberDave Cross4-Oct-11 2:05 
GeneralNice work, Daniele. PinmemberMauro Leggieri30-Sep-11 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Sep 2011
Article Copyright 2011 by Daniele Rota Nodari
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid