Click here to Skip to main content
13,255,769 members (66,941 online)
Click here to Skip to main content
Add your own
alternative version

Stats

8.6K views
2 bookmarked
Posted 30 Sep 2011

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. :)


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#, 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).

You may also be interested in...

Pro

Comments and Discussions

 
GeneralI read an article recently, questioning a Silcon valley CEO ... Pin
Ron Richins3-Oct-11 16:09
memberRon Richins3-Oct-11 16:09 
GeneralRe: What are you on? Pin
Dave Cross4-Oct-11 3:05
memberDave Cross4-Oct-11 3:05 
GeneralNice work, Daniele. Pin
Mauro Leggieri30-Sep-11 7:57
memberMauro Leggieri30-Sep-11 7: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 30 Sep 2011
Article Copyright 2011 by Daniele Rota Nodari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid