none
Attach process in code?

    Question

  • Hey so I was asked to make a plugin, that has a toggleable button, which when  pressed, will simulate clicking Tools/Attach to Process/Selecting the "w3wp" process.

    It has to select only the w3wp process that's username is contains "Administrator".

    I have figured out how to get a list of Processes but I can't for the life of me figure out how to attach the process.

    Would love any help with this please and thank you.

    Here is the code and methods I have so far!

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
    	handled = false;
    	if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
    	{
    		if(commandName == "AutomaticProcessAttacher.Connect.AttachProcess")
    		{
                    //Need to get the array of proccesses that are called w3wp so we can use that to get only the 				                Administrator version of the process
    
                        System.Diagnostics.Process[] processArray = System.Diagnostics.Process.GetProcessesByName("w3wp");
                        int processToAttach = ReturnProcessToAttach(processArray);
    
                        System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(processToAttach);
                        System.Diagnostics.Debugger.Launch();
    
    		    handled = true;
    		    return;
    		}
    	}
    }
    
    private int ReturnProcessToAttach(System.Diagnostics.Process[] processArray)
    {
           	int processToAttach = 0;
    
            for (int i = 0; i < processArray.Length; i++)
            {
                    string p = GetProcessOwner(processArray[i].Id);
                    if (p.Contains("Administrator"))
                    {
                        processToAttach = processArray[i].Id;
                        break;
                    }
            }
    
            return processToAttach;
    }
    
    public string GetProcessOwner(int processId)
    {
            string query = "Select * From Win32_Process Where ProcessID = " + processId;
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            ManagementObjectCollection processList = searcher.Get();
    
            foreach (ManagementObject obj in processList)
            {
                string[] argList = new string[] { string.Empty, string.Empty };
                int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
                if (returnVal == 0)
                {
                    // return DOMAIN\user 
                    return argList[1] + "\\" + argList[0];
                }
            }
    
            return "NO OWNER";
    }

    • Moved by CoolDadTxMVP Tuesday, July 16, 2013 4:16 PM Extensibility related
    Tuesday, July 16, 2013 2:37 PM

Answers

All replies

  • Attaching to a process is a debugger feature.  As such your application would need to be a debugger and implement the appropriate debugging API.  Ultimately attaching to a process is handled by calling DebugActiveProcess but there are lots of steps to get this to work and will require advanced knowledge of Windows, memory management and how processes work if you intend to do anything beyond simply attaching.  Refer to this starter article on how to write a debugger.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, July 16, 2013 3:41 PM
  • after reading this article, you may try this approach.

    http://www.codeproject.com/Articles/39063/Quick-Attach-to-Process-by-Macro

    Tuesday, July 16, 2013 3:44 PM
  • Oh really? So I can't just attach the w3wp process using the DTE.Attach() method?
    Tuesday, July 16, 2013 4:06 PM
  • Is it sort of cheating by using a macro?

    Tuesday, July 16, 2013 4:06 PM
  • Sorry, you're referring to an extension in VS.  In that case then yes you can use the Process.Attach method as documented here.  The debugger is already implemented.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Tuesday, July 16, 2013 4:16 PM
  • From looking at my code could you give me an example of how I use the Process.Attach method?
    I have no idea how to do it.

    I have the process I want to attach I just don't know how to

    Tuesday, July 16, 2013 4:23 PM
  • Currently you're using the standard .NET Process object.  You'll want to switch to the version that the DTE uses via DTE.Debugger.LocalProcesses.  Once you do that you'll have the process ID which you can use to get the owner as you're doing now.  Once you've found the correct process (which will be the DTE version of Process) then you can simply call Attach.

    Summary:

    Modify ReturnProccessToAttach to accept EnvDTE.Process types
    Pass ProcessID to your GetProcessOwner method rather than ID
    If a match is found then return the EnvDTE.Process instance rather than just the ID
    In Exec if you get a process instance back then call Attach() on it - note this attaches the current VS instance to the process rather than starting a new instance

    Tuesday, July 16, 2013 4:36 PM
  • Thanks for that.

    My code is compiling now, but I'm not sure if the process is definitely attaching.

    Usually when you go to Tools/Attach to process/w3wp process, it's grayed out when it's attached.

    However, this isn't. Both of the w3wp processes are still there.

    Is this just a UI glitch or something? How can I check if the process is attached

    Tuesday, July 16, 2013 4:50 PM
  • Look for the process under DTE.Debugger.DebuggedProcesses (http://msdn.microsoft.com/en-us/library/envdte.debugger.debuggedprocesses.aspx), if it is there you are debugging it, if not you aren't.
    Tuesday, July 16, 2013 5:17 PM
    Moderator
  • Thank you!

    I'll have a look!

    Tuesday, July 16, 2013 6:45 PM