Wednesday, June 6, 2007

Launching a Process from ASP.NET 1.1

I've been grappling with some pretty crappy code lately, as I try to rescue a project from another coder's gross mismanagement. Features are poorly implemented or not implemented at all, there is code duplication everywhere, the whole thing makes me want to scream bloody murder and take an axe to it.

One big problem was integrating with a third-party COM object that just plain didn't work under ASP.NET. The component creator had no advice on the matter, so I took things into my own hands and wrote a console application that wrapped the object's core functions. Then came the pain: how do I invoke a process from C# that runs as a given user (Network Service won't cut it) and doesn't show a window?

Here's the result of a ton of Google searches and the pulling out of much hair. It uses the Windows API call "CreateProcessWithLogonW" (can anybody explain why windows API coders use such arcane nomenclature?), and sets flags to keep the process hidden. (The real gem was finding the @"winsta0\default" line -- without this your process cannot execute from ASP.NET. Why? Hard to say from merely Google. Perhaps if I bought a book.) Feel free to incorporate this into your own code, most of it came from this handy site.

UPDATE: I spoke too soon. This ONLY works if the user I log in with is in the Administrators group -- not good! I'm going to take a completely different tack. If anybody knows what I'm missing to make this work with a non-admin, chime in in the comments. Even if it's five years from the posting date, I'd love to know what I did wrong!

Show Code

No comments:

Website Counter