简体   繁体   中英

Attaching to process in Visual Studio Package

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM