Home > other >  Visual Studio Extension which starts a program and attaches the debugger: How to avoid stop because
Visual Studio Extension which starts a program and attaches the debugger: How to avoid stop because

Time:01-09

I am trying to create a Visual Studio Extension (VS 2022) which starts a program and attaches the debugger. At this point no solution/project is loaded.

It works. However, at startup a window appears that a breakpoint has been reached: enter image description here

Does anyone know a way to avoid this breakpoint? Or a hint to an other API? Thanks.

Initial setup:

  • create VSIX Project
  • add New Item: Command

The process is launched suspended because i want to add my own breakpoints (not in example but works).

~~

    static DTE dte;

    public static async Task InitializeAsync(AsyncPackage package)
    {
        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(package.DisposalToken);
        OleMenuCommandService commandService = await package.GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService;
        Instance = new Command1(package, commandService);

        dte = await package.GetServiceAsync(typeof(DTE)) as DTE;
    }

    public struct PROCESS_INFORMATION
    {
        public IntPtr hProcess;
        public IntPtr hThread;
        public uint dwProcessId;
        public uint dwThreadId;
    }

    public struct STARTUPINFO
    {
        public uint cb;
        public string lpReserved;
        public string lpDesktop;
        public string lpTitle;
        public uint dwX;
        public uint dwY;
        public uint dwXSize;
        public uint dwYSize;
        public uint dwXCountChars;
        public uint dwYCountChars;
        public uint dwFillAttribute;
        public uint dwFlags;
        public short wShowWindow;
        public short cbReserved2;
        public IntPtr lpReserved2;
        public IntPtr hStdInput;
        public IntPtr hStdOutput;
        public IntPtr hStdError;
    }

    [DllImport("kernel32.dll")]
    public static extern bool CreateProcess(string lpApplicationName, string lpCommandLine, IntPtr lpProcessAttributes, IntPtr lpThreadAttributes,
                             bool bInheritHandles, uint dwCreationFlags, IntPtr lpEnvironment,
                            string lpCurrentDirectory, ref STARTUPINFO lpStartupInfo, out PROCESS_INFORMATION lpProcessInformation);

    [DllImport("kernel32.dll")]
    public static extern uint ResumeThread(IntPtr hThread);

    private void Execute(object sender, EventArgs e)
    {
        STARTUPINFO si = new STARTUPINFO();
        PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
        uint CREATE_SUSPENDED = 0x00000004;
        bool success = CreateProcess(@"C:\Windows\System32\notepad.exe", null, IntPtr.Zero, IntPtr.Zero, false, CREATE_SUSPENDED, IntPtr.Zero, null, ref si, out pi);

        EnvDTE.Processes processes = dte.Debugger.LocalProcesses;

        foreach (EnvDTE.Process proc in processes)
        {
            if (proc.ProcessID == pi.dwProcessId)
            {
                proc.Attach();
                break;
            }
        }

        ResumeThread(pi.hThread);
    }

~~

CodePudding user response:

Found the solution:

public void LaunchDebugTarget(string filePath, string options, Guid engineId)
{
    IVsDebugger4 debugger = (IVsDebugger4)Package.GetGlobalService(typeof(IVsDebugger));
    VsDebugTargetInfo4[] debugTargets = new VsDebugTargetInfo4[1];
    debugTargets[0].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
    debugTargets[0].bstrExe = filePath;
    debugTargets[0].bstrOptions = options;
    debugTargets[0].guidLaunchDebugEngine = engineId;
    VsDebugTargetProcessInfo[] processInfo = new VsDebugTargetProcessInfo[debugTargets.Length];

    debugger.LaunchDebugTargets4(1, debugTargets, processInfo);
}

 Guid g = VSConstants.DebugEnginesGuids.NativeOnly;
 LaunchDebugTarget(@"C:\Users\user\source\repos\ConsoleApplication19\x64\Debug\ConsoleApplication19.exe", "", g);
  • Related