======================================== VC Tips ======================================== Teach VC to intelligently expand classes/structs in debugger Isn't it neat how VC's debugger knows how to intelligently expand CStrings, CPoints, POINTS, and alot of other stuff? Well, you can teach it to handle your own structs and classes. Just edit autoexp.dat in sharedide/bin (in the directory where Visual Studio is installed.) The format of that file is fairly complicated, so I suggest just copying the examples already in the file. ------------------------------------------------------------ Add user defined keywords for syntax highlighting Why can you set a color for user defined keywords in Tools > Options > Format? Where do you set the keywords? Easy, just create usertype.dat in the sharedide/bin directory that Visual Studio is installed in. Put your keywords in that file, one per line. ------------------------------------------------------------ Custom Tip of the Day Any files with the extension ".tip" in the sharedide/bin/ide directory where Visual Studio is installed will be read. You can delete the .tip files Microsoft provides and add your own. You might want to take a look at Microsoft's files to see the format. I personally suggest filling the .tip file with quotes, news, or something more useful and entertaining that the TotDs. ------------------------------------------------------------ How to use .cc file extensions for C++ Make the following modifications to the registry: HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\Text Editor\Tabs/Language Settings\C/C++ FileExtensions=cpp;cxx;c;h;hxx;hpp;inl;tlh;tli;rc;rc2;cc;cp HKEY_USERS\S-1-5-21-1219703950-274334628-1532313055-1335\Software\Microsoft\DevStudio\6.0\Build System\Components\Platforms\Win32 (x86)\Tools\32-bit C/C++ Compiler for 80x86 Input_Spec=*.c;*.cpp;*.cxx,*.cc,*.cp HKEY_USERS\S-1-5-21-1219703950-274334628-1532313055-1335\Software\Microsoft\DevStudio\6.0\Build System\Components\Tools\ Input_Spec=*.c;*.cpp;*.cxx;*.cc;*.cp Add the flag "/Tp" to the compiler settings for the project. ------------------------------------------------------------ Removing the "docking" capability from the menus In Tools > Options..., in the Workspace tab, turn on "Use screen reader compatible menus". Your menus will lose the gripper (the double line on the left edge indicating dockability), and will stay nailed down like they should. Unfortunately this also removes the icons from the menus. ------------------------------------------------------------ Useful build messages The following macros make it easy to add reminders which are displayed when code is compiled. You can double click on a reminder in the Output Window and jump to the line. Useful for marking TODOs. (Originally from Windows Developer Journal, 1997?) // Statements like: // #pragma message(Reminder "Fix this problem!") // Which will cause messages like: // C:\Source\Project\main.cpp(47): Reminder: Fix this problem! // to show up during compiles. Note that you can NOT use the // words "error" or "warning" in your reminders, since it will // make the IDE think it should abort execution. You can double // click on these messages and jump to the line in question. #define Stringize( L ) #L #define MakeString( M, L ) M(L) #define $Line \ MakeString( Stringize, __LINE__ ) #define Reminder \ __FILE__ "(" $Line ") : Reminder: " Once defined, use like so: #pragma message(Reminder "Fix this problem!") This will create output like: C:\Source\Project\main.cpp(47): Reminder: Fix this problem! ------------------------------------------------------------ Hard code a debugger breakpoint If you need to insert a hard breakpoint in your code (perhaps because you need to attach to a process), simply add the following line to your code. __asm int 3; ------------------------------------------------------------ Tracking GDI resource leaks Plenty of tools exist to help track down memory leaks. You've got the debug heap, Rational Purify for Windows, HeapAgent, and other tools. But there aren't any good tools to help track GDI resource leaks. A resource leak can crash the system under Windows 95 or Windows 98, and can ruin performance on any Windows operating system. The article "Resource Leaks: Detecting, Locating, and Repairing Your Leaky GDI Code"(http://msdn.microsoft.com/msdnmag/issues/01/03/leaks/default.aspx) is the only real help I've found. In particular, the article includes a program that tracks current globally allocated resources under Windows 95 or Windows 98. You can track what resources were created between a start and stop points, and display what those resources are. Check the article for the download (Leaks.exe). ------------------------------------------------------------ Change operator new to throw an exception Visual C++ operator new return NULL on failure (See KB Q167733). The STL expects operator new to throw an exception on failure, otherwise its behaviour is undefined. You should install a new handler that will be called when operator new fails. Your handler will simply force standard conforming behaviour be throwing a std::bad_alloc exception. Do not implement the second example in KB Q167733. The code given (apart from not compiling) also hooks malloc to throw an exception on failure. This behaviour is not desirable and most existing library code is probably not anticipating this behaviour. Also, if you do set malloc to throw on failure, then operator new(size_t, const std::nothrow_t) will also throw on failure. This is definitely not desired behaviour. The following code demonstrates how to correctly set operator new to throw on failure: // Force operator new failure to throw an exception #include #include namespace { int new_handler(size_t) { throw std::bad_alloc(); return 0; } struct new_handler_installer { _PNH m_old_new_handler; new_handler_installer() { m_old_new_handler = _set_new_handler(new_handler); } ~new_handler_installer() { _set_new_handler(m_old_new_handler); } }; #if defined(_LIB) // Included via a static library - force the inclusion extern "C" new_handler_installer g_new_handler_obj; new_handler_installer g_new_handler_obj; #pragma comment(linker, "/include:_g_new_handler_obj") #else new_handler_installer g_new_handler_obj; #endif } You will also need to implement your own version of operator new(std::nothrow) as the installed new_handler now results in operator new (std::nothrow) throwing an exception in release builds only. ------------------------------------------------------------ Include Debugging information in release builds If you include debugging information in your release builds, it becomes possible to step through the (optimised) code when you experience re-producible release errors. If the error occurs when the application is running on a computer that also has Visual C++ installed, it is possible to attach the debugger to the running process and investigate the error. If the application is installed on a remote computer and the error is reproducible, you can remotely debug the application from your development box (Visual C++ is currently not able to attach to a running process on a remote computer). This is priceless for beta versions and in-house applications. Adding debugging information to your release builds does not impact the performance of your application, neither does it allow your code to be reverse engineered and it only adds a small, fixed, increase to the size of the resulting executable. When you start to generate debugging information, be sure to ship it with your product and make sure that you keep the versions of the debugging files (PDB) up to date with the binaries (EXE, DLL) . For more information about debugging and release builds, see this fantastic article by John Robbins. ------------------------------------------------------------ Watch the value of GetLastError in the debugger (VC 6 only) Place ERR in the watch window to monitor the value of GetLastError. Place ERR,hr in the watch window to monitor the string message corresponding to the value of GetLastError. ------------------------------------------------------------ Format system HRESULT values to see the string error message Append any HRESULT value in the watch window with ,hr (E.G. hrRes,hr) to watch the string equivalent of the failed HRESULT value (only works for system HRESULT values). This also works for Win32 error codes. ------------------------------------------------------------