Gold mine of Visual C++ tricks!
Archive for March 23, 2008
Log path informations to eventlog, Even better.
Mar 23rd
![]()
In most of our projects, when some error occurs, it’s being logged to the evenlog with line number and filename. Its done by using __FILE__ and __LINE__ macro.
But the file macros expands to the fullpath. for e.g. If i am building the delivery in my personal folder – “C:\Jijo\Build\MyProduct”, the __FILE__ macro will include this full path and finally in eventlog will contain funny paths which the end-user might see. You can see some 3ed party eventlogs in event viewer which contains authors name in path.
![]()
You can use #line directive to modify __LINE__ and __FILENAME__. The syntax is as follows,
#line lineno “FileName”
Please see an example below.
#line __LINE__ "MyProduct\Sources\JobDll\Job.cpp"
![]()
While experimenting I’ve found that – Its safe to use #line after all #includes. If its used at the top of file, It can be reinitialized by the include files. At first it was not working for me. Latter works. Anyway take care.
Handy MSDN.
Mar 23rd
![]()
In some urgent cases we need to refer function details such as .h, lib, dll name of API etc…, evenif MSDN or internet is not accessable. For that inside visual studio directory a CSV file is provided which describe complete Win32 API function descriptions.
![]()
Its here – <VisualStudioDir>\VC98\Lib\WIN32API.CSV
Open and have a look!
Hardcoding Breakpoint.
Mar 23rd
![]()
In some cases, MSDEV wont allow to set break point event if we press F9 a number of times. In those situations you can hardcode a break point by yourself.
![]()
Just insert the following line to the point where you would like to break while executing.
__asm int 3;
![]()
VisualStudio uses Interrupt 3 for implement break points. When we put a breakpoint, actually the debugger is inserting an int 3 instruction at that point. While executing, when an interrupt 3 is raised – debugger understood that one breakpoint is reached and it breaks and shows the appropriate code.
Avoid Stepping into unwanted functions while debugging.
Mar 23rd
![]()
While debugging we usually step into functions by using F11. But if the function have some CString parameters, the F11 will get into the CString constructor. This is a real headache while debugging. To avoid this, you can define a set of Step into ignorable functions in the autoexp.dat of Visual Studio.
![]()
1) The Autoexp.bat is in the following location.
<VisualStudioDir>\Common\MSDev98\Bin\AUTOEXP.DAT
2) For ignoring CString’s constructor and operator =, please add the following,
[ExecutionControl]
CString::CString=NoStepInto
CString::operator==NoStepInto
3) If you want to ignore all functions of a class then you can use,
[ExecutionControl]
CMyClass::*=NoStepInto
4) If you want to ignore all functions of a perticular namespace then you can use,
[ExecutionControl]
MyNameSpace::*=NoStepInto