CoCreateInstance and Process Id: Working around COM limitations


CoCreateInstance is a workhorse that makes COM so much more fun and usable. Yet, it doesn’t let you in on one singularly important piece of information — the id of the newly launched process. Neither is there any other API which would tell you this. This is, as Microsoft would call it, by design.

The problem arises when you are launching something as big as Excel. Or, PowerPoint. They do your bidding well for as long as you need them. But go try and get them to quit and you land in Oz. There’s no way some Excel processes would go away. And then you’d see why you’d need the process id. At least you could force close the process if you did have it.

Now, though there aren’t any official way to do this, there are a few COM gurus out there who have done the hard work for you. They figured that the process id is there somewhere in the data that is marshaled to and from COM. It’s just obscure. Kim Gräsman is one such guru and a god-send! He wrote an utility that gets you the process id. Go ahead and use it, should you ever land in COM-fused state. Make sure you read his article here. He’s a super nice guy and if you have questions/suggestions do mail him. Mail him anyway and say thanks in case this just saved your day!

Note: The process id doesn’t really make much sense if you’re working with DCOM, so this will probably not be of any use if you’re launching processes on a remote machine.

Kim was generous enough to update the source and licensing information on my request and has put it up on github here!

Advertisements

About this entry