views:

519

answers:

4

Using C++ with Visual Studio, I was wondering if there's an API that will print the callstack for me. Preferably, I'd like to print a callstack 5 levels deep. Does windows provide a simple API to allow me to do this?

+8  A: 

It looks like Microsoft's DbgHelp library can do what you want. Consult the StackWalk64 function's documentation on MSDN for more information. Also, this CodeProject article may be useful.

bcat
A: 

Have a look at the Stackwalk and Stackwalk64 API of the DbgHelp API.

steve
+1  A: 

I believe you can get that out of their debugger API (dbghelp).

T.E.D.
+3  A: 

There is a number of ways to do this.

See http://stackoverflow.com/questions/590160/how-to-log-stack-frames-with-windows-x64

In my opinion, the simplest and as well the most reliable way is the Win32 API function:

USHORT WINAPI CaptureStackBackTrace(
     __in       ULONG FramesToSkip,
     __in       ULONG FramesToCapture,
     __out      PVOID *BackTrace,
     __out_opt  PULONG BackTraceHash
);

This FramesToCapture parameter, determines the maximum call stack depth returned.

RED SOFT ADAIR