http://www.developer.com/net/cplus/article.php/3441021/Managed-C-Debugging-with-the-StackTrace-Class.htm
As a C++ developer, you're more accustomed than most to looking at the call stack during your debugging sessions. As an extension of that, many times you need a way to programmatically retrieve stack information during the execution of your application. Unfortunately, for a long time the best you had for this task was the standard __FILE__ and __LINE__ directives. This week's tip introduces the StackTrace and StackFrame classes, which enable you to walk up the entire stack, gathering specific information about each method (such as method name, return type, accessor level, and parameter). This should make your debugging life a bit easier in situations where you need to know which methods have been called (and in which order).
As an example of that, consider the following code where the btnDisplayStack_Click method calls the BadMethod that throws an exception. Since the StackTrace object is constructed with the caught exception, the only StackFrame object within the StackTrace object will represent the method that threw the exception, which is sometimes all you need—as opposed to the entire stack (The passing of the true value denotes that I also want file name and line number information.):
To download the accompanying source code for the demo, click here.
The founder of the Archer Consulting Group (ACG), Tom Archer has been the project lead on three award-winning applications and is a best-selling author of 10 programming books as well as countless magazine and online articles.
Managed C++: Debugging with the StackTrace Class
November 30, 2004

Click here for a larger image.
The StackTrace and StackFrame Classes
The StackTrace object represents the call stack at the point that it is instantiated and includes an ordered list of one or more StackFrame objects; each of which represents a method in the call stack. The following code shows an example of one method (Foo) calling another (Bar), where the second enumerates the stack and retrieves each method name in the stack:
protected: System::Void Foo()
{
// Instantiate the StackTrace object here or
// else its info will include the Foo method that
Bar(new StackTrace());
}
protected: System::Void Bar(StackTrace* stack)
{
int frames = stack->FrameCount;
// Enumerate the stack frames starting at the current
// method (0). (Frames can only be retrieved by index value.)
StackFrame* frame;
MethodInfo* methodInfo;
String* methodName;
for (int i = 0; i < frames - 1; i++)
{
frame = stack->GetFrame(i);
methodInfo = static_cast<MethodInfo*>(frame->GetMethod());
methodName = methodInfo->Name;
}
}
Note the following three things about the code:
Retrieving Method Information
Once you have a MethodInfo object, you can query it for the salient information that your application needs.
if (methodInfo->IsPublic) ...
else if (methodInfo->IsPrivate) ...
else if (methodInfo->IsFamily) ...
else if (methodInfo->IsAssembly) ...
if (methodInfo->IsStatic) ...
String* returnType = methodInfo->ReturnType;
String* methodName = methodInfo->Name;
ParameterInfo* params[] = methodInfo->GetParameters();
for (int i = 0; i < params->Count; i++)
{
ParameterInfo* param = params[i];
String* paramDisp = ((param->ParameterType->IsByRef) ? S"ByRef" : S"ByVal");
String* paramType = param->ParameterType->Name;
String* paramName = param->Name;
}
String* fileName = frame->GetFileName();
int lineNumber = frame->GetFileLineNumber();
if (fileName && lineNumber)
{
String* truncatedName;
int idx = fileName->LastIndexOf(S"\\");
if (-1 != idx)
truncatedName = fileName->Substring(idx+1, fileName->Length - idx - 1);
else
truncatedName = fileName;
}
Exceptions and the StackTrace Class
There are many ways to instantiate a StackTrace object so I won't go through them all. However, I will mention one that is especially useful when handling exceptions. When you pass an Exception object to the StackTrace constructor, only one StackFrame object will be created and it will represent the method that threw the exception.
private: System::Void btnDisplayStack_Click(System::Object * sender, System::EventArgs * e)
{
try
{
BadMethod();
}
catch(Exception* e)
{
DisplayStackFrames(new StackTrace(e, true));
}
}
private: System::Void BadMethod()
{
throw new Exception(S"Failed");
}
Download the Code
About the Author