Visual Studio 2010 Debugging C++ Console Application by "Attach to process"
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开发者_如何学Go and linked with /DEBUG and /ASSEMBLYDEBUG. Optimization disabled.
Thanks.
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.
"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?
精彩评论