2010-10-15 10:10:44 +03:00
|
|
|
/* SOURCE: http://lazy.codeplex.com/
|
|
|
|
* File: http://lazy.codeplex.com/SourceControl/changeset/view/55373#307770
|
|
|
|
* Author: pablito900
|
|
|
|
* Licence: GNU General Public License version 2 (GPLv2)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2011-04-10 05:44:01 +03:00
|
|
|
using System.Runtime.InteropServices;
|
2012-03-04 07:39:36 +03:00
|
|
|
using EnvDTE;
|
2010-10-15 10:10:44 +03:00
|
|
|
using EnvDTE80;
|
2011-04-10 05:44:01 +03:00
|
|
|
using Thread = System.Threading.Thread;
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
{
|
2012-03-04 07:39:36 +03:00
|
|
|
//[DebuggerStepThrough]
|
2010-10-15 10:10:44 +03:00
|
|
|
public class ProcessAttacher
|
|
|
|
{
|
|
|
|
public static void Attach()
|
|
|
|
{
|
2011-04-22 20:09:06 +03:00
|
|
|
DTE2 dte2;
|
2012-03-04 07:39:36 +03:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.11.0");
|
|
|
|
}
|
|
|
|
|
2011-04-22 20:09:06 +03:00
|
|
|
|
|
|
|
var pa = new ProcessAttacher(dte2, "iisexpress", 10);
|
|
|
|
pa.PessimisticAttachManaged();
|
2011-04-22 07:03:59 +03:00
|
|
|
|
2010-10-15 10:10:44 +03:00
|
|
|
// Get an instance of the currently running Visual Studio IDE.
|
|
|
|
}
|
|
|
|
|
2011-03-29 00:57:06 +03:00
|
|
|
#region private
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
private readonly Dictionary<AttachType, string> _attachTypesMap;
|
|
|
|
private readonly DTE2 _dte;
|
|
|
|
private readonly string _processName;
|
|
|
|
private readonly int _waitTimeout;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2011-03-29 00:57:06 +03:00
|
|
|
#region ctor
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
private ProcessAttacher(DTE2 dte, string processName, int waitTimeout)
|
|
|
|
{
|
|
|
|
_processName = processName;
|
|
|
|
_waitTimeout = waitTimeout;
|
|
|
|
_dte = dte;
|
2011-04-10 05:44:01 +03:00
|
|
|
_attachTypesMap = new Dictionary<AttachType, string>
|
|
|
|
{
|
|
|
|
{AttachType.Managed, "Managed"}
|
|
|
|
};
|
2010-10-15 10:10:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2011-03-29 00:57:06 +03:00
|
|
|
#region private methods
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
private AttachResult Attach(AttachType attachType)
|
|
|
|
{
|
|
|
|
string engine = _attachTypesMap[attachType];
|
|
|
|
|
|
|
|
if (IsBeingDebugged())
|
|
|
|
{
|
|
|
|
return AttachResult.BeingDebugged;
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbg = _dte.Debugger as Debugger2;
|
|
|
|
var trans = dbg.Transports.Item("Default");
|
|
|
|
|
|
|
|
var eng = trans.Engines.Item(engine);
|
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
Process2 proc = null;
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
proc = dbg.GetProcesses(trans, "").Item(_processName) as Process2;
|
2010-10-15 10:10:44 +03:00
|
|
|
}
|
2011-03-30 09:18:35 +03:00
|
|
|
catch (Exception)
|
2010-10-15 10:10:44 +03:00
|
|
|
{
|
2010-10-24 10:46:58 +03:00
|
|
|
return AttachResult.NotRunning;
|
2010-10-15 10:10:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
proc.Attach2(eng);
|
|
|
|
|
|
|
|
return AttachResult.Attached;
|
|
|
|
}
|
|
|
|
|
|
|
|
private AttachResult PessimisticAttach(AttachType attachType)
|
|
|
|
{
|
2011-10-07 09:57:43 +03:00
|
|
|
var res = Attach(attachType);
|
2010-10-15 10:10:44 +03:00
|
|
|
|
2011-10-07 09:57:43 +03:00
|
|
|
var timeout = DateTime.Now.AddSeconds(_waitTimeout);
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
while (res == AttachResult.NotRunning && timeout > DateTime.Now)
|
|
|
|
{
|
|
|
|
res = Attach(attachType);
|
2011-04-10 05:44:01 +03:00
|
|
|
Thread.Sleep(100);
|
2010-10-15 10:10:44 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsBeingDebugged()
|
|
|
|
{
|
|
|
|
if (_dte.Debugger.DebuggedProcesses != null)
|
|
|
|
{
|
2011-04-10 05:44:01 +03:00
|
|
|
foreach (Process process in _dte.Debugger.DebuggedProcesses)
|
2010-10-15 10:10:44 +03:00
|
|
|
{
|
|
|
|
if (process.Name.IndexOf(_processName) != -1)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2011-03-29 00:57:06 +03:00
|
|
|
#region public methods
|
2010-10-15 10:10:44 +03:00
|
|
|
|
|
|
|
public void OptimisticAttachManaged()
|
|
|
|
{
|
|
|
|
Attach(AttachType.Managed);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PessimisticAttachManaged()
|
|
|
|
{
|
|
|
|
PessimisticAttach(AttachType.Managed);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
#region Nested type: AttachResult
|
|
|
|
|
|
|
|
private enum AttachResult
|
|
|
|
{
|
|
|
|
Attached,
|
|
|
|
NotRunning,
|
|
|
|
BeingDebugged
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2010-10-30 05:46:32 +03:00
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
#region Nested type: AttachType
|
|
|
|
|
|
|
|
private enum AttachType
|
|
|
|
{
|
|
|
|
Managed,
|
|
|
|
Native,
|
|
|
|
ManagedAndNative
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
2010-10-15 10:10:44 +03:00
|
|
|
|
2011-04-10 05:44:01 +03:00
|
|
|
#endif
|