views:

22

answers:

0

I have a service that gets updated automatically by another service. The updating service downloads the latest version, shuts down the service (waiting for the stopped status), copies in new files, and starts it back up again. Lately, though, it's getting an access denied error when trying to copy the executable file. This is similar to another issue I was having, where I was trying to update a DLL that a service was relying on. I solved this by ensuring that all of the services's threads were shut down before the service was fully stopped.

Thinking that the same thing was going on here, I made absolutely sure that the service's threads were all shutting down before it was closed. I've verified this, but I still can't overwrite that file. I ran handle.exe on it, and it says that nothing is accessing the executable. I can also uninstall the service and then delete the file, should I have to do that just to update it?

In a post that I put up on that other issue, this was an answer that I got:

The DLL was loaded and mapped into virtual memory. That puts a lock on the file. Starting or stopping the service doesn't change that. The exact same is true for the service EXE.

-Hans Passant

First, I know that the two issues are very similar, but the focus on that post was if it was possible to break the connection between an executable and its referred DLL.

Anyway, is uninstallation of the service they key to releasing this lock? I can do that, it just seems like a bit of overkill, especially because I know that if I were doing this manually (rather than programattically), I know that I could overwrite that executable as long as the service was shut down. And since I was able to update that other service's DLL (I don't know if updating its executable will work), I would imagine that I could do the same thing with this other service's executable.

So what's the proper way to update a service programatically without running into file lock conflicts?