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. How to get the caller of a fuction?

How to get the caller of a fuction?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 4 Posters 1 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.
  • L Offline
    L Offline
    Lizp
    wrote on last edited by
    #1

    at run time i want to known a fuction's caller ,how can? for example: void a(int i) { CString szCaller = GetTheCaller(); MessageBox(szCaller); } void b(int j) { a(j); } I'd like to get a message :"void b(int j)" How to implement method "GetTheCaller()" Scratch

    M D D 3 Replies Last reply
    0
    • L Lizp

      at run time i want to known a fuction's caller ,how can? for example: void a(int i) { CString szCaller = GetTheCaller(); MessageBox(szCaller); } void b(int j) { a(j); } I'd like to get a message :"void b(int j)" How to implement method "GetTheCaller()" Scratch

      M Offline
      M Offline
      MVH
      wrote on last edited by
      #2

      How 'bout just passing the caller as a string argument? Getting the calling function's name as a string is way beyond my knowledge ;)

      L 1 Reply Last reply
      0
      • L Lizp

        at run time i want to known a fuction's caller ,how can? for example: void a(int i) { CString szCaller = GetTheCaller(); MessageBox(szCaller); } void b(int j) { a(j); } I'd like to get a message :"void b(int j)" How to implement method "GetTheCaller()" Scratch

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        You want a stack trace, huh? I did it once and regreted it. It was a great bunch of work. Some starting points: 1. Look at the StackWalk Win32 API function. Walk the stack until you find the calling function. 2. Once with the address of the calling function, you'll need the SymFromAddr Win32 API function. 3. You'll need to ship the PDB file together with your application, because the Symbol functions will need this file. If you only need this info for debugging purposes, you could try looking, IIRC, the address (&i)[1] (the address of the first parameter + 4 bytes. In your case, the parameter is an int), which will give you the return address on the stack. This is a hack and can fail for a hundred reasons, including the automatic inline made by the optimizer. The right way of doing it is the StackWalk way. I see dumb people

        1 Reply Last reply
        0
        • M MVH

          How 'bout just passing the caller as a string argument? Getting the calling function's name as a string is way beyond my knowledge ;)

          L Offline
          L Offline
          Lizp
          wrote on last edited by
          #4

          It's a good idea,but didn't fit my application. Scratch

          1 Reply Last reply
          0
          • L Lizp

            at run time i want to known a fuction's caller ,how can? for example: void a(int i) { CString szCaller = GetTheCaller(); MessageBox(szCaller); } void b(int j) { a(j); } I'd like to get a message :"void b(int j)" How to implement method "GetTheCaller()" Scratch

            D Offline
            D Offline
            David Chamberlain
            wrote on last edited by
            #5

            How about creating a global array of some appropriate size, adding a call in every function in your code to add its own name to the array, and then at any point, just get the last so-many elements to know where you came from? It's not really a call "stack" as much as a call "trace." The caller of any function would be the last element in the array. The function to add to the array would have to do all the proper subscript incrementing and roll-around, making it a circular list of the last some-number of functions called. This may not be appropriate, because you do have to add something to every function. Dave "You can say that again." -- Dept. of Redundancy Dept.

            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