Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8329917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:57:10+00:00 2026-06-09T01:57:10+00:00

I’d like to be able to automatically attach a debugger, something like: System.Diagnostics.Debugger.Launch() ,

  • 0

I’d like to be able to automatically attach a debugger, something like: System.Diagnostics.Debugger.Launch(), except rather than the current process to another named process. I’ve got a process name and PID to identify the other process.

Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:57:13+00:00Added an answer on June 9, 2026 at 1:57 am

    Edit:
    GSerjo offered the correct solution. I’d like to share a few thoughts on how to improve it (and an explanation). I hope my improved answer will be useful to to others who experience the same problem.


    Attaching the VS Debugger to a Process

    Manually

    1. Open the Windows Task Manager (Ctrl + Shift + Esc).
    2. Go to the Tab Processes.
    3. Right click the process.
    4. Select Debug.

    Or, within Visual Studio, select Debug > Attach to Process....

    Results will vary depending on whether you have access to the source code.

    Automatically with C#

    A note of caution: The following code is brittle in the sense that certain values,
    such as the Visual Studio Version number, are hard-coded. Keep this in mind going forward
    if you are planning to distribute your program.

    First of all, add a reference to EnvDTE to your project (right click on the references folder in the solution explorer, add reference). In the following code, I’ll only show the unusual using directives; the normal ones such as using System are omitted.

    Because you are interacting with COM you need to make sure to decorate your Main method (the entry point of your application) with the STAThreadAttribute.

    Then, you need to define the IOleMessageFilter Interface that will allow you to interact with the defined COM methods (note the ComImportAttribute). We need to access the message filter so we can retry if the Visual Studio COM component blocks one of our calls.

    using System.Runtime.InteropServices;
    
    [ComImport, Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleMessageFilter
    {
        [PreserveSig]
        int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
    
        [PreserveSig]
        int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
    
        [PreserveSig]
        int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
    }
    

    Now, we need to implement this interface in order to handle incoming messages:

    public class MessageFilter : IOleMessageFilter
    {
        private const int Handled = 0, RetryAllowed = 2, Retry = 99, Cancel = -1, WaitAndDispatch = 2;
    
        int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
        {
            return Handled;
        }
    
        int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
        {
            return dwRejectType == RetryAllowed ? Retry : Cancel;
        }
    
        int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
        {
            return WaitAndDispatch;
        }
    
        public static void Register()
        {
            CoRegisterMessageFilter(new MessageFilter());
        }
    
        public static void Revoke()
        {
            CoRegisterMessageFilter(null);
        }
    
        private static void CoRegisterMessageFilter(IOleMessageFilter newFilter)
        {
            IOleMessageFilter oldFilter;
            CoRegisterMessageFilter(newFilter, out oldFilter);
        }
    
        [DllImport("Ole32.dll")]
        private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
    }
    

    I defined the return values as constants for better readability and refactored the whole thing a bit to get rid of some of the duplication from the MSDN example, so I hope you’ll find it self-explanatory. extern int CoRegisterMessageFilter is our connection to the unmanaged message filter code – you can read up on the extern keyword at MSDN.

    Now all that’s left is some code illustrating the usage:

    using System.Runtime.InteropServices;
    using EnvDTE;
    
    [STAThread]
    public static void Main()
    {
        MessageFilter.Register();
        var process = GetProcess(7532);
        if (process != null)
        {
            process.Attach();
            Console.WriteLine("Attached to {0}", process.Name);
        }
        MessageFilter.Revoke();
        Console.ReadLine();
    }
    
    private static Process GetProcess(int processID)
    {
        var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
        var processes = dte.Debugger.LocalProcesses.OfType<Process>();
        return processes.SingleOrDefault(x => x.ProcessID == processID);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have two tables with like below codes: Table: Accounts id | username |
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.