Posts

Showing posts from 2009

I have joined the OSS community

Besides working a professional software engineer I have a few side projects that I have created. You can see them over at www.jrl-software.com. Recently I decided I would take the plunge and take an application I've created and license it under the GPL v3.0. I'm still not entirely sure how I feel about this whole GPL thing so this has been an interesting expirement for me. I like being able to download source code for many popular programs I use. But at the same time it's disconcerting to know people can see exactly what I've done and written in my code as well. So if anyone wants to look at the project it's hosted on sourceforge and you can see it using the link below. Filesort OSS It will be interesting to see what life if any takes off from this project. I have railed against OSS in the past but I think that was more of a rant against PHP than something against OSS in general.

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 techn

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

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 * Ctr

Visual Studio Tips & Tricks - Part Two Conditional Breakpoints

Image
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 abo

Visual Studio Tips & Tricks - Part One Formatting

Image
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

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);

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 Env

Sql Server Reporting Services export to PDF in Landscape

So I have found a need to create ssrs reports in pdf format. Some of these reports contain a lot of data. So I needed to layout the page in landscape format. Sadly there is no page layout settings in srrs (once again Crystal wins but that is another story). So to achieve the same thing you will need to change the settings of the page. Open the properties of the report and set the width=29cm and height=21cm. These are the settings for landscape. Now you have a lot of extra room to move things around. I also found to maximize the report you can get the width of the report to about 22.86cm. So to summarize width=29cm (11.41") height=21cm (8.27") report data=27.30cm (10.75")

C# Finding all tables in access file

I recently found a need to access table names found in an access file dynamically. I found some code on the net to do this. But I discovered a bug, the code I was using would only read static tables. The files I was getting could contain linked and pass-through tables. So after some more searching I found the best way was to get all the tables (system, access) etc and then filter out only what I needed. So after some coding here is the solution. This is sitting in a function public static DataTable GetTableNames( string accessFile ) { OleDbConnection con = null; try { // create a connection string String connect = String.Format( "Provider=Microsoft.JET.OLEDB.4.0;data source={0}", accessFile ); // open the connection con = new OleDbConnection( connect ); con.Open(); // get all the tables in the file, system, etc DataTable dtAllTables = con.GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object[] { null, null, null, null } ); DataTable fi

String.Replace vs Regex.Replace

Image
I recently ran into a situation where I had to do some string manipulation. I had to replace some bad text with valid text. All this had to do with creating valid xml element names. So we had to string white space and some other invalid characters and replace them with valid xml element formatting. To do this we ended up using the Regex.Replace static method. Simply because there was some complexity involved that would have entailed using more then 1 String.Replace call when we can do it all with one regular expression. This caused some discussion about how heavy the regex class is and when to use it. I decided to do some testing and these are my results. First the code static void Main( string[] args ) { // this is our test value string testValue = "This is a test string"; int count; // run the regex replace DateTime regexStart = DateTime.Now; for( count = 0; count < 10000000; count++ ) { string newValue = Regex.Replace( testValu

Your code sucks and I hate you

It's been a while since I posted any code. I've been very very busy actually working and haven't had a chance to update the 'ol blog. Today we're getting away from straight code and getting more into the soft side of programming. I read an interesting article on code reviews. You should take a look at it over here: Your code sucks and I hate you Code reviews are something we do every time code gets checked into the main base line. They are important and help ensure high quality of code. However if you don't take some care they can quite easily get out of hand and develop into a personality/pissing contest between people. If you follow some decent guidelines you will find yourself amazed by the results. I know I hated code reviews in the past but have found them very helpful in uncovering bugs before they went into production. It does take some letting go of the ego but if you go in with the right attitude they will make you a better programmer/engine

MFC reading from a .ini file

Recently I came across a need to read from an .ini file for an MFC application I'm working on. Now some might say why even bother using an .ini file, that's what the registry is for douchebag! Well I need something that a user can easily edit and store 3 small values and creating screens/forms to read/write to the registry doesn't interest me too much. So a simple ini file will do the trick. I found the perfect function is GetPrivatProfileString I however ran into some issues using this function. It's a win32 function and I'm using it in an MFC based application with CStrings. The function itself is expecting LPWSTR parameters so I thought by using the CString.GetBuffer method this would work. But I found this eventually will run you out of stack memory and cause exceptions. This even after using the ReleaseBuffer method that MSDN tells us to call after using GetBuffer. So in order to read all the values I needed from the file I found I had to read in LP

MFC search tree view based on label (name)

So I recently came across a need to use a tree view in an MFC based application. I also found I needed to search that tree based on the label (text displayed to the user). After some research I found an old link here at MSDN . However the article is almost 6 years old. MFC has changed a bit since then. So I had to change the code a bit to use all the new MFC macros for tree view usage. All these macros are also defined as functions on the CTreeCtrl class but doing it this way allows you to traverse any tree you want instead of being tied to a single instance. You can read more about the macros and treeviews here . All items are passed in except for MAXTEXTLEN which needs to be defined somewhere and included for reference. Mine is set to 100 since we can get fairly long long names in our application. The function: HTREEITEM GetItemByName( HWND hWnd, HTREEITEM hItem, LPCTSTR szItemName ) { try { // If hItem is NULL, start se

C# get current fiscal year stop and end dates

The following two functions can be used to determine the start and stop dates of the current fiscal year. They assume a standard FY of 10/1 - 9/30. The same logic applies if you want to modify them with different start dates. You will just need to use whatever DateTime values you want instead of the current. Get the start date // get the current month and year int month = DateTime.Now.Month; int year = DateTime.Now.Year; // if month is october or later, the FY started 10-1 of this year // else it started 10-1 of last year return month > 9 ? new DateTime( year, 10, 1 ) : new DateTime( year - 1, 10, 1 ); Get the stop date // get the current month and year int month = DateTime.Now.Month; int year = DateTime.Now.Year; // if month is october or later, the FY ends 9/30 next year // else it ends 9-30 of this year return month > 9 ? new DateTime( year + 1, 9, 30 ) : new DateTime( year, 9, 30 );

C# send email with .net framework

So I have come across a need to send some email via the .net framework to report status to a disto list we have for a product we support. The following code below shows the basics of sending a simple text email with 1 attachment. The values being assigned to the message are parameters coming from a function. You can assign any string value you want to them. All this can be found in the using System.Net.Mail namespace. // build the mail message MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress( From ); // this can be one at a time, or a , delimited list foreach( string toAddress in ToAddresses ) { try { mailMessage.To.Add( new MailAddress( toAddress ) ); }//end try catch( Exception ) { // do something with your error }//end catch }//end foreach mailMessage.Subject = SubjectLine; mailMessage.Body = Body; // add the attachment mailMessage.Attachments.Add( new Attachment( AttachmentLocation ) ); // send the message vi