Getting a Handle on it all …

Window Handles that is.

WPF Window Handles.

It’s not hard to get the HWND for a WPF Window. There’s a class that makes it easy to get access to the Win32 based HWND that hosts a WPF Window, called WindowInteropHelper.

I saw this post by Brandon and it made me think, Extension Methods to the rescue!

His solution, although neat, means that there’s an extra object that needs to be created just so that a properties value could be delay loaded. Window handles generally should be obtained and discarded anyway (as they can change), so I thought I’d go with this method to add the Handle property to every WPF Window, without needing to add either a custom class and derive everything from that, or manually add it to every class.

public static class WindowExtension
{
    public static IntPtr GetHandle(this Window w)
    {
        return new WindowInteropHelper(w).Handle;
    }
}

To use:

this.GetHandle()

Done. I don’t mind it being a method in this case as it’s not necessarily a fast operation, and it’s read-only.

What technique would you use?

I need a Jargon-Free Silverlight description/blurb

I want to be able to describe what Silverlight is to a non-tech person. The description, which shouldn’t be more than a few sentences, must be jargon and marketing jibberish free. It cannot use acronyms or phrases such as "Rich Internet Application." Those are meaningless. And no, it should not include the phrase, "lights up the web." :)

The best way I can summarize it to people:

Do you know what Flash Is? Eh, It’s like that, from Microsoft though.

If they answer no, then it boils down to suggesting an example of Flash in use (think annoying advertisements you can click on!), or saying it will just make their web experience better somehow.

Any suggestions?

Dirt Simple 301 Redirect on Shared hosting and the 404 trick.

I’m using a shared hosting account for my web site. I have no control over the file types (extensions) that are handled by ASP.NET — only the standard file extensions (ASPX, ASMX, etc.) are supported. By request, I was asked to make my old RSS and Atom XML file feeds up to date with my new blog posts. During the transitions I’ve made, I may have lost some subscribers as I hadn’t kept them up to date as much as I would have liked.

So, with a wave of the magic ASP.NET wand and a little 404 magic, the two XML files are now serving up content from my new WordPress installation. On my web host, I can modify what pages handle 404 web server errors (file not found). I changed the setting to point to a new ASPX file, appropriately named, 404.aspx. Here’s what it contains:

<%@ Page Language="C#" %>
<%
    string qstr = HttpUtility.UrlDecode(Request.QueryString.ToString());

    if (qstr.Contains("index.xml") || qstr.Contains("atom.xml"))
    {
        string movedTo = "http://feeds.feedburner.com/wiredprairie";
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", movedTo);
        Response.End();

        return;
    }

    Response.Status = "404 Not Found";        
    Response.End();

    return;
%>

I grab the query string which minimally contains the requested Url, and if it’s either atom.xml or index.xml, I redirect to the FeedBurner location for the RSS feed. If it wasn’t one of those files, I just return a 404 error and quit (as I don’t have a custom 404 page developed).

Of course, with a bit more robust coding, this could handle a wider variety of cases and redirect to far more web pages. Or, you can just point to my RSS feed. :)

What does it take to make a rock star software developer?

From ReadWriteWeb, “Top 10 Traits of a Rockstar Software Engineer.”

See the full post for the details of each point.

  1. Loves To Code
  2. Gets Things Done
  3. Continuously Refactors Code
  4. Uses Design Patterns
  5. Writes Tests
  6. Leverages Existing Code
  7. Focuses on Usability
  8. Writes Maintainable Code
  9. Can Code in Any Language
  10. Knows Basic Computer Science

What do you think about this list — does this list represent a rock star software engineer? I personally think the list is too slanted on “Agile” practices. What works for a web application software provider may not work well for an ISV or an enterprise developer in all cases.

I would have expected some mention of one of the classic books of software development to be mentioned, Code Complete: A Practical Handbook of Software Construction by Steve McConnell.

Maybe the biggest issue I have with the list is that even if I checked all of these off while interviewing someone, I don’t know that I’d hire them. There are a few things I’d add for a developer to be a rock star. The following are a few ideas.

  • Certainly, I look for “passion” or “energy” when interviewing. You can love to code – but will you love coding what I need you to code? Will you believe in the project or application? Will it excite you?
  • Do you work well on a team? Individuality is important, but being a great peer is an important trait to have. Maybe the best way I could think about this is: even rock stars need a band (or at least a supporting crew). Having a good team behind the “rockstar” is just as important as having the rockstar … actually, maybe more important! So, don’t leave the band out of the rock star.
  • Do you understand the platform you’ll be working on? How well can you learn it? It’s not about a programming language or knowing basic computer science … if you don’t know how the system works (ticks), are you going to be as effective as the engineer who does (I doubt it).
  • Can you communicate well — in whatever form is necessary? It’s not about the language so much as your ability to communicate ideas, problems, solutions, etc. well to others. I’m not suggesting you need to do a presentation to a Board of Directors or 500 people at the next conference … just to your coworkers, one on one, etc.

What would you add?

SMTP Component for .NET Recommendation

I’ve been relying on the MailBee.NET SMTP Component from AfterLogic for several months now and just wanted to recommend it to anyone needing a SMTP component for your .NET development. It’s inexpensive and easy to use:

Pop3 pop = new Pop3();
try
{
    pop.Connect(emailPop3Server, 110);
    if (_quitting) { return; }

    pop.Login(emailAccountName, emailPassword, AuthenticationMethods.Auto);
    if (_quitting) { return; }

    MailMessageCollection msgs = pop.DownloadEntireMessages();
    foreach (MailMessage m in msgs)
    {
        try
        {
            if (m.HasAttachments)
            {
                foreach (Attachment attach in m.Attachments)
                {
                    if (attach.ContentType == "image/jpeg")
                    {
                        using (MemoryStream memStream = new MemoryStream(attach.GetData()))
                        {
                            Image img = Image.FromStream(memStream);

                            if (_quitting) { return; }

                            using (MemoryStream compressedJPG = BuildJPGWithCompressionSetting(img, 60))
                            {
                                // the filename doesn't need to be meaningful, and would be better if it weren't to prevent conflicts!
                                string newFileName = string.Format("{0}.jpg", Guid.NewGuid());
                                Upload(compressedJPG, ftpSite + newFileName, ftpAccountName, ftpPassword);
                            }
                        }
                    }
                    if (attach.ContentType == "multipart/related")
                    {
                        //MimePart mp = attach.AsMimePart;
                        //MimePartCollection mpc = mp.GetAllParts();
                    }
                } // thru each attachment
            } // has attachement?
        }
        finally
        {
            m.Dispose();
        }
    }   // loop thru all the messages
}
finally
{
    if (pop != null)
    {
        pop.DeleteMessages();
        pop.Disconnect();
        pop = null;
    }
}

The above code is used in a Windows Service I’ve written to automatically connect to a specific e-mail address, download the messages, and automatically upload the attached images to an FTP Server (I’ve removed some of the security checks that my code does).

You can see though in the code how easy it is to use the component. It takes the pain out of doing SMTP development and handles a wide variety of cases that might exist (there are multiple techniques for attaching images for example).

I purchased the Single Developer License at $69 — which is a license for unlimited use of the component by me.

They also have a set of components for IMAP, POP3 and security available individually or as a package deal (although it wasn’t clear the package was necessarily as good a price).

AfterLogic has a 30 day trial available for all of their components.

Recommended.