附加到Visual Studio包

时间:2015-08-04 15:58:59

标签: visual-studio visual-studio-debugging visual-studio-package

我试图编写一个将调试器附加到命名进程的Visual Studio包。

我在我的包中使用以下代码。

var info = new VsDebugTargetInfo
{
                dlo = DEBUG_LAUNCH_OPERATION.DLO_AlreadyRunning,
                bstrExe = strProcessName,
                bstrCurDir = "c:\\",
                bstrArg = "",
                bstrEnv = "",
                bstrOptions = null,
                bstrPortName = null,
                bstrMdmRegisteredName = null,
                bstrRemoteMachine = "",
                cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf<VsDebugTargetInfo>(),
                grfLaunch = (uint)(__VSDBGLAUNCHFLAGS.DBGLAUNCH_DetachOnStop| __VSDBGLAUNCHFLAGS.DBGLAUNCH_StopDebuggingOnEnd| __VSDBGLAUNCHFLAGS.DBGLAUNCH_WaitForAttachComplete),
                fSendStdoutToOutputWindow = 1,
                clsidCustom = VSConstants.CLSID_ComPlusOnlyDebugEngine
};
VsShellUtilities.LaunchDebugger(ServiceProvider, info);

然而,我得到以下,无益,错误:

Exception : Unable to attach. Operation not supported. Unknown error: 0x80070057.

代码显然正在做某事,因为如果进程尚未启动,我会收到此错误

Exception : Unable to attach. Process 'xxxxxxxx' is not running on 'xxxxxxxx'.

该过程是一个受管理的.net 4进程,我可以通过VS UI附加到它。

对于上下文我试图替换我在VS 2010中使用的简单宏来执行相同的工作,但显然无法在较新版本的Visual Studio中运行。

1 个答案:

答案 0 :(得分:1)

我发现了一段完全不同的代码,受到https://github.com/whut/AttachTo的启发,可以更好地实现相同的结果

foreach (Process process in (DTE)GetService(typeof(DTE)).Debugger.LocalProcesses)
    if (process.Name.EndsWith(strProcessName,StringComparison.InvariantCultureIgnoreCase))
        process.Attach();

我不得不用&#39;结束&#39;因为进程名称包含正在运行的exe的完整路径。