Tuesday, July 13, 2004

Obscure MFC Gadgets



MFC Internals: Inside the Microsoft(c) Foundation Class ArchitectureLong ago, when I began developing shrink-wrapped software in MFC, I wish I'd had a few watch-outs pointed out to me. They would have saved me time, energy and more than a few hair follicles.

CMap Hash Table Size



CMap is a member of MFC's collection classes. It is a classic map in STL parlance; or "associative array" in the computer science world. One of the watch-outs for CMap is that the starting hash-table size is quite small (maybe 15, if I remember correctly). In any event, if you're expecting to load thousands of key/value pairs into the map, you could expect really crappy performance or you could utilize the following method:


void InitHashTable( UINT hashSize, BOOL bAllocNow = TRUE ); // MSDN


CMapStringTo... and Case-Sensitivity



CMapStringToString and CMapStringToPtr are two of the pre-built classes that map string-keys to other strings or VOID pointers, respectively. Be careful when mixing case with the keys. I've frequently forgotten that the keys are case-sensitive and then spent awkward moments realizing my mistake.


CMapStringToString mapNames;
//
strShortName = "Doug";
strCommonName = "Doug Ross";
mapNames.SetAt(strShortName, strCommonName);
//
// This won't work!
//
strShortName = "doug";
mapNames.Lookup(strShortName, strCommonName);
//


So remember to lower- or upper-case your keys as needed when looking up values or setting new values.

CArray Sizing



CArray is a collection of, well, arrays. Here's my watch-out: for applications that need to add a bunch of elements to the the array (say, when you're collecting log messages or building a list of items to write to disk), use the second argument of SetSize.

Ah, you may not have been aware that SetSize had a second argument. It does, and it is called the "grow-by" value. That is, if you frequently add large numbers of items to your array, it would be wise to jack up the "grow-by" value to a reasonable sum based upon the typical number of items added.

By doing so, you'll reduce the need to re-allocate and re-copy large numbers of array items. If you don't set this appropriately, you could see bizarre delays as your array sizes grow. And then you could tell your customers that they need the "grow-by" patch.

Other info



And for a great collection of classes related to Win32 non-GUI tasks, I refer you to Sam Blackburn's Windows Foundation Classes, one of the finest free class libraries every assembled for Win32 development. They are the classes that Microsoft forgot and are, in general, quite helpful. For example: classes to manage ACL's and ACE's; a base-64 class, various socket (talker and listener) classes, XML parsing, mixer control, service creation and service-control management, and other useful tools.

Windows Foundation Classes

No comments:

Post a Comment