Fractal Life

October 10, 2008

Posting code in Wordpress

Filed under: Blogging — wordpress @ 14:42

Like I said in the previous post, posting code inside Wordpress is a huge pain.

But I’ve found some tools that make it somewhat easier.  Still stinks, just not as badly.

First thing you’ll want is the SyntaxHighlighter set of Javascript files from Google.  Get them here as a RAR file.  Under your blog’s directory, create a scripts directory and copy the .js files into it.

Next, get the SyntaxHighlighter plugin for Wordpress, which is found here.  Unzip this, and copy the top-level folder and all child folders & files to your wp-content\plugins directory (you should now have a new folder in there named SyntaxHighlighter).  Be aware that some of the files are text, some are binary, so make sure your FTP tool can tell the difference.

You will need to modify your header.php file from your theme.  If you’re using the classic theme like me, this will be found at: wp-content\themes\classic. Your hosting provider may give you a way to edit this file online - mine does and it didn’t work.  I had to download the file and make changes locally before re-uploading it.  Insert the needed Script tags after the meta tag, but before the style tag.  For the languages I will be writing, I added this block:

</p>
<p><script src="/blog/scripts/shCore.js" type="text/javascript"></script><br />
   <script src="/blog/scripts/shBrushCSharp.js" type="text/javascript"></script><br />
   <script src="/blog/scripts/shBrushSql.js" type="text/javascript"></script><br />
   <script src="/blog/scripts/shBrushVb.js" type="text/javascript"></script><br />
   <script src="/blog/scripts/shBrushXml.js" type="text/javascript"></script><br />

And that should do it. I occasionally see some problems with leading spaces being eaten, and still haven’t found a good way to prevent/fix that.

September 9, 2008

MMC 3.0 Concepts

Filed under: 64-bit — wordpress @ 15:10

I’ve been working on a MMC v3.0 snap-in for our Oak City Software project to manage configuration settings (licensing, etc) and was having some problems getting it to respond to the standard action verbs.

These are the items you see over in the Actions pane (select Customize View, then check the Action Pane checkbox) that represent standard things you can do with/to a snapin. Stuff like Refresh, Cut, Paste, Properties.

I was trying to implement the Refresh verb and it just wasn’t working.

What it turns out is that there is a disconnect in the object hierarchy between your class derived from ScopeNode and your class that derives from UserControl and implements IFormViewControl (if you’re doing a WinForm view like I was). At runtime, there is one instance of your ScopeNode, but there could be multiple instances of your FormViewControl class because the MMC environment is MDI, not SDI. So the node has no way of knowing which instance of your FormViewControl to call to tell to refresh itself. And since MMC is the one creating the instance (you tell it the Type, and it creates it for you), there’s no way for you to stick a reference to your control into the node, or other tricky ways.

What you have to do is implement the Observer pattern so that your FormViewControl subscribes to an event notificaton from the Node, and thus would know when it has to refresh itself (or cut, paste, show the properties, etc).

Here’s a code sample. First, your Node class:

 // Represents the license node in the MMC tree
public class OcsLicenseNode
         : ScopeNode
{
   // FormViewControl instances subscribe to this to know when to refresh themselves.
   public event EventHandler RefreshEvent;

   // Constructor
   public OcsLicenseNode()
   {
      EnabledStandardVerbs = StandardVerbs.Refresh;
      FormViewDescription rootView = new FormViewDescription(typeof(OcsRootNodeControl));
      rootView.DisplayName = "OCS License"; // TODO: get from resource file
      rootView.LanguageIndependentName = "OCS License";
      rootView.ViewType = typeof(OcsRootNodeFormView);
      rootView.ControlType = typeof(OcsLicenseNodeControl);
   }

   // Called when the user selects the Refresh action
   protected override void OnRefresh(AsyncStatus status)
   {
      base.OnRefresh(status);
      // Notify observers
      if (RefreshEvent != null)
         RefreshEvent(this, null);
   }
}

Now the FormViewControl implementation:

 // The usercontrol that is shown when the user selects the License node in the MMC tree
public partial class OcsLicenseNodeControl
   : UserControl, IFormViewControl
{
   private FormView _view;

   // Constructor
   public OcsLicenseNodeControl()
   {
      InitializeComponent();
   }

   // Forces the control to invalidate its client area and immediately redraw itself and any child controls.
   public override void Refresh()
   {
      base.Refresh();

      // See if they already have a license, and if so, display it.
      //
      // {snip}
      //
   }

   // Initializes the specified view.
   public void Initialize(FormView view)
   {
      Dock = DockStyle.Fill;
      _view = view;
   }
}

For production code, you’d use a try-catch block in the spot where we’re invoking the delegate — if one of the event implementations causes an exception to be thrown, then it would prevent any of the other observers in the delegate linked-list from being called.

Sidenote: posting code inside Wordpress is amazingly annoying. Enough so that I might need to consider changing my blog software after only using it for a few months. Getting just this little bit required me writing a VS.NET add-in to convert spaces to non-breaking space HTML entities, replace newlines with break tags, and several other unobvious tweaks. Not to mention Wordpress itself behaving erratically.

May 20, 2008

Implications of using Vista 64-bit when writing 32-bit code

Filed under: 64-bit — wordpress @ 19:48

I run Vista 64-bit Enterprise (under Bootcamp on my Mac Book Pro), and ran into an interesting side-effect yesterday.  At least it was interesting to me. :)

I want to store some values in the Windows Registry under the LocalMachine hive (a global setting, to get around a chicken-and-the-egg configuration problem I had).  My build target within Visual Studio 2008 was ‘Any CPU’, which means in effect, it creates a 32-bit executable.

The code worked fine - I could both write to and read from the registry.

But when I ran Regedit to go look at the values, I couldn’t find them.  I was looking at the right spot — HKEY_LOCAL_MACHINE\SOFTWARE\Oak City Software but my value wasn’t there!  I knew the write suceeded because I could read the value back again — it’s just that it wasn’t written where I thought it would go.  Walking off to get a refill on Diet Dr. Pepper while a global search in the registry ran, I returned to find that the values had been stored under HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Oak City Software

It turns out that when running 32-bit code on a 64-bit OS, Microsoft transparently redirects your registry calls to this special Wow6432 node in the registry.

OK, time for an experiment.  I changed the build target from ‘Any CPU’ to ‘x64′, thus creating a 64-bit executable.  Running this resulted in my values being written to the place I expected within the registry.

I’m tempted to call this a leaky abstraction, but on the other hand, it is transparent to my code, and any support burden would be internal — I don’t expect my endusers to be spelunking in the registry.  But at the same time I’m curious as to why this was done.  I suspect that the registry doesn’t store values with as strong a datatyping as we think — someone trying to read a 64-bit value when only 32-bits was actually written might be a problem.

May 5, 2008

Q & A Session

Filed under: Vagabond — wordpress @ 18:37
  • Q: Why are you doing this?
  • A: We’ve already covered this. I’m either nuts, or inspired. Take your pick.
  • Q: Can you fit everything in your SUV that you might need for 3 years?
  • A: It’s going to be tight. I (briefly) thought about getting an RV, but the depreciation on them follows a curve more like that of a car than a home.

    This chart shows buying a $65k ($80k MSRP) RV, which is pretty run-of-the-mill. At the end of 13 years, your home is now worth 2/3 less than when you bought it. The appliances (the design of which is specific to a RV) are worn out, the chassis has 150,000 miles or more on it, and to buy a replacement costs even more than this one, thanks to the wonders of inflation.

    I’m developing multiple shopping lists for when I move to a new town. I expect, as much as I dislike the company, I’ll be doing some shopping at Wal-Mart. I’ve always tried to avoid joining the disposable society we’ve become, but with this change, it looks like I’ll have no choice but to buy the cheapest stuff available, and throw away what I can’t give away when I’m done with a contract.

  • Q: What are you going to do with your house?
  • A: I’m going to rent it out. If you know someone who wants to move to Raleigh, and be near WakeMed North Healthplex, it’s a great location, only about a mile from I-540, and so excellent for commuting to RTP.
  • May 3, 2008

    What is Chip up to?

    Filed under: Career, Vagabond — wordpress @ 18:43

    I’m leaving my job and becoming a vagabond.

    This may be the dumbest thing I’ve ever done, but I really want to leave the Raleigh area and see more of the country. The Grand Scheme is to take contracts of six to nine month duration, and just go where the work is. It’ll be me and Hopper the cat, roaming the backroads of the USA in a small SUV.

    Does it make sense to give up a stable job, working with really smart people at a profitable firm that is making a real difference in people’s lives? Nope. None at all. But I want to do it anyway.

    May 2, 2008

    First Entry

    Filed under: Uncategorized — wordpress @ 13:46

    This is my new home on the internet, after hosting for years at my RoadRunner personal page.

    I’m doing a mass posting of the stuff I’ve written earlier, but before I had WordPress set up.

    Powered by WordPress