6

I know I can find if a process is being debugged via a call to Debugger.IsAttached in .NET, but I'd like to be able to get the PID of the Visual Studio that is the debugging the processes. Is this possible?

Robert
  • 6,407
  • 2
  • 34
  • 41
  • 3
    If the process is started from the debugger, the debugger pid will be the parent pid of debugged process and this can be determined. Are you more interested in the scenario where the debugger is remotely attached to an active process. – panickal Jun 22 '12 at 15:49
  • 1
    There are several cases where VS is *not* the debugger process. Like remote debugging or debugging 64-bit code or various hosting scenarios. Pretty important that you don't need info. This is an XY question, can't see the X. – Hans Passant Jun 23 '12 at 11:05

2 Answers2

2

You could use the TryGetVSInstance method outlined in this answer to get a hold of each instance of Visual Studio's EnvDTE COM Automation object. Once you have that, just iterate the DTE.Debugger.DebuggedProcesses collection and check if any of them are pointing to the same processID as the process you're interested in.

Community
  • 1
  • 1
Omer Raviv
  • 11,409
  • 5
  • 43
  • 82
  • I was hoping they'd be a win32 api you could call, but I think you're suggestion is probably the cleanest way. Thanks! – Robert Jun 23 '12 at 14:53
0

This worked for me.

public static Process GetParent(Process process)
{
    var processName = process.ProcessName;
    var nbrOfProcessWithThisName = Process.GetProcessesByName(processName).Length;
    for (var index = 0; index < nbrOfProcessWithThisName; index++)
    {
        var processIndexdName = index == 0 ? processName : processName + "#" + index;
        var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
        if ((int)processId.NextValue() == process.Id)
        {
            var parentId = new PerformanceCounter("Process", "Creating Process ID", processIndexdName);
            return Process.GetProcessById((int)parentId.NextValue());
        }
    }
    return null;
}
Community
  • 1
  • 1
Gabe Halsmer
  • 808
  • 1
  • 9
  • 18