Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Obtaining the name of the caller function

Obtaining the name of the caller function

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    emmatty
    wrote on last edited by
    #1

    Is there any way to obtain the name of the caller function in VC++? eg: Func1() { Func2() } I need to get the name of Func1() from Func2() using code. Any help would be appreciated. Thanks, John

    G C B M 4 Replies Last reply
    0
    • E emmatty

      Is there any way to obtain the name of the caller function in VC++? eg: Func1() { Func2() } I need to get the name of Func1() from Func2() using code. Any help would be appreciated. Thanks, John

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      Unfortunately, there's no way to do that directly. The compiler does create a #define symbol called __FUNCTION__, that is the name of the current function. You could modify your code as follows:

      void Func2(char *caller);
      void Func1()
      {
      Func2(__FUNCTION__);
      }
      void Func2(char *caller)
      {
      TRACE(_T("Func2 called by %s.\r\n"),caller);
      }

      Everywhere you call Func2 use Func2(__FUNCTION__);.


      Software Zen: delete this;

      C 1 Reply Last reply
      0
      • E emmatty

        Is there any way to obtain the name of the caller function in VC++? eg: Func1() { Func2() } I need to get the name of Func1() from Func2() using code. Any help would be appreciated. Thanks, John

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        Look at the DbgHelp API, in particular the symbol handling functions. There are articles here on CP about 'call-stack tracing' that will also help. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/dbghelp_functions.asp[^] ...cmk Save the whales - collect the whole set

        1 Reply Last reply
        0
        • E emmatty

          Is there any way to obtain the name of the caller function in VC++? eg: Func1() { Func2() } I need to get the name of Func1() from Func2() using code. Any help would be appreciated. Thanks, John

          B Offline
          B Offline
          Bob Stanneveld
          wrote on last edited by
          #4

          Hello, The callstack[^] is a usefull place to look for a function address and name. The only problem is that this only works in debug builds. Do you need this also in release builds? If so, you have to provide the *.pdb file with your application, which reveals vital information about your application. Hope this helps. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

          1 Reply Last reply
          0
          • G Gary R Wheeler

            Unfortunately, there's no way to do that directly. The compiler does create a #define symbol called __FUNCTION__, that is the name of the current function. You could modify your code as follows:

            void Func2(char *caller);
            void Func1()
            {
            Func2(__FUNCTION__);
            }
            void Func2(char *caller)
            {
            TRACE(_T("Func2 called by %s.\r\n"),caller);
            }

            Everywhere you call Func2 use Func2(__FUNCTION__);.


            Software Zen: delete this;

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            Gary R. Wheeler wrote:

            The compiler does create a #define symbol called __FUNCTION__

            is there something you need to do to enable this? or is this not available in VC6 ? Cleek | Image Toolkits | Thumbnail maker

            G M 2 Replies Last reply
            0
            • C Chris Losinger

              Gary R. Wheeler wrote:

              The compiler does create a #define symbol called __FUNCTION__

              is there something you need to do to enable this? or is this not available in VC6 ? Cleek | Image Toolkits | Thumbnail maker

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #6

              This may be a VC7.1 feature, as that's the only compiler where I've used it. There's no option required to enable it. The value is the fully-resolved name of the function, e.g. base1::base2::...::function.


              Software Zen: delete this;

              1 Reply Last reply
              0
              • C Chris Losinger

                Gary R. Wheeler wrote:

                The compiler does create a #define symbol called __FUNCTION__

                is there something you need to do to enable this? or is this not available in VC6 ? Cleek | Image Toolkits | Thumbnail maker

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                __FUNCTION__ and the other related macros aren't in VC 6 :( --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ "That probably would've sounded more commanding if I wasn't wearing my yummy sushi pajamas."   -- Buffy

                1 Reply Last reply
                0
                • E emmatty

                  Is there any way to obtain the name of the caller function in VC++? eg: Func1() { Func2() } I need to get the name of Func1() from Func2() using code. Any help would be appreciated. Thanks, John

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  Setting aside the problems of getting the human-readable name of functions, walking the callstack at runtime is problematic. See Don't trust the return address[^] --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Actual sign at the laundromat I go to: "No tinting or dying."

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups