Obtaining the name of the caller function
-
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
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
useFunc2(__FUNCTION__);
.
Software Zen:
delete this;
-
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
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
-
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
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[^]
-
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
useFunc2(__FUNCTION__);
.
Software Zen:
delete this;
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
-
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
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;
-
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
__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 -
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
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."