Posts

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 LPWST...

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 search...

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 via the smt...

MFC using an ImageList in a TreeView control

This is a bit of a departure for my regular blogging but I've been working in MFC lately so I'm posting some of the things I've learned. So I have a tree view control that I want to add some images to. In .net this is easy just drop in an ImageList and add the images in the designer. In MFC this takes a wee bit more work. Here is the actual code: /* create the image list with 16x16 sized icons, 32 bit color, 0 images to start, and resize the array 1 at a time when adding new images*/ m_ImageList.Create(16,16,ILC_COLOR32,0,1); m_ImageList.Add( AfxGetApp()->LoadIcon(IDI_ICON_ORG) ); m_ImageList.Add( AfxGetApp()->LoadIcon(IDI_ICON_POSITION) ); // set theimage list to be used in the tree view control with normal sized icons m_treeGFM.SetImageList( &m_ImageList, LVSIL_NORMAL ); m_ImageList a CImageList member variable. m_treeGFM is a member variable mapped to my tree view control. The icons are resources I have added to my application. For mo...

Validating an email address with a regular expression

Below is a quick and dirty post on how to use the C# regular expression library to validate an email address. The regular expression stuff is located in the System.Text.RegularExpression namespace. So somewhere in my class I use the code below. Now you don't have to do class member variables. You could just do all this code in a function. I however will be doing multiple validations so I don't want to be destroying/creating multiple objects during the life of my class. You have to create your regular expression and then use that expression when creating the RegEx object. private string _expression = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; private Regex _validEmailRegEx = new RegEx( _expression ); Then in my class I define a function that uses the RegEx object to validate an email address bein...

Interesting article on famous programmers

In a bit of a break from my usual posts I read an interesting article on the breakdown of 200 famous programmers . It would appear most have only worked on 1 project and are male. I was surprised to see .45% are transsexual and only 2.7% are women. I know IT/Programming is heavily male but that blew my mind. Also only 1 project? I think a lot of these guys get into a specialty and then don't do anything else for a while. Anyway a bit of an interesting read.