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";
}