Archive for March, 2008

A penny for your soybean?

Monday, March 24th, 2008

If you know me, or if you’ve read some of my blog posts you probably know I’m a big fan of diesel engines. In particular I’m quite fond of biodiesel. I’ve often preached that while OPEC continues to shaft us on the price of crude oil making gasoline and petroleum based diesel unreasonably expensive, biodiesel should decrease in price as the process used to produce it becomes more advanced and cost effective.

So, you can imagine my dismay when I drove by the local station which sells B99 expecting to see that it’s price continued to hover around $3.30 per gallon, only to see that it was well over $4 per gallon. What gives?! Biodiesel was supposed to be the stable and reliable alternative to petroleum diesel, and here it is trumping the price of petroleum diesel by several cents per gallon.

Well, I forgot one important point. Biodiesel is not just about the process required to produce it, there is also the feedstock required for the raw vegetable oil. Turns out, the price of soybean oil has increased dramatically this season. This in turn has made biodiesel prices mirror petroleum diesel prices.

Now, I suppose this should have been an easily foreseeable problem based on the rules of supply and demand, but somehow I’m still disappointed. This is leaving me with a lot of doubt over what had previously seemed like a rock solid and obvious conclusion of buying diesel vehicles or converting my existing ones to diesel. Where I had thought I’d want to buy an Excursion, I think the wife is going to get her way and we’re going to get an Acura MDX. And where I had thought I’d like to convert my Ford Explorer to some sort of diesel power plant, I’m seriously considering modding my existing motor. I don’t suppose I’ll get the same kind of fuel economy, or performance in either case but at least I can predict that the price of my fuel will steadily rise until I’d rather walk than drive, rather than being at the mercy of a new fuel and a new industry.

I’m actually in no particular hurry to change anything, so I suppose I’ll just wait to see how things pan out, and maybe I’ll end up doing what I’d initially planned. But for now, consider me dejected and sad.

Microsoft’s Tlbimp creates leaky BSTR signatures

Thursday, March 13th, 2008

This one confounded me when I first discovered it, and I’ve recently been reminded about it. For the sake of remembering the details, and hopefully helping someone else out I’m going to document it here.

The problem is this. When you have a COM library that you need to use from a C# app, you import it as a reference. In the background the Microsoft.NET wizards do their magic by running the Tlbimp.exe to generate a managed DLL with all of the objects and interfaces from the COM library. You proceed to use the code that Microsoft so conveniently converted for you fully confident that all is well.

But it’s not. See, suppose your COM library has a method that returns a BSTR via an [out] parameter, or perhaps it defines an interface for a listener your managed code must implement. Suddenly there is the potential for a serious memory leak!

See, a BSTR in unmanaged code aren’t just any normal string. BSTR’s are allocated by the system by calling SysAllocString and subsequently released by calling SysFreeString. This poses a problem for managed code if you aren’t careful. Take the following listener interface for example.

*Interface names and GUID’s changed to protect the innocent

1
2
3
4
5
6
interface IImplementMeListener : IDispatch{
[
id(0x000000C9)
]
HRESULT _stdcall notify([in] TEventType eventType, [in] BSTR data );
};

The Tlbimp.exe generates a managed assembly with the following signature for this same method.

1
2
3
4
5
6
7
[ComImport, TypeLibType((short) 0x10c0), Guid("00000000-0000-0000-0000-000000000000")]
public interface IImplementMeListener
{
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(0xc9)]
    void notify([In, ComAliasName("ExampleLib.TEventType")] TEventType eventType,
        [In, MarshalAs(UnmanagedType.BStr)] string data);
}

Which means when you implement this interface in your managed class, you’ll just have String as the data type for the second parameter. Which might look like this.

1
2
3
4
5
6
class MyManagedListener : ExampleLib.IImplementMeListener{
    public void notify(ExampleLib.TEventType eventType, String data)
    {
        DoSomethingWithData(data);
    }
}

So this is what happens.

1) Your COM library allocates the string to pass into your listener using SysAllocString.

2) Your COM library passes the newly allocated string into your managed app by calling the notify method of your listener.

3) Your managed app does whatever it’s going to do with the string, then returns.

Normally in a fully managed app this would be no problem, when the reference count to the string finally reaches 0, the garbage collector sweeps it up and the memory is reclaimed. However, in this case we have a problem. The COM library allocated the string, and passed it into your managed app, and it’s responsibility for that string ends there. The expectation is that the client will release the BSTR by calling SysFreeString. Clearly we can’t explicitly do that to the managed String type.

So what do we do? We rewrite the part of the assembly that Tlbimp.exe made for us, and adjust our listener implementation slightly.

This is how I did it, though there may be better ways.

1) Use a disassembler to view the code of the Tlbimp.exe generated assembly for your COM library. I used, Lutz’s Reflector.

2) Copy the code for the entire library into a *.cs file, then change just the signature of the method you’re concerned with.

The new signature should look like this.

1
2
3
4
5
6
7
[ComImport, Guid("00000000-0000-0000-0000-000000000000"), TypeLibType((short) 0x10c0)]
public interface IImplementMeListener
{
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(0xc9)]
    void notify([In, ComAliasName("ExampleLib.TEventType")] TEventType eventType,
        [In] IntPtr data);
}

And your implementing class changes to this.

1
2
3
4
5
6
7
8
class MyManagedListener : ExampleLib.IImplementMeListener{
    public void notify(ExampleLib.TEventType eventType, IntPtr data)
    {
        String dataStr = Marshal.PtrToStringBSTR(data);
        DoSomethingWithData(dataStr);
        Marshal.FreeBSTR(data);
    }
}

This is not particularly tricky wizardry. All we’re doing is marshaling the input value from the library as an IntPtr instead of a managed String. This allows us to explicitly release it using the System.Runtime.InteropServices.Marshal.FreeBSTR method, just like the library expects us to do.

Hopefully, this will save you some hastle, and avoid a potentially large memory leak.

Check out my new ride!

Tuesday, March 11th, 2008

Ford GT

No, I didn’t actually buy a super car, but I think that I should be able to. Allow me to elaborate.

I live in a rather affluent part of Southern California and nice (expensive) cars are not uncommon on our roads. I spent just a couple hours recently at Shoreline (some bluffs just over the beach) and watched as every 3rd car that drove by was a Porshe. Mostly boxters and cayman but none the less, where else in this world are porche’s more common than hondas? Here, you have to drive a Ferrari or Lamborghini to even begin to stand out.

It’s worse than that though. I know of no less than 3 Ford GT’s, 2 BMW M5’s, 4-5 Astin Martin Vanquish, 2-3 Ferrari F430’s, countless Bently GT’s and Mercedes AMG’s of all flavor, and last but not least one Maserati quattroporte which I see every day at work. If I recall, when I read a magazine review of the Maserati the author felt comfortable saying that you may never see one of these vehicles in person, and here I am fighting for a parking spot with one.

Now those are just the cars that I’ve observed on the road, god knows what is tucked away in garages and not driven but on the most holy of days to tootle on down to the country club. And here, by my reckoning, is the worst part. Most of the owners of the cars have NO CLUE how to drive, nor will they ever come close to exercising those cars to any where near the potential. The worst offenders are the trophy wives who are driving around in their various Mercedes Benz AMG’s. There isn’t one of them that drives more than 40 MPH at any time for any reason, very much including the freeway. Either that, or they think they’re an Andretti and drive dangerously fast in a straight line, and panic whenever they have to actually change trajectory. Why their sugar daddies thought it was necessary to spend 30% more for the same car, but with a ton more power they’ll never use, I can only guess.

Which brings me to the point of my rant. I know, about time eh?! I think that all manufacturers that make cars built for the enthusiast should have some sort of discount program. Here’s how it would work.

Perhaps only a couple weeks out of each year, you could walk in to your favorite uber car dealership, take a test drive, and chat with the salesperson. Then before you go to sign the paperwork for your shiny new cruise missile you are required to prove your enthusiasm for the car. How you ask? By proving your mettle on a series of driving tests.

Picture this, you get behind the wheel of the $360k car that you’d like to buy, and if you can wring it around the test track in less time than a certain predefined speed, demonstrate competence in controlling the car at, and beyond the limits of traction, and otherwise convince the manufacturer that you’re willing and able to drive the car in the way it was designed, why it’s yours for a mere fraction of the price!

Isn’t that what it’s really about anyway? These cars aren’t built so that only real estate moguls and CEO’s can own and drive them. No! They’re designed to be driven FAST, on a track by a talented and appreciative driver. So why not give your average Joe the ability to get into one of these vehicles, someone who would really cherish the privilege and (hopefully) have some respect for traffic laws and be able to control the vehicle on the street, someone like ME!

It might even cut down on the number of incompetent and unexperienced drivers that get behind the wheel of these monsters.

Just a thought. Manufacturers are you listening? I’ll take delivery of my Ford GT as soon as you realize how good this would be for business. ;-)

Tools for updating an online image gallery

Thursday, March 6th, 2008

Not long ago I shared my bash script for creating thumbnails and manageable sized “big” images for an image gallery, or other image store. Well, today I had to update an existing gallery with a pretty significant number of new images. It occurred to me that I’d like to only convert new images rather than re-converting the whole lot. Then, I’d like to simply upload the new files to my server in some automated way.

So here’s the tools to do it. It assumes you have the same layout for your pictures on your local drive as I do, which is as follows.

  • RootDirectoryForGallery — This contains the full resolution pictures directly from your camera, and the following directories.
    • web — This is a sub directory of RootDirectoryForGallery which contains the “big” web version of your image, and the thumbnails directory described below.
      • thumbnails — This is a sub directory of web, and contains the thumbnail sized versions of the images.

The first step, is to copy your new pictures from the digital camera to your RootDirectoryForGallery. Then, we want to get a list of the files that are new, so we run the following from the RootDirectoryForGallery.

diff . web/ | grep 'Only in \.: d' | awk '{print $4}' | grep jpg >> 3-6-08_update.diff

This creates a file in the RootDirectoryForGallery named “3-6-08_update.diff” which contains a list of only the new pictures, one per line. Now, we want to use our image thumbnail script to convert only the new images to the “big” and thumbnail sized images. I discovered that I needed to make a change to my script in order to do this. Namely in the for loop I needed to enclose the command used to list files in “$()”. The modified script is shown below. Notice the “$($SEARCH)” where there was previously just “$SEARCH”.

#!/bin/bash
 
SEARCH=$1
SIZE=$2
DEST=$3
 
if [ $# -lt 3 ]
then
echo "You must pass three arguments 1) The search string (usually *.jpg) 2) The destination size I.E. 500x374 3) The destination directory"
exit 1
fi
 
for i in $($SEARCH)
do
        echo "Converting $i"
        convert -resize $SIZE $i -resize $SIZE +profile '*' $DEST$i
done

So with our edited script we convert the files.

/opt/imagethumbnail.sh "cat 3-6-08_update.diff" "800x600" "web/"
/opt/imagethumbnail.sh "cat 3-6-08_update.diff" "150×112 "web/_thb_"

Lastly, we want to upload just the changed files to our server. We’ll use rsync for this. The paths, server name, and user name have been changed to protect the innocent. ;-)

rsync -e ssh -av /path/to/local/copy/ yourusername@yourserver.com:/path/to/www/server/copy/

And there you have it, with just a few simple commands you’ve created thumbnails of all of your images, and uploaded just the new ones to your server. Easy!

With great power comes?

Wednesday, March 5th, 2008

I’ve been noticing a trend lately in modern cars. Everything is getting exponentially faster and more powerful, and meanwhile more affordable.

You can go out today and buy a lightweight SUV or sedan from Jeep/Dodge/Chrystler with a 420hp Hemi for around $35k. But it’s not just the “hot rod” cars that are getting big power, a 2008 Honda Civic has no less than 140hp in it’s cheapest form. Not even 10 years ago that amount of power was reserved for the “Si” model. A 2008 Honda Accord v6 has 286hp. The performance of these cars easily exceeds that of the 60’s and 70’s muscle car and is well in excess of what most drivers need, or can use.

Meanwhile, all you need to do is memorize a few rules and convince a government worker that you can drive in a straight line and use your signals when appropriate to get a license. Worse still, you only need to prove it once. Afterward you’ll just get a renewal notice in the mail until your 130th birthday when they start to wonder if you are still alive, and should be driving.  That’s well past the age where “oops I mistook the gas for the brake” is at an acceptable margin of error. There is no requirement to handle power induced slides, know how to react when you start to go sideways in a turn, nothing! But 16 year old Johnny, if he’s fortunate, or wealthy can be behind the wheel of a relative super car.

Where’s the responsibility? You should have some vague inclination of how to drive, and control yourself if you’re going to be able to drive some of todays cars on the street with others. Now don’t get me wrong, my first car was a 1967 Thunderbird which made 340hp. However, it almost NEVER ran well enough to generate that power and deliver it to the tires. Furthermore, I had to work hard on that car to even keep it running, which gave me a good sense of it’s inner workings, and exactly what I was dealing with. I was also taught a few important lessons about speed and control before I was set out on my own with it or any other vehicle.

The idea of just anyone with an (easily acquired) drivers license behind the wheel of some of these vehicles is frankly quite terrifying to me, particularly in the face of the sort of antics I see on a daily basis even from “mature” drivers. Mind you I wasn’t alive during the “hot rod” era of the 60’s and 70’s where I’m sure a similar situation existed, even in the absence of anti lock disk brake systems, air bags, and chassis/traction controls, so maybe this is just history repeating itself.