1

I'm trying to write a Visual Studio Package that will attach the debugger to a named process.

I am using the following code in my package.

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);

However I get the following, unhelpful, error:

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

The code is obviously doing something because if the process has not started I get this error

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

The process is a managed .net 4 process and I am able to attach to it through the VS UI.

For context I am trying to replace a simple Macro I was using in VS 2010 to do the same job but that obviously can't be run in newer versions of Visual Studio.

Alan Hinton
  • 489
  • 4
  • 6
  • To continue use your Macro in a new VS you can try Visual Commander https://visualstudiogallery.msdn.microsoft.com/deda8ac1-75e6-4068-89ab-b607cee38f2d – Sergey Vlasov Aug 04 '15 at 16:38
  • Thanks, after a bit of tweaking Visual Commander was able to run my original macro, however I'm still interested to know how to fix the original problem as I would like to build my own extension. – Alan Hinton Aug 06 '15 at 13:31

1 Answers1

1

I found a totally different piece of code, inspited by https://github.com/whut/AttachTo, worked much better to achieve the same result

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

I had to use 'ends with' because the process names include the full path to the running exe.

Alan Hinton
  • 489
  • 4
  • 6