I would recommend against using "GetStdHandle". When stdout is redirected (like in the "do" part of a for loop) the handle returned is the redirected handle. "CreateFile" can give a direct handle.
I wrote some tools myself a while ago, I haven't written anything for a while though.
Have a look at my "wcde" tool, it might give you some ideas of things you might want to implement. I'm not really happy with the source and it's an abandoned project now, but anyway it might give you some ideas.http://judago.webs.com/downloads.htm#590682301