Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting the name of the caller function

641 views
Skip to first unread message

Yash

unread,
Mar 19, 2009, 4:46:45 AM3/19/09
to
Hi,

I have a function which logs a message to a log file. This function
will be called from many functions within my .vbs script. In this
function I want to find the name of the function which has called it
(its parent function). This is so that the name of the caller gets
logged.

Is there a way to obtain the name of the caller or the entire call
stack in .vbs?

Thanks,
Yash

Pegasus [MVP]

unread,
Mar 19, 2009, 5:16:12 AM3/19/09
to

"Yash" <yas...@gmail.com> wrote in message
news:8e6f303c-344f-483d...@w35g2000prg.googlegroups.com...

You could pass the name of the calling function as an additional parameter:

Sub SetPrinter
...
LogEvent (55, "SetPrinter")
...
End Sub

Function LogEvent (iEventID, sCaller)
wscript.echo "ID=" & iEventID & " Caller=" & sCaller
...
End Function


Yash

unread,
Mar 19, 2009, 5:58:46 AM3/19/09
to
The reason why I want to obtain it from call stack is so that I do not
have to pass it as an argument.

Alex K. Angelopoulos at

unread,
Mar 19, 2009, 2:05:06 PM3/19/09
to
No, unfortunately. This is one of the awkward issues about the plain-vanilla
nature of most of the script hosts - there's no way to generally determine
enclosing scopes for segments of code. In theory, a custom script host could
expose this kind of functionality, but that's a theoretical point that's not
likely to be very helpful.

In the past I've tried to do similar tracing activity within scripts, and
the approach that I've used _may_ be helpful, although it's not as
straightforward as what you're after. I created a generic linewriter
function to make calls to the logger easier. Then within each
function/routine in the code I was tracing I would insert a logger call at
the beginning and end like this:

function GetFoo(args)
WriteLog "<Entering GetFoo"
<code here>
GetFoo = ...
WriteLog ">Exiting GetFoo"
end function

This is, of course, quite cumbersome. You need to look through the log for
matching enter/exit statements, and the function doesn't even know its own
name automatically, making it a bit irksome to update.

Out of curiosity, what's your end objective with this? I don't know if
there's a palatable solution for your problem, but there may be another
angle on it you haven't considered.

"Yash" <yas...@gmail.com> wrote in message

news:a24d885b-defe-4983...@s9g2000prg.googlegroups.com...

mr_unreliable

unread,
Mar 19, 2009, 2:22:04 PM3/19/09
to
Yash wrote:
> I have a function which logs a message to a log file. This function
> will be called from many functions within my .vbs script. In this
> function I want to find the name of the function which has called it
> (its parent function). This is so that the name of the caller gets
> logged.
>

hi Yash,

You might try running your script under a debugger.

One debugger which might work is the "Microsoft Script
Editor". The MSE comes as part of msOffice, but may
also be available elsewhere. Earlier versions are
named MSE.EXE, and later versions are named MSE7.EXE,
(or maybe MSE8 or MSE9???.

MSE does have a Call Stack Viewer. Look under:
View => Debug Widows => Call Stack.

I suspect there may be other debuggers which also
have a call stack viewer. I have no experience with
Sapien's PrimalScript, but it is the "Cadillac" of
scripting tools, and has just about every feature
you can think of, so you may find a call stack viewer
in PrimalScript too.

As far as "programatically" getting at the call stack
-- that would be problematical at best. You may be
able to find a 3re-party control to do it for you, but
I have never seen one. You may also be able to "do-it-
yourself" -- but I strongly suspect that some assembly
code will have to come into play somewhere along the
way.

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but, no guarantee
the answers will be applicable to the questions)

mr_unreliable

unread,
Mar 19, 2009, 2:42:39 PM3/19/09
to
mr_unreliable wrote:
> As far as "programatically" getting at the call stack
> -- that would be problematical at best. You may be
> able to find a 3re-party control to do it for you, but
> I have never seen one. You may also be able to "do-it-
> yourself" -- but I strongly suspect that some assembly
> code will have to come into play somewhere along the
> way.
>

For anybody who would like to "do-it-yourself", I suggest
reading Jochen Kalmbach's article (with code) entitled:
"Walking the CallStack":

http://www.codeproject.com/KB/threads/StackWalker.aspx

Jochen is using microsoft's StackWalker/StackWalker64 tool,
which I understand is part of the microsoft "Debugging Tools
for Windows" package, which is part of the microsoft
"Windows Developer Kit" (WDK).

http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx

If you don't have this installed, it is apparently downloadable
(for free) from msdn.

cheers, jw

0 new messages