Monday, December 07, 2009

Learning how to debug

There are a lot of smart programmers coming out of college. You learn a lot of great and important things about computer science and how to be a good programmer. However one thing I think more junior developers (and even us senior) ones can always learn more about is debugging.

What debugging you say? Everyone knows how to debug man, just set a break point and step through the code. Well believe it or not but a time will come when you don't have complete access or control over the code you're relying on. So debugging is a lot more then just being able stop code execution. Because of this I recommend everyone read the following blog:

Using winDBG

winDBG is a debugging tool that allows you to debug windows based applications (even .net ones). It's quite powerful and while not easy to use can allow you to track down exceptions/crashes/hangs in your code. I'm just starting with it myself and the learning curve is huge but I have already learned several new techniques for finding hard to diagnose bugs in your applications. So take some time, read the blog, and hopefully we'll all be writing better applications because of it.

Thursday, October 22, 2009

MFC reading and writing from the registry

After spending some time today coding in MFC I realized I had to store some values in the registry. I haven't done this in a while and didn't find much help on the net so I am adding my experience to hopefully help some folks out. It used to be you had to use the Get/Write private profile string methods in the Win32 API. MFC has made that easier with the CSettingsStore class. This is a new class in the MFC feature pack. So to use it you have to have service pack 1 installed for visual studio 2008.

To use the class all you have to do is create an instance of that class and you can use it's methods to read,write,create, and pretty much do whatever you want in the registry. So let's look at some code that does some stuff in the registry.

We're going to create/open a new key and write some values to it
// let's create a new pointer to our registry class 
// Sadly the MSDN MFC documentation seems to be wrong
// on the constructor logic.
// This will put us under the HKEY_CURRENT_USER node
// with full read/write rights.

CSettingsStore* m_pRegistry = new CSettingsStore( FALSE, FALSE );

int test = 100;
bool testBool = false;

// First always check your pointer to make sure nothing
// jinky has happened to it.
// CreateKey opens the key or if it doesn't exist
// creates a new key and sets it as the default current node.

if( m_pRegistry &&
m_pRegistry->CreateKey( _T("\\Software\\Test Application") ) )
{
// all the methods on the CSettingsClass return BOOL to
// let you know if they succeeded
// now we're under the node we want so we're going
// to write some values, the Write methods looks for DWORD
// so we need to do some casting to ensure our values get
// written correctly

m_pRegistry->Write( _T("intValue"), (int)test );
m_pRegistry->Write( _T("boolValue"), (bool)testBool );

// always close our current connection when done
m_pRegistry->Close();

}


Reading is much the same. We create an instace of the CSettingsClass object and call the Read method.

// let's create a new pointer to our registry class 
// Sadly the MSDN MFC documentation seems to be wrong
// on the constructor logic.
// This will put us under the HKEY_CURRENT_USER node
// with full read/write rights.

CSettingsStore* m_pRegistry = new CSettingsStore( FALSE, FALSE );

int test = ;

// First always check your pointer to make sure nothing
// jinky has happened to it.
// CreateKey opens the key or if it doesn't exist
// creates a new key and sets it as the default current node.

if( m_pRegistry &&
m_pRegistry->CreateKey( _T("\\Software\\Test Application") ) )
{
// read in the value from the node
// again this will bring back a bool result for success or failure

m_pRegistry->Read( _T("test"), (int)test );

// always close our current connection when done
m_pRegistry->Close();

}


So there is some basic usage on how to read/write to the registry. A very easy way to quickly get data in and out for your application.

Wednesday, October 21, 2009

Visual Studio Tips & Tricks - Part Three Keyboard Shortcuts

Visual Studio has a keyboard command for just about everything. Many coders find having to move their hand away from their keyboard to their mouse slows them down. I agree and always look for a quick way to do something via the keyboard since I'm typing most of the time anyway. Let's start with the complete list from Microsoft.

Visual C# 2008 Bindings

Visual C# 2005 Bindings

Visual Basic 2008 Bindings

Visual Basic 2005 Bindings

A few of my favorites are:

* Ctrl-Shift-B: Build Solution (I'm an old VC coder so mine is set to F7)
* Ctrl-. (period): Show "Add Using/Imports" dropdown for unknown types.
* Ctrl-K, C: Comment Selection
* Ctrl-K, U: Uncomment Selection
* Ctrl-K, F: Format Section
* Ctrl-K, D: Format Document
* Ctrl-Space: Show Intellisense List
* Ctrl-Shift-V: Paste Loop - Hit Multiple Times to paste through the list of recent 'copies'
* Ctrl-F: Simple Find
* Ctrl-Shift-F: Find in Files
* Ctrl-H: Simple Replace
* Ctrl-Shift-H: Replace in Files
* Ctrl-N: New->Project Item
* Ctrl-Shift-N; New Project
* F9: Set Breakpoint
* Ctrl-Shift-R: Start/End Temporary Macro Recording
* Ctrl-Shift-P: Play Temporary Macro Recording
* F12; Navigate to Type (or metadata for Type) under the cursor


Please share some of your own in the comments, the more the merrier!

Friday, October 16, 2009

Visual Studio Tips & Tricks - Part Two Conditional Breakpoints

Many people know and love the debugger in Visual Studio. Being able to set a breakpoint to stop execution at a certain line in code is invaluable. However many times you find yourself only wanting to stop when a certain condition has been meet. So often you find yourself stepping through many lines of code until you reach the desired result. The solution to this is a conditional breakpoint. VS lets you set conditions on breakpoints so they only stop when that condition is met. So let's take a look at a quick example. This is a small console app that prints numbers from 1 to 10. However we want it to stop when the loop counter is equal to 5.

First we set a breakpoint on the Console.Write method.



So now we have a standard breakpoint set. Now to make it conditional right click on the breakpoint which brings up a context menu.



Select the Condition menu item to bring up the Condition dialog box where we can set our condition. VS is pretty smart and in 2008 (not sure about 2005) you can use intellisense on variables. Notice you can select Is true or Has changed. Select is true for cases like we're doing here. We want to check if the variable is the value we want it to be. Has changed lets you check if the value has changed.



Now Click OK when you're done and you will see your new conditional breakpoint. You can tell it's conditional because of the white plus sign in the middle of the red.



That's all there is to it to create conditional breakpoints. I use them all the time and you can really get creative when you set those conditional values. I have put many statements in there with ANDS, ORS, etc. Hopefully you will find them useful as well.

Tuesday, October 13, 2009

Visual Studio Tips & Tricks - Part One Formatting

This is a departure from my regular blogging about code but I find that there are many little tips and tricks you can use in VS that make it easier to use. Not that it's a bad product but there are little things that can allow you to work faster and be just a bit more productive with less effort. As a very lazy coder I find that invaluable. This post or series of posts will cover a wide variety of things but hopefully it will help some people out.

First let's mess with some options in Visual Studio. The first thing I do in VS is to turn on word wrap and line numbers. To access these you need to get into the options. So in the main menu select Tools -> then options



This will bring up an Options dialog box with a tree view on the left with a whole slew of options. For now we're going to focus on the Text Editor section. Expand the Text Editor node and select the language you are interested in. I have selected C# here for my example. On the right hand side you will see options for enabling word wrap, show glyphs for when wrapping happens and further down the screen enabling line numbers.



You now have word wrapping and line numbers at your disposal for that particular language. Now you have to do this for every language you use so when I'm writing ASP.Net applications I have to remember to go in and do the same for HTML as well. I think that's a PITA and I hope they fix it in VS 2010 so you can apply that setting to all languages supported by the IDE.

Now while we're here we might as well change some of the standard source code formatting options VS defaults to. I have a very particular way I like my code formatted and hate the default formatting VS uses so whenever I get onto a new dev box I always come in here and change the settings. Expand the language node you're interested in, my example below again uses C# and select the formatting node.



There are only 3 items under the main Formatting node. The real fun stuff are the items under the Formatting node. In each of these sections you can control how you want VS to format your source code automatically. There are a lot of options in there so I suggest you get in there and play around with them. VS let's you see how changing the option will affect the way your source code looks.

So that's it for the first post in VS tips and tricks. You can now change the look and feel of your code including word wrap, line numbers, white space, etc.

Tuesday, September 29, 2009

C# convert HTML/System.Drawing.Color

Many times I find myself having to use an HTML color that isn't defined in the System.Drawing.Color section of the framework. .Net gives you an easy way to get around this. Use the ColorTranslator.FromHtml static method in the System.Drawing namespace.

System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#F5F7F8");
String strHtmlColor = System.Drawing.ColorTranslator.ToHtml(c);

Wednesday, September 23, 2009

C# Finding Special Folders, My Documents, Home, etc

There are many times you need to find a user's home directory or my documents folder. .NET has made this pretty easy. It's all enumerated in the

Environment.SpecialFolder enumeration

The following values are available are pretty self explanatory, however the link above explains all the details.

Environment.SpecialFolder.ApplicationData
Environment.SpecialFolder.System
Environment.SpecialFolder.CommonApplicationData
Environment.SpecialFolder.CommonProgramFiles
Environment.SpecialFolder.Cookies
Environment.SpecialFolder.Desktop
Environment.SpecialFolder.DesktopDirectory
Environment.SpecialFolder.Favorites
Environment.SpecialFolder.History
Environment.SpecialFolder.InternetCache
Environment.SpecialFolder.LocalApplicationData
Environment.SpecialFolder.MyComputer
Environment.SpecialFolder.MyMusic
Environment.SpecialFolder.MyPictures
Environment.SpecialFolder.Personal
Environment.SpecialFolder.ProgramFiles
Environment.SpecialFolder.Programs
Environment.SpecialFolder.Recent
Environment.SpecialFolder.SendTo
Environment.SpecialFolder.StartMenu


Now to get the actual path stored you have to pass the enumeration you want into the

Environment.GetFolderPath method.

So

string path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);


Will load the actual physical path of the desktop directory into our string.