none
Get StackTrace Inside COM Method RRS feed

  • Question

  • I'm not sure where this question exactly belongs, so if someone knows of a better forum, please say so.

    I have a user-defined function for Excel 2007. This function is called via COM, probably from native code within Excel. My UDF then calls a separate method, which tries to access the stack trace. All my second method can access is the current stack frame, not the calling method or any other. My requirement needs a way to get a stack trace containing at least all the managed methods on the stack. Is this possible?

    • Moved by Leo Liu - MSFT Tuesday, October 4, 2011 7:20 AM Moved for better support. (From:Visual C# General)
    Monday, October 3, 2011 8:40 PM

All replies

  • Hi kendfrey,

    I am moving your thread into the Common Language Runtime Forum for specialized support.
    Have a nice day,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 4, 2011 7:21 AM
  • You can use Environment.StackTrace Property to get current stack trace information.

     

    Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);

     

    I hope this can help you.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, October 6, 2011 6:45 AM
  • I want a StackTrace, not a string.
    Thursday, October 6, 2011 12:32 PM
  • You can get all StrackFrame of StackTrace from the caller's frame:

    StackTrace st = new StackTrace(true);
                for(int i =0; i< st.FrameCount; i++ )
                {
                    // Note that high up the call stack, there is only
                    // one stack frame.
                    StackFrame sf = st.GetFrame(i);
                    Console.WriteLine();
                    Console.WriteLine("High up the call stack, Method: {0}",
                        sf.GetMethod());
    
                    Console.WriteLine("High up the call stack, Line Number: {0}",
                        sf.GetFileLineNumber());
                }

    More information, you can refer to:

    http://msdn.microsoft.com/en-us/library/yhs34xdh.aspx

     


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, October 7, 2011 5:20 AM
  • Unlike some people, I did my research before posting this question. I have eliminated the obvious answers. In my original post I said "All my second method can access is the current stack frame, not the calling method or any other." That is my problem.
    Friday, October 7, 2011 12:20 PM
  • Although I don't have an answer to your original question, I can confirm your last comment.

    I wonder if although understandably you can't get the full stack trace, access to the call context or thread context is possible. Might be a way to explicitly inject the stack trace to get it from the other side when needed.


    Paulo Morgado
    Friday, October 7, 2011 1:02 PM