code

.NET: Robust Comparison of Floating-point Values

A clever technique for the robust comparison of floating-point values -- reinterpreting the bits as sign-magnitude integers -- using purely verifiable, managed code.

Inspiration comes from Bruce Dawson (by way of Kim Gräsman) and Jeroen Frijters.

Some unit tests are included, but swim at your own risk, ok?

2004.09.29
.NET: Jitsu.Collections.Specialized.ByteVector

Strongly-typed collection class for System.Byte -- manages storage as a byte[] internally, to avoid both boxing and excess heap allocations.  Use this class as a template for your own custom collection types.

2004.05.06
.NET: ColorConsole class

A p/invoke wrapper for SetConsoleTextAttribute -- for writing colored text to the console.

2004.02.22
.NET: AsyncStreamPump class

The programming model for asynchronous I/O is an alien beast. You call BeginRead, specifying a callback function to be invoked asynchronously when the read operation completes (or the stream is closed). But you're not given the actual data -- for that, you have to call back into the stream's EndRead method, and then reestablish the async read by calling BeginRead, again. It's really quite a chore, because you have to have cached that Stream object somewhere... Uncle!

Doesn't .NET have a nice, tidy event model? Yes it does. Can't we just subscribe to receive the data as it comes in, without writing two pages of boilerplate code juggling streams, buffers, and IAsyncResult references? Yes we can.

Submitted for your approval: ASyncStreamPump, my attempt at a reusable solution for this problem...  useful for large file copies, downloads, uploads -- can even be used to proxy a network connection, with just a few lines of client code!

2004.02.17
.NET: RegexMarshalByRefWrapper class

Regular expressions created with RegexOptions.Compiled will leak, unless they're housed in a secondary AppDomain that can be unloaded -- you'll need this wrapper class, or something like it, to do that.

A similar technique can be used with System.CodeDom.Compiler types, or anything that uses Reflection.Emit under the hood.

(from the beforetime)
Win32: HiResTimer class

It's not rocket science, but it's amazing how often folks get this wrong (also available in C#).

(from the beforetime)
.NET: HiResTimer class

It's not rocket science, but it's amazing how often folks get this wrong (also available in unmanaged C++).

(from the beforetime)
DCOM: PingRemoteHost function

Use this helper function to "ping" a remote DCOM server, before calling CoCreateInstanceEx -- thus greatly mitigating the DCOM timeout problem (at least for the initial activation request).

Update -- Windows Server 2003 and Windows XP SP2 don't allow unauthenticated DCOM activation requests, by default (if at all).  I'm removing support for bUnAuthenticated=true.

(from the beforetime)
Win32: ModelessDialogHook class

Encapsulating reusable dialogs as components (either in DLLs, or in COM objects) is a great idea -- much better than trying to reuse a .rc file -- but things get tricky if you like your dialogs modeless...

(from the beforetime)
ATL3: CSimpleVector template

The ATL3 array template (CSimpleArray) is buggy and leaky -- don't use it (note: ATL7 finally repairs the bugs in CSimpleArray, and also offers a newer template, CAtlArray).

(from the beforetime)
ATL3: CSimpleList template

As seen in Attila (note: ATL7 offers similar functionality with their new CAtlList template).

(from the beforetime)
ATL3: CComTSTR class

The ATL3 string conversion macros are ugly (at best) or they pose terrible security holes (at worst) -- use this heap-based wrapper class instead (note: ATL7 offers a similar solution with its new CW2A et. al. classes).

(from the beforetime)

Updated: Mon, 31 Dec 2007 03:12:00 GMT