启动程序并附加调试器的Visual Studio扩展:如何避免因LdrpDoDebuggerBreak()而停止?

lsmepo6l  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(216)

我正在尝试创建一个Visual Studio扩展(VS 2022),它可以启动一个程序并附加调试器。此时没有加载任何解决方案/项目。
但是,在启动时会出现一个窗口,提示已经到达断点:

有人知道避免这个断点的方法吗?或者其他API的提示?谢谢。
初始设置:

  • 创建VSIX项目
  • 添加新项目:命令

这个过程被启动挂起,因为我想添加自己的断点(不是在例子中,但工作)。
~~

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

~~

7ivaypg9

7ivaypg91#

找到解决方案:

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

相关问题