Well, well. The second fruit of my labor has risen. I’m currently developing custom Microsoft Provisioning Services (MPS) providers. These are COM DLLs written in C#. The problem is that when we test them, the MPS process keeps the DLLs loaded (so the files are in use). So if I edit the source and recompile, I get errors because my post-build script can’t overwrite the DLL, because they are in use. Stopping and starting the two MPS services doesn’t help. The only thing that helps is manually killing the DllHost process from the task manager. However, there are two such processes and I need to kill only one of them. One is owned by the system and one is owned by an MPS user. This is not an issue when killing the process by hand, you can see the owner in the task manager, but when I tried to automate it I ran into a small issue. For as far as I could see, there is no way to determine the user who owns the process from the System.Diagnostics namespace. I even looked at the standard Win32 API funcions, but still no dice. The task manager knows, so I too should be able to find it out.
Then it hit me: WMI! I checked and indeed. With WMI I can enumerate the processes (in fact, thanks to WMI’s SQL-like interface, I can select the process I want based on its name) and then get the user who owns the process.
So I wrote a small program and plugged it into my post-build step. It doesn’t work a 100% (for some reason the program is finished and the post-build script tries to copy the files while the process is still busy terminating), but it works good enough for me.
To get the program, simply download this file and put it in a new Windows Forms project. It’s an empty Windows Forms class (I created it as Windows Forms, because a console application would pop-up a DOS box and I don’t like unneeded pop-ups
) and all it runs is the Main() function.
To plug it in my post-build script I used the following code, maybe you can use it as well:
position = InStrRev(WScript.ScriptFullName, "")
scriptDir = Left(WScript.ScriptFullName, position)
set shell = CreateObject(”WScript.Shell”)
shell.Run scriptDir & “ProgramKill dllhost.exe MPFServiceAcct”,, true