Sunteți pe pagina 1din 5

3/31/2011

Visual C++ Tips and Tricks | www.catch2

Visual C++ Tips and Tricks


Home > Tutorials
james - Fri, 08/29/2008 - 22:19

This section presents a collection of Visual C++ 6.0 tips w hich are often hard to find information about. All of the tips are useful in their ow n w ay, and many w ill w ork exactly as-is in Visual Studio .NET also, so take a moment to brow se through the collection.

1. Debug build works, Release build doesn't ??


This is the single most common posting made to programming forums and usenet. The answ er is simple - fix the bug in your code!!! The reason you are experiencing problems is usually due to uninitialized variables in functions or classes. Problems like this are present in both Debug and Release builds, but typically only manifest themselves in the release binary because the debug build is alot more tolerant to errors.

2. Fix Intellisense problems


This is the most frustrating "feature" of Visual Studio - w hen intellisense stops w orking. Simply exit Visual Studio and browse to your project directory then delete the .ncb file for your project. When you restart Visual Studio the problem w ill be fixed.

3. Auto-indent current selection


Press Alt+F8 on any highlighted text in the code-editor, and the code w ill be automatically adjusted so that each line is at the correct indentation level. Selecting an entire function body properly indents each line w ithin the function.

4. Define custom keywords


Visual C++ has the ability to highlight user-defined keyw ords in your code. The colour of these user-defined keyw ords is easily adjusted using the Options dialog, but adding user-defined keyw ords is not documented. Simply create a file called "usertype.dat" in your C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin directory (the same directory that contains MSDEV.EXE). The contents of this file is a series of custom keyw ords, each one on a separate line. These keywords do not need to contain the built-in types such as int, char etc - only additional keyw ords that you want highlighted. Here is the usertype.dat I use on my development machine. This same tip applies to Visual Studio.NET - look in the C:\Program Files\Visual Studio.NET\Common7\IDE directory (w here DEVENV.EXE lives) and place your usertype.dat file there.

5. Define custom colours in the IDE


The Visual C++ IDE only allow s you to select from 16 different colours. If you are fussy about the these colours then you can modify the base 16 colours from the following registry location:

HKCU\Software\Microsoft\Devstudio\6.0\Format\Source Window\
Inside this key there are a series of binary data items, one for each user-definable entity in the source w indow - such as comments, keyw ords, strings and normal text. Each value is made up using the same format. The first four bytes are the foreground colour, in RGBA format (3 bytes for RGB and 1 byte for padding). The background colour follow s immediately afterw ards using the same format:

[ RR GG BB xx ][ RR GG BB xx ] Foreground Background
You can use the Registry Editor to Export and Import your colour settings from machine-to-machine.

6. Column-based selection
The Visual studio editor lets you select text in a line-by-line manner. However, holding the ALT key dow n w hilst making a selection w ith the mouse causes columns of text to be selected rather than row s (lines).s

7. View structure members


Position the cusor next to the same of a structure variable and press CTRL+SPACE. The structure members popup w indow w ill appear.

8. View function parameters


Position the cursor on/after the name of a function and press CTRL+SHIFT+SPACE. The function-parameters popup w indow w ill appear.

9. Display Disassembly
Whilst debugging a project, press ALT+8 to sw itch to assembler mode.

10. Find Definition of any identifier


Right-click the mouse on any variable, function, structure or macro. A popup menu w ill appear - select "Goto Definition Of". Usually an error box w ill appear stating that you need to rebuild your project w ith brow se information. Click YES, and w ait for the project to rebuild. Now w hen you "Goto Definition Of", Visual C++ w ill open the appropriate source / header file and jump to the line containing the definition of the item you just selected.

11. Bookmark lines of text

http://www.catch22.net/tuts/vctips

1/5

3/31/2011
marked positions w ithin each source-file.

Visual C++ Tips and Tricks | www.catch2

You can bookmark lines of text for further reference, using the "Mark" commands. Press Ctrl+F2 to mark/unmark a line of text. Press F2 to skip between

The list of current bookmarks can be displayed by the Edit -> Bookmarks dialog or by pressing ALT+F2.

12. Callgraphs
Position the cursor over any function name and hit ALT+F12. If your project w as built w ith browse-information, a dialog w ill appear asking what you w ant to do. Select CallGraph to view a hierarchy of all sub-functions w hich are called by your function. This is a very useful feature often overlooked in Visual Studio.

13. Navigate around your source quickly


Hold dow n Control w hilst using the Arrow Keys to make the cursor skip betw een w ords, rather than moving character-by-character.

14. Find corresponding brackets


Position the cursor over any "{", "[", "(", "}", "]" or ")" bracket and hit CTRL+"]". The cursor w ill move to the corresponding opening/closing bracket of that expression.

15. Show/Hide whitespace


Press CTRL+SHIFT+8 to show / hide w hitespace markers.

16. Open header files


Right-click any filename in your source-code (i.e. in a string expression or #include line) and click "Open Document". Visual Studio w ill open the corresponding file in the IDE.

17. Installing Platform SDK


The simplest method to install a new SDK or DDK is to install the entire package to a new location on your hard-disk (rather than install over the old one). I alw ays recommend storing SDKs and MSDN help libraries in a "simple" directory location so it is easy to browse to using the commandprompt. i.e. C:\MSVS\SDK2004\ C:\MSVS\MSDN2004\ When you w ant to integrate this new SDK into Visual C++, bring up the Tools->Options dialog box, and select the "Directories" tab. There is a drop-down list w hich lets you entire directories for Include files (*.h) and Library files (*.lib). Enter a new directory location for the follow ing paths and move them to the top of their respective lists: C:\MSVS\SDK2004\Include C:\MSVS\SDK2004\Lib When Visual C++ compiles your project, it first searches the specified directories for include and library files. By putting the new directories at the top of the list it searches those first, before resorting to the "default" locations.

18. Preserve Keyboard and Layout settings


Visual Studio's keyboard and layout settings are stored in the follow ing locations:

HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\Keyboard HKEY_CURRENT_USER\Software\Microsoft\DevStudio\6.0\Layout
Use REGEDIT to Export these registry locations to an external file, then simply Import the settings w hen you need them on any new machine.

19. Using MAP files


You can see exactly w hat functions and global variables have been compiled into your project by looking at the corresponding MAP file in your /Debug or /Release directory. To enable this feature, open the Project>Settings dialog and select the "Linker" tab. Make sure the "Generate Map File" checkbox is selected.

20. Generate assembly-language output


You can see the full assembly language output of your project (complete w ith corresponding source-line and op-code output). Open the Project>Settings dialog and select the "C/C++" tab. Select the "Listing Files" option from the drop-dow n list and then select w hat type of output you w ant. The resulting file is generated in your /Debug or /Release directory.

21. Tiny Executables!


Generate instantly smaller executables by performing the follow ing steps: 1. Open the Project Settings dialog from the Project menu. 2. Select the C/C++ tab

http://www.catch22.net/tuts/vctips

2/5

3/31/2011

Visual C++ Tips and Tricks | www.catch2

3. Select Code Generation from the Category combo-box. 4. In the Use run-time library drop-dow n-list, select: Debug Multithreaded DLL for Debug builds Multithreaded DLL for Release builds

Now add the follow ing line to the top of your main source file and you will have instantly smaller exes:

#pragma comment(linker, "/OPT:NOWIN98")

22. Breakpoints Dialog


It is very easy to miss the Breakpoints dialog from the Edit menu (w hat a strange place to put it!). However some very pow erful debugging can be achieved. Set breakpoints when specific variables change in value, w hen a Window procedure is called w ith a certain w indow message, or w hen other conditions are met.

23. Edit Executable Resources


Visual Studio is a fully functional binary resource editor. Simply open an executable or dll using the File->Open dialog, but before pressing OK, select the "Resources" option in the "Open As" drop-dow n-list. You can use this feature to edit menus, dialogs, strings and bitmaps in regular executables and dlls such as notepad, paint, wordpad etc.

24. Display Workspace window


Press Alt+0 (Alt+zero) at any time to bring up the Project Workspace w indow .

25. Debug Release Builds


It is possible (and usually desirable) to generate debug information for your release builds. This is advantageous because you can now debug problems that only manifest themselves in a release-build environment. You can do this in such a w ay that the debug information is not stored inside the executable (thus bloating it up) - instead, it is stored in a separate .pdb (program database) file. The only entry in the executable is a small string w hich directs the debugger to your program database, so you can distribute the executable without this sensitive information. Select "Link" from the Project->Settings dialog, and then select "Debug" from the Category drop-dow n list. Make sure that the "Debug Info" checkbox is selected, and then enable "COFF Format". Now for any source-file that you w ant debugging information generated for, select that file (or your entire project) in the Project->Settings dialog, and then make sure that the "Program Database" option is enabled in the C/C++ General-settings tab.

26. Warning Level 4


Catch more errors in your project by enabling "Warning Level 4" in the C/C++ General settings tab.

27. Console and Window applications


You can sw itch betw een Console and Win32 applications at any time by editing the Linker options manually in the "Project Options" edit-field under the Project->Settings->Link dialog. Edit the /SUBSYSTEM option to sw itch betw een console and gui builds. i.e. /SUBSYSTEM:CONSOLE /SUBSYSTEM:WINDOWS

28. Generate Browse Information


Select Generate Browse Info under the C/C++ project settings tab, for any source-files that you w ant to be included. This is very useful for searching for function definitions and declarations - just right-click a function / variable and select "Goto Definition Of..." and the IDE w ill take you straight there.

29. Visual Studio Debugger - Watch Window


There are many useful commands available in the w atch w indow , w hich can alter the way variables and error values are displayed by the debugger.

Command eax @err

Description Displays the return value of a function after it returns. Display the current Win32 GetLastError value

http://www.catch22.net/tuts/vctips

3/5

3/31/2011
string,su value,hr value,wm value,x array,23 address,mb

Visual C++ Tips and Tricks | www.catch2


Display the specified string as Unicode. Treat the specified value as a HRESULT and display it's string representation Decode the specified value as a Windows Message. Display value in Hexadecimal Display exactly 23 elements of the specified array Display the specified address/variable as a memory-block hex-dump.

30. Avoid stepping into CRT functions


The reason the Visual Studio debugger steps into CRT functions (i.e. into new /delete) w hen you don't want it to, is because you installed the source-code to the CRT/MFC libraries. Either move the source-code to a different location, or prevent the Visual Studio debugger from stepping into specific functions (whilst pressing F11) by performing the follow ing steps: 1. Open AUTOEXP.DAT (from the COMMON\MSDEV98\BIN\ directory) 2. Add a section called [ExecutionControl] 3. Add a line in this section w ith the follow ing format: functionName=NoStepIntoclassName::*=NoStepInto

4. Restart the IDE for the changes to take effect. Of course as soon as you accidently step into a function that you didn't w ant to be in, simply press SHIFT+F11 to step out again. Array Tip

Comments
Very Nice
Nice collection ... Looking for more ahead... Great w ork. Keep it going.. Suraj
Suraj - Tue, 04/14/2009 - 08:04

Array

Nice info...
The article is very useful. Keep it going. houses for sale |article|select comfort Array

Nathanial - Fri, 11/20/2009 - 13:37

Thank you

Aroob - Fri, 12/04/2009 - 15:53

There are many useful commands available in the w atch w indow , w hich can alter the way variables and error values are displayed by the debugger. corporate gifts | promotional clothing | Promotional Products Array

step out
I think you need a new editor . Juicy Couture Juicy Outlet Array

goodgirls - Fri, 08/27/2010 - 08:11

I am currently working as a

mkp - Tue, 09/14/2010 - 05:51

I am currently w orking as a project Guide in a project guidance institution. So I have to be updated in programming. This site is my best companion in learning new things about programming. This post consists of tips and tricks, in Visual C++ 6.0 w as really a helpful one for me. All those thirty tips and tricks in visual c++ w ere really aw esome and I w ill share them w ith my students. Even though many of these tips provided in this post are so simple, such as displaying w orkspace w indow , I w as not aware about them before. Thank you so much for the great information. John | hosted PBX Array

Indonesian Teak Furniture


Furniture Array

agilolel - Wed, 09/15/2010 - 03:07

I've been looking something like this. This article must be made in a carefull manner. Indonesia Furniture Manufacturer and Exporter :: Teak Garden

This post consists of tips

tuhuo - Wed, 09/15/2010 - 18:44

This post consists of tips and tricks, in Visual C++ 6.0 w as really a helpful one for me. All those thirty tips and tricks in visual c++ w ere really awesome and I

http://www.catch22.net/tuts/vctips

4/5

3/31/2011

Visual C++ Tips and Tricks | www.catch2

w ill share them w ith my students. Chanel|Manolo Blahnik Array

Warning: Table './strobede_catch22/accesslog' is marked as crashed and should be repaired query: INSERT INTO accesslog (title, path, url, hostname, uid, sid, timer, timestamp) values('', 'node/13', 'http://w w w .google.ca/url? PQzCSgt7t188zhX87GIp0fSmg', '216.16.247.154', 0, '16b9a79dddaef7bc370f7936002b59bb', 165, 1301494550) in /home/strobede/catch22/includes/database.mysqli.inc on line 128

sa=t&source=w eb&cd=1&ved=0CBUQFjAA&url=http%3A%2F%2Fw w w.catch22.net%2Ftuts%2Fvctips&rct=j&q=visual%20studio%20ncb%20file&ei=GTuTTYXbBYvQgAf1pZw Z&usg=AFQjC

http://www.catch22.net/tuts/vctips

5/5

S-ar putea să vă placă și