Click here to Skip to main content
15,886,362 members
Articles / General Programming / Debugging
Alternative
Tip/Trick

Self-debugger attach to process

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
30 Sep 2011CPOL 13.9K   2   3
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. :)


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


C#
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)


Written By
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#, always trying to grasp any available language tool (the last being Generics, Linq, IL, Expression trees).

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'm not worry to step into).

Comments and Discussions

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

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

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