Visual Studio 2010 调试 C++通过“附加到进程”的控制台应用程序

发布于 2024-10-24 11:36:21 字数 269 浏览 5 评论 0原文

我想调试我的 C++ 应用程序。它需要一些命令行参数。我知道我可以在“项目属性”对话框中指定它们,但我正在考虑将调试器附加到我将用来运行程序的控制台进程。

这有可能吗?

当我尝试时,VS 不会加载符号(当前不会命中制动点。尚未为此文档加载任何符号。),即使我在 Debug->Options 中指定了符号目录并且设置。

活动配置是调试。使用 /ZI 编译并使用 /DEBUG 和 /ASSEMBLYDEBUG 链接。优化已禁用。

谢谢。

I would like to debug my C++ application. It takes a few command line arguments. I know I can specify them in the "Project Properties" dialogue, but I was thinking about attaching the debugger to a console process which I would use to run my program.

Is this possible at all?

When I try, VS does not load the symbols (The brakepoint will not currently be hit. No symbols have been loaded for this document.) even though I specify the symbol directory in Debug->Options and Settings.

Active configuration is Debug. Compiled with /ZI and linked with /DEBUG and /ASSEMBLYDEBUG. Optimization disabled.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云胡 2024-10-31 11:36:21

我认为你也可以动态地做到这一点。我分享 C++ 代码:

在开头附近的某个地方:

#include <atlbase.h>
#pragma warning( disable : 4278 )
#pragma warning( disable : 4146 )
//The following #import imports EnvDTE based on its LIBID.
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids
//The following #import imports EnvDTE80 based on its LIBID.
#import "libid:1A31287A-4D7D-413e-8E32-3B374931BD89" version("8.0") lcid("0") raw_interfaces_only named_guids
//The following #import imports EnvDTE90 based on its LIBID.
#import "libid:2ce2370e-d744-4936-a090-3fffe667b0e1" version("9.0") lcid("0") raw_interfaces_only named_guids
//The following #import imports EnvDTE100 based on its LIBID. - This doesn't work for me
//#import "libid:26ad1324-4b7c-44bc-84f8-b86aed45729f" version("10.0") lcid("0")  raw_interfaces_only named_guids
#pragma warning( default : 4146 )
#pragma warning( default : 4278 )

然后在主要部分的某个地方:

CoInitialize(NULL);
CComPtr<EnvDTE::_DTE> m_pDTE;
CComPtr<EnvDTE80::DTE2> m_pDTE2;
CLSID clsid;
CLSID clsid2;
CLSIDFromProgID(L"VisualStudio.DTE.10.0",&clsid);
CLSIDFromProgID(L"VisualStudio.DTE.10.0",&clsid2);

CComPtr<IUnknown> punk;
CComPtr<IUnknown> punk2;
// Get a running instance of Visual Studio.
HRESULT hr = GetActiveObject(clsid,NULL,&punk);
hr = GetActiveObject(clsid2,NULL,&punk2);
m_pDTE = punk;
m_pDTE2 = punk2;

EnvDTE::DebuggerPtr p_dbg;
while(FAILED(m_pDTE2->get_Debugger(&p_dbg)))
{
}
bool Sucess = false;
do
{
    EnvDTE::ProcessesPtr p_processes;
    while(FAILED(p_dbg->get_LocalProcesses(&p_processes)))
    {
    }
    long max;
    if(FAILED(p_processes->get_Count(&max)))
    {
        std::cerr << "Failed to obtain process count.";
        return 2;
    }
    for(long i = 0; i < max; ++i)
    {
        EnvDTE::ProcessPtr p_process;
        // Get item and check for process id if any
        if(FAILED(p_processes->Item(variant_t(i), &p_process)))
            continue;
        if(p_process != nullptr)
        {
            long process_id;
            while(FAILED(p_process->get_ProcessID(&process_id)))
            {
            }
            if(process_id == GetProcessId(GetCurrentProcess()))
            {
                p_process->Attach();
                Sucess = true;
                break;
            }
        }
    }
} while(!Sucess);

希望它会对某人有所帮助。

I think you can also do it dynamically. I share code for c++:

Somwhere near the beginning:

#include <atlbase.h>
#pragma warning( disable : 4278 )
#pragma warning( disable : 4146 )
//The following #import imports EnvDTE based on its LIBID.
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids
//The following #import imports EnvDTE80 based on its LIBID.
#import "libid:1A31287A-4D7D-413e-8E32-3B374931BD89" version("8.0") lcid("0") raw_interfaces_only named_guids
//The following #import imports EnvDTE90 based on its LIBID.
#import "libid:2ce2370e-d744-4936-a090-3fffe667b0e1" version("9.0") lcid("0") raw_interfaces_only named_guids
//The following #import imports EnvDTE100 based on its LIBID. - This doesn't work for me
//#import "libid:26ad1324-4b7c-44bc-84f8-b86aed45729f" version("10.0") lcid("0")  raw_interfaces_only named_guids
#pragma warning( default : 4146 )
#pragma warning( default : 4278 )

Then somhere in the main:

CoInitialize(NULL);
CComPtr<EnvDTE::_DTE> m_pDTE;
CComPtr<EnvDTE80::DTE2> m_pDTE2;
CLSID clsid;
CLSID clsid2;
CLSIDFromProgID(L"VisualStudio.DTE.10.0",&clsid);
CLSIDFromProgID(L"VisualStudio.DTE.10.0",&clsid2);

CComPtr<IUnknown> punk;
CComPtr<IUnknown> punk2;
// Get a running instance of Visual Studio.
HRESULT hr = GetActiveObject(clsid,NULL,&punk);
hr = GetActiveObject(clsid2,NULL,&punk2);
m_pDTE = punk;
m_pDTE2 = punk2;

EnvDTE::DebuggerPtr p_dbg;
while(FAILED(m_pDTE2->get_Debugger(&p_dbg)))
{
}
bool Sucess = false;
do
{
    EnvDTE::ProcessesPtr p_processes;
    while(FAILED(p_dbg->get_LocalProcesses(&p_processes)))
    {
    }
    long max;
    if(FAILED(p_processes->get_Count(&max)))
    {
        std::cerr << "Failed to obtain process count.";
        return 2;
    }
    for(long i = 0; i < max; ++i)
    {
        EnvDTE::ProcessPtr p_process;
        // Get item and check for process id if any
        if(FAILED(p_processes->Item(variant_t(i), &p_process)))
            continue;
        if(p_process != nullptr)
        {
            long process_id;
            while(FAILED(p_process->get_ProcessID(&process_id)))
            {
            }
            if(process_id == GetProcessId(GetCurrentProcess()))
            {
                p_process->Attach();
                Sucess = true;
                break;
            }
        }
    }
} while(!Sucess);

Hope it will help somebody.

鱼忆七猫命九 2024-10-31 11:36:21

“附加到进程”通常用于在程序运行后附加到程序。

听起来您正在尝试附加到将启动程序的命令提示符。这是一个不同的过程,当附加到 cmd.exe 时,您将收到各种警告,因为它不包含调试信息。

但是,如果启用了“同时调试子进程”选项,则一旦启动程序,调试器将在那时安装断点。

另请阅读:可以让 Visual Studio 进行调试吗像 WinDBG 这样的子进程?

"Attach to process" is usually used to attach to YOUR PROGRAM after it's already running.

It sounds like you are trying to attach to the command prompt which will launch your program. This is a different process, and when attaching to cmd.exe you will get all sorts of warnings because it does not include debug information.

However, if you have the "also debug child processes" option enabled, once you start your program the debugger will install the breakpoints at that time.

Read also: Can Visual Studio be made to debug child processes like WinDBG?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文