在非托管代码中相当于System.Diagnostics.Debugger.Launch()是什么?

编程入门 行业动态 更新时间:2024-10-27 18:30:57
本文介绍了在非托管代码中相当于System.Diagnostics.Debugger.Launch()是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当满足某些条件时,我需要从本机C ++程序启动调试器。在C#中,我刚刚调用System.Diagnostics.Debugger.Launch()。我以为Win32 DebugBreak()调用会做我想要的,但是如果没有调试器,它就会终止应用程序。

I need to launch a debugger from my native C++ program when certain conditions are met. In C# I just call System.Diagnostics.Debugger.Launch(). I thought that Win32 DebugBreak() call will do what I want, but it just terminates the application if there is no debugger present.

如何启动一个新的实例调试器(着名的可能的调试器对话框)从本机代码?甚至有可能吗我可以尝试使用COM创建一个新的Visual Studio实例,但它有点复杂,并且也将锁定到一个特定版本的VS。

How do I launch a new instance of the debugger (the famous "Possible debuggers" dialog) from native code? Is it even possible? I could try to use COM to create a new instance of Visual Studio, but it is kinda complicated, and will also lock me to a particular version of VS.

推荐答案

我发现可以使用当前进程的PID直接调用vsjitdebugger.exe。确保在Visual Studio中的Tools-> Options-> Debugging-> Just-in-Time中选择了Native。

I turns out that it is possible to call vsjitdebugger.exe directly with the PID of the current process. Make sure that "Native" is selected in Tools->Options->Debugging->Just-in-Time in Visual Studio.

这里是启动调试器的C ++代码。它使用UNICODE版本的各种Win32 API。我得到系统目录,因为CreateProcess()不使用PATH。

Here's C++ code to launch debugger. It uses UNICODE versions of various Win32 APIs. I get System directory, because CreateProcess() does not use PATH.

bool launchDebugger() { // Get System directory, typically c:\windows\system32 std::wstring systemDir(MAX_PATH+1, '\0'); UINT nChars = GetSystemDirectoryW(&systemDir[0], systemDir.length()); if (nChars == 0) return false; // failed to get system directory systemDir.resize(nChars); // Get process ID and create the command line DWORD pid = GetCurrentProcessId(); std::wostringstream s; s << systemDir << L"\\vsjitdebugger.exe -p " << pid; std::wstring cmdLine = s.str(); // Start debugger process STARTUPINFOW si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); if (!CreateProcessW(NULL, &cmdLine[0], NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return false; // Close debugger process handles to eliminate resource leak CloseHandle(pi.hThread); CloseHandle(pi.hProcess); // Wait for the debugger to attach while (!IsDebuggerPresent()) Sleep(100); // Stop execution so the debugger can take over DebugBreak(); return true; }

更多推荐

在非托管代码中相当于System.Diagnostics.Debugger.Launch()是什么?

本文发布于:2023-11-07 04:27:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   System   Diagnostics   Debugger   Launch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!