This is how I should be creating
February 26th, 2010 at 5:41 pm
VONNEGUT
I said in Slapstick that she was the person I wrote for—that every successful creative person creates with an audience of one in mind. That’s the secret of artistic unity. Anybody can achieve it, if he or she will make something with only one person in mind. I didn’t realize that she was the person I wrote for until after she died.
Via The Paris Review
Writing an Email client is Hard
February 3rd, 2010 at 8:00 am
Here’s a great Email post about how to write a proper Email client. Certain classes of software are just fundamentally difficult to do a complete job on, things you wouldn’t necessarily expect like a Textbox control or a build system. An Email client is one of these categories – even today if you discard legacy protocols and standards, you’ve still got a Mount Everest to climb. Here’s the epic mail, written by John C. Welch. Which is a shame, because at least on Windows and Mac, most of the desktop mail clients are sorely lacking in my opinion, with little hope of truly improving.
Some Quotes:
Stop thinking about IMAP as if it were some cohesive standard with a consistent implementation. It is not. It’s a bloody mess that manages to work in spite of its implementations, and Google’s IMAP implementation is one of the worst I’ve seen in the last 12 years I’ve been working with IMAP servers.
Regardless of who this project is aimed at, people using it are going to get email from “icky” sources, like Outlook users who have no control over their email format. (You think I’m kidding about this. I’m not. You should see how bizarre an overly locked-down windows shop can be.) They won’t have the option to get the sender to change format, so in addition to “normal” HTML, you should start thinking about building in TNEF decoding. In fact, if you do this well, it would be a monster point in the application’s favor, since that’s a major problem for people in the real world.
Power User != Programmer, Power User != Programmer, Power User !=
Programmer, Power User != Programmer, Power User != Programmer, Power User
!= Programmer, Power User != Programmer. There, point made.
HTML editor. Yes, I know text, blah, blah. Again, as the non-programmer power user, HTML is a reality, and it needs to be properly managed. There’s no reason to have the entire spec, but things like fonts, bold/italic/underline, alignment, real lists, and ‘real’ indents/tabs are not the work of satan. And that includes images. Welcome to the modern world, pretending it’s not there helps no one.
Using Blend Sample data at Design time and real data at Runtime
February 2nd, 2010 at 8:00 am
Sample Data is Awesome
Here’s a clever trick that I found out the other day – one of Blend 3’s best features is Sample Data, which allows you to generate basic data structures in Blend, create sample arrays of them, and mock up UIs so that your DataTemplates actually show something useful. This is a huge improvement over Blend 2 and VS, where important parts of your UI would basically be invisible until you actually could build the project and populate it with real data.
Keep Sample Data all the time
One thing I always struggled with though is, that you always had to switch the binding back and forth between the sample data and the real data – even though Blend has a “Only enable sample data at design time” feature, you could still only bind the data to one place. But there’s a clever hack to get around this, you can see me applying it in this Github commit.
Basically, say you had a Listbox:
<!– Sample Version
<ListBox ItemsSource="{Binding Path=Data, Source={StaticResource SampleData}}" />
–>
<listbox ItemsSource="{Binding Path=TheData}" />
</grid>
Here’s the trick – Blend doesn’t let you redirect the binding at Design time, but it will let you redirect the context. So all you have to do, is group the ItemsControl in a meaningless container, then rig the DataContext; d:DataContext is what Blend will use at Design time and will be ignored by WPF / Silverlight.
<border DataContext="{Binding Path=TheData}" d:DataContext="{Binding Path=Data, Source={StaticResource SampleData}">
<listbox ItemsSource="{Binding}" />
</border>
</grid>
Et, Voila!
Now, whenever we edit in Blend we see our sample data so we can edit the UI, then when we run it, we’ll use the live data instead.
HTML5’s Video tag already works in IE
February 1st, 2010 at 9:53 am
Just add these two lines to the top of your page:
<script src="http://visitmix.com/dlr/gestaltmedia.js" type="text/javascript"></script>
Of course, this is a hack workaround that uses Silverlight to emulate the Video object, but going from “doesn’t work at all” to “make users install a plugin, once” is a pretty good deal. Here’s the original post describing it.
You can get the crash reports from Windows Error Reporting (Watson)
January 11th, 2010 at 11:43 am
One of the things I see pop up on Stack Overflow all the time is, people wanting to hide or suppress the Windows Error Reporting dialog – the feature of Windows where crash reports are sent to Microsoft when your app crashes. Developers say, “MS doesn’t care about my app, what’s the point?” If they’re not so bright, they’ll write a try/catch block around their main() and hide all errors, then they’ll never find out why their apps crash in the field. Smarter developers will essentially write their own hacked-up version of WER with varying degrees of success. However, there’s a 3rd option that is superior:
Register with Microsoft, and get the WER Reports
Microsoft doesn’t just trash your reports, we will automatically aggregate them into distinct crashes (called ‘buckets’) and show you a ‘popularity contest’ of crashes – you can find out a ton of detailed information about what is happening in the field. Here’s the MSDN article detailing how to sign up. It takes a bit of leg work, but if you’re writing any sort of production-quality application, it’s 100% worth it.
But here’s what to do if you don’t believe me
Alright, so you have some super-good reason as to why you really don’t want this dialog to show up. Here’s what works on Win2k and higher (Vista has an API to do this explicitly though):
{
TerminateProcess(GetCurrentProcess(),
WhyGodWhy->ExceptionRecord->ExceptionCode);
// Never gonna get here
return EXCEPTION_EXECUTE_HANDLER;
}
int main(int, char**)
{
SetUnhandledExceptionFilter(KillSelfOnUnhandledException)
}
Expression Blend Glitches in VMWare / Parallels?
January 10th, 2010 at 9:37 pm
WPF hardware acceleration dies under VMWare
Just as a note since this comes up a lot from designers who use Macs and are getting into Silverlight / Expression Blend – if you are trying to use VMWare Fusion or Parallels and are coming up with weird graphics glitches or black screens while trying to use Blend, it is because WPF doesn’t get along well with these products’ graphics virtualization (I imagine because both products are focused on gaming / full-screen graphics).
WPF can be forced to run under Software-only rendering however, which clears all of these glitches up (and for Blend, doesn’t seem to have a huge impact on its usability). To do this, check out this MSDN page – you’ll need to open up the Registry Editor (regedit.exe), find the HKEY_CURRENT_USER\SOFTWARE\Microsoft key, create a new folder called Avalon.Graphics, then in that folder, create a new DWORD value called DisableHWAcceleration and set its value to 1. Close Blend and restart it, and everything should work now
A C# 3.5 pattern – the Accept Block pattern
January 10th, 2010 at 9:25 pm
A coworker pointed me towards this blog post as a useful trick to make code read cleaner. I’ve been using this trick in C# for quite some time, as it’s a very fundamental pattern in Ruby programming. In my head I call this the “Accept Block” pattern, but I’m fairly certain its just a syntactic version of an existing pattern (the Strategy pattern?). In some sense, it’s like being able to write your own version of the “using” or “lock” statements.
Here’s the old imperative way you might write some code:
{
SetupTheDatabase();
bool ShouldExit = SomeFunctionThatNeedsADatabase();
TeardownTheDatabase();
if (ShouldExit)
return;
DoOtherStuff();
}
And here’s how you’d rewrite it using the pattern:
{
SetupTheDatabase();
try {
block()
} finally {
TeardownTheDatabase(); // Note: Make sure this doesn’t throw!
}
}
void Foo()
{
bool ShouldExit;
WithEnsureDatabaseIsSetup(() => {
ShouldExit = SomeFunctionThatNeedsADatabase();
if (ShouldExit)
return;
});
DoOtherStuff();
}
In my opinion, the 2nd version is better to read, and more importantly, allows you to encapsulate the “setup/teardown” pattern – this way, this block of code is consistent, removes copy-paste errors from writing the same thing over and over again, and is easily updated if you want to do additional work when setting up the database.
But there’s a gotcha!
There’s a bug in the above code though that’s quite subtle, and that has bitten me a few times – the syntax () => { } is defining a closure, an anonymous function who can use variables bound inside its lexical environment (to understand the difference, take a look at that ShouldExit variable – even though it’s not a parameter of the function, we were still able to use it). When you call return, in the original example we will exit Foo() – in the new version, we will exit the closure and still execute DoOtherStuff()! Here’s how we should fix it:
{
bool ShouldExit;
WithEnsureDatabaseIsSetup(() => {
ShouldExit = SomeFunctionThatNeedsADatabase();
});
if (ShouldExit)
return; // Now we’ll return properly
DoOtherStuff();
}
Ruby doesn’t have this problem
This is a fundamental pattern in Ruby and built into the language and the runtime, so much so that it has special syntax to make it happen. However in Ruby a “block” is a slightly different syntactic structure than a function, so return will do what you expect:
an_array.each { |item| # This is a block with 1 param
print item
return if item > 2 # Actually returns from Foo, not from block
}
end
How to move Windows to another computer
December 4th, 2009 at 9:00 am
Moving a Linux install is easy
One of the awesome things about Linux was always, that it’s so incredibly easy to move your stuff to another machine – just stick the drive in another computer and it boots right up; if you do this directly with Windows, it will probably blow up in your face. Installing everything from scratch takes forever and generally sucks.
Windows can do this too, but with some more work
So, if you’re a Dell or a Toshiba or a Lenovo, here’s how you operate – you have a lot of different machines you’re trying to sell, but you don’t want to maintain an OS image for every one. OEMs didn’t like this, so they asked MS to help out, and Microsoft gave them Sysprep.
One of the main features of Sysprep is called “generalize”, where we’ll keep the installed stuff, but “forget” everything about the specific hardware of the machine. The next boot, Windows will go through the same process as when it was first installed, and reinstall everything.
This is where you come in, intrepid reader
So basically, you need to download the Windows Automated Installation Kit for Win7. Update: Apparently this tool comes in-box, no download needed! This gives you Sysprep (and ImageX, which is an interesting tool itself). Then, run the following magic command:
Windows shuts down, and then you move the drive to another computer.
Moving a VM to a Native Boot-to-VHD
The same trick is also how you can take a VHD image of a machine and run it using Windows’s “Native VHD boot” feature. You know, where you can take a VHD and just run it, on your machine, full speed. Awesome. While it’s a VM, Sysprep it, then boot from it – you won’t have any of the STOP 0×7F problems that you might have if you ran it natively, and you just saved yourself a ton of time versus waiting for Windows to install.
Running Hyper-V on Windows 7 Client
December 3rd, 2009 at 9:00 am
Run Hyper-V on Windows 7? Unpossible!
Ok, so I lured everyone in with a provocative title, and I can’t exactly deliver – there is no way as far as I know to directly run Hyper-V on any client version of Windows 7. But there’s an important bit of software that has an obscure name that can really help you out.
The caveat is, you need a machine that’s free and supports hardware virtualization (i.e. AMD-V or VT-x). Not all machines support it, and a lot of them need some BIOS fiddling to make it work properly.
Hyper-V Server 2008 R2 costs exactly zero dollars
Nobody knows about this, and I don’t know why it’s not more popular – Microsoft gives away the Server Core Hyper-V SKU. For free. No dollars. Go over here and download it. Set this up on a machine and it should drop you at a command prompt – that’s all there is to Server Core, just a cmd prompt; that’s all you need for Hyper-V though.
Remote Server Administration Tools for Windows 7
A lot of people think that you need to have Windows Server installed to be able to administer other servers – otherwise you don’t have the MMC snap-ins, so people resort to TSing into their boxes to administer them. Ever since Vista, we’ve made a package called the Remote Server Administration Tools (RSAT), which brings all of the snap-ins like the Active Directory admin page, the DNS page, everything that’s on Server – only on Vista / Win7.
This won’t magically make your Windows 7 box be able to be a Domain Controller though, you’ll only be able to connect to other machines. However, this includes all of the Hyper-V client components – you’ll be able to view the console, manage/add machines, etc. Here’s the only trick though, the installer is kind of goofy – installing the package only adds the entry in the Add Optional Features list. Then, you have to actually choose what to install.
Combine these two, and you’ve got Hyper-V on Win7 for free
Just like the heading says, if you combine these two, you’ve got Hyper-V for free. Yahtzee! Combine this with disk2vhd, and you can get rid of a bunch of test machines and move them to VMs. Move VHDs using the SMB admin shares, like \\mycoolbox\C$\Users\Public\Documents\Hyper-V Disks
Convert a .NET 2.0 DLL to 4.0 (VS2010) without source
December 3rd, 2009 at 12:36 am
Converting .NET DLLs to 4.0 by hand is too much work
One of the blockers for upgrading a project to .NET 4.0 is that your old .NET 2.0/3.0/3.5 DLLs will have some difficulty running in .NET 4.0. I’m far too lazy to track down all the 3rd party DLLs like Moq or log4net, download the source, switch the project to 4.0, then recompile the DLL.
Hackery makes life easier
Instead of doing this, for a lot of DLLs, you can get away with roundtripping the DLL using ildasm/ilasm; the only tricky part is changing the assembly references so that they point to .NET 4.0 DLLs instead of the old and busted 2.0 System.* assemblies.
The good news is, I’ve written an IronRuby (or MRI, or JRuby, etc) script to handle this automagically. Here’s how to use it:
How to use:
- Download IronRuby from CodePlex (You can use the .NET 4.0 release too, doesn’t matter), and copy it to
C:\IronRuby - Download dotnet4ify_dll.rb from my website
- Launch the VS Command Prompt (2010) – don’t launch the 2008 one by accident!
set path=%path%;C:\ironruby\binmkdir v4dllsir dotnet4ify_dll.rb C:\path\to\an\old\assembly.dll .\v4dlls\assembly.dll
Some caveats
- While I’ve tested this on some pretty complex DLLs (DotNetOpenAuth, ParallelFramework_3_5.dll), it’s definitely in the “Works on My Machine” class of software; in particular, C++/CLI DLLs will probably not work. Embedded resources do still get embedded in the new binary
- .NET Assemblies get upset if you rename them, so you can’t do something like “ir dotnet4ify_dll.rb foo.dll foo_v4.dll” – just put all your v4 assemblies in a separate directory

Worked for me, but I make no guarantees it won’t replace DLLs with a lolcat