Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C++

Implementing the __FUNCTION__ Macro in VC++ 6.0

Rate me:
Please Sign up or sign in to vote.
2.85/5 (13 votes)
27 Mar 2006CPOL2 min read 116.9K   1.2K   23   26
Tired of searching for the __FUNCTION__ macro to debug your apps in VC++ 6.0? Here's a little overkill to replicate it
Sample Image - xtrace.jpg

Introduction

Ok, so you're stuck with VC++ 6.0 (can't upgrade to .NET) but you need that nifty __FUNCTION__ macro for your debug sessions or, as in my case for debug session logging, and you Googled for it all over the world (including China) to get such ambiguous answers as "I'm a 100% sure that VC++ 6.0 implements it" and "get .NET you @$$#ole!". So you're outdated, sour and two steps shy of self-suicide (what an oxymoron).

Enter The Code Project (where you should have looked in the first place) to find that there's a whole world of portable executable image help APIs and symbols and processes and threads you really never cared about before, and... oh well.

There's also a whole debug section on CodeProject I didn't know about. Now get to Zoltan Csizmadia's article in which he implements the Extended Trace functions. My version is a downsized one from that of Csizmadia's, in which I only retrieve function names (to the point of omitting class names and arguments) to produce the kind of output shown in the figure. Granted, the code looks like overkill to achieve what .NET (VC++ 7.0 to my understanding) does intrinsically, but it works for me, and I bet you something not too worthy that intrinsically means going through a similar image-symbol-scanning process. In any case, I learned a lot about PEs, the Image Help API, debugging and a lot of stuff I wasn't really looking for, and ultimately, I got away with what I wanted to do.

To learn about all that stuff, get to the debug section of The Code Project, for I will not go into it in this article. The code is almost self-explanatory and fairly commented; just add the two files (header and CPP) to your project, add the #include directives at the proper places, and call InitSymInfo() upon start, and KillSymInfo() before exiting; use the __FUNCTION__ macro anywhere in your code; it returns a (temp) LPCTSTR you can use as you like.

Zoltan's version does retrieve the full function prototype, its arguments and their values; if you need all that or just want to check it out, click here.

Thank Zoltan and enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Texas Capital Bank
United States United States
Professional software engineer with 30+ years of experience delivering systems across diverse industries, looking for the next opportunity to deliver cutting edge end-to-end technology solutions.

Avid reader, disciplined writer and enthusiastic tinkerer with a background in electronics, looking inside and thinking outside the box, genuinely passionate about robust, extensible, reusable and performant code.

Framework developer leading, coaching and learning about best practices, code quality, DevOps and software and data lifecycle management with an agile mindset to create the most elegant and sustainable solutions.

Comments and Discussions

 
QuestionDoes not work for me. StackWalk() fails with error code: error_partial_copy. Pin
sondhi_chakraborty11-Feb-12 18:48
sondhi_chakraborty11-Feb-12 18:48 
GeneralRe: callStack.AddrFrame.Offset = 0 Pin
MYeager5630-Sep-10 9:34
MYeager5630-Sep-10 9:34 
QuestionSymGetSymFromAddr in GetFuncInfo fails due to GetCurrentProcess returning 0xffffffff Pin
X-treem18-Aug-06 4:51
X-treem18-Aug-06 4:51 
AnswerRe: SymGetSymFromAddr in GetFuncInfo fails due to GetCurrentProcess returning 0xffffffff Pin
hector [.j.] rivas22-Aug-06 6:14
hector [.j.] rivas22-Aug-06 6:14 
GeneralRe: SymGetSymFromAddr in GetFuncInfo fails due to GetCurrentProcess returning 0xffffffff Pin
cavalierfire22-May-08 22:53
cavalierfire22-May-08 22:53 
Generalmy modification Pin
comealong25-Jul-06 20:07
comealong25-Jul-06 20:07 
QuestionRe: my modification Pin
Christopher Stratmann26-Jul-06 2:19
Christopher Stratmann26-Jul-06 2:19 
AnswerRe: my modification Pin
comealong26-Jul-06 20:21
comealong26-Jul-06 20:21 
GeneralRe: my modification Pin
hector [.j.] rivas29-Jul-06 8:24
hector [.j.] rivas29-Jul-06 8:24 
Question__FUNCTION__ not working in console debug mode Pin
Christopher Stratmann6-Jul-06 4:13
Christopher Stratmann6-Jul-06 4:13 
AnswerRe: __FUNCTION__ not working in console debug mode Pin
juls337-Jul-06 5:25
juls337-Jul-06 5:25 
GeneralRe: __FUNCTION__ not working in console debug mode Pin
dasdasa27-Aug-06 20:05
dasdasa27-Aug-06 20:05 
GeneralRe: __FUNCTION__ not working in console debug mode Pin
conrad Braam14-Jan-08 5:17
conrad Braam14-Jan-08 5:17 
Question__FUNCTION__ working in Release Mode? Pin
Christopher Stratmann3-Jul-06 2:37
Christopher Stratmann3-Jul-06 2:37 
AnswerRe: __FUNCTION__ working in Release Mode? Pin
Graham Reeds7-Jul-06 3:52
Graham Reeds7-Jul-06 3:52 
QuestionRe: __FUNCTION__ working in Release Mode? Pin
Christopher Stratmann7-Jul-06 3:56
Christopher Stratmann7-Jul-06 3:56 
AnswerRe: __FUNCTION__ working in Release Mode? [modified] Pin
hector [.j.] rivas12-Jul-06 14:44
hector [.j.] rivas12-Jul-06 14:44 
AnswerRe: __FUNCTION__ working in Release Mode? Pin
dasdasa25-Aug-06 1:55
dasdasa25-Aug-06 1:55 
GeneralRe: __FUNCTION__ working in Release Mode? Pin
dasdasa25-Aug-06 2:05
dasdasa25-Aug-06 2:05 
QuestionQuestion about UnDecorateSymbolName Flags Pin
Christopher Stratmann30-Jun-06 1:32
Christopher Stratmann30-Jun-06 1:32 
AnswerRe: Question about UnDecorateSymbolName Flags Pin
hector [.j.] rivas12-Jul-06 15:19
hector [.j.] rivas12-Jul-06 15:19 
GeneralcallStack.AddrFrame.Offset = 0 Pin
Rick Crone2-Mar-06 5:28
Rick Crone2-Mar-06 5:28 
GeneralRe: callStack.AddrFrame.Offset = 0 Pin
hector [.j.] rivas10-Mar-06 15:10
hector [.j.] rivas10-Mar-06 15:10 
GeneralRe: callStack.AddrFrame.Offset = 0 Pin
Graham Reeds7-Jul-06 4:11
Graham Reeds7-Jul-06 4:11 
GeneralRe: callStack.AddrFrame.Offset = 0 Pin
hector [.j.] rivas12-Jul-06 15:33
hector [.j.] rivas12-Jul-06 15:33 
make sure you are doing the correct number of iterations when walking the stack.

The callStack.AddrFrame.Offset is set from the EBP register
directly, which is part of the CONTEXT section that is returned if the
ContextFlags word contains the flag CONTEXT_CONTROL, so try adding the flag
before retrieving the thread's context:

context.ContextFlags = CONTEXT_FULL | CONTEXT_CONTROL;<br />
<br />
if (!GetThreadContext(hThread, &context))<br />
	return "";


See WINNT.H for more details!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.