I'm developing a program which could do live-update to modules of our main program.
If the main exe is running, it needs to
1) rename it to temporary name, eg: %productpath%\main.exe -> %productpath%\temp\temp.exe
::MoveFileEx(%productpath%\main.exe, %productpath%\temp\temp.exe, MOVEFILE_REPLACE_EXISTING)
--- GetLastError()
returns ERROR_SHARING_VIOLATION
2) delete temporary file until reboot
::MoveFileEx(%productpath%\temp\temp.exe, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
3) copy new downloaded exe to original path %productpath%\main.exe
My questions is why does it fail in the first step, where MoveFileEx()
returns ERROR_SHARING_VIOLATION
because the exe is running?
My update program has Administrator rights.
Because when windows starts a process, it locks its executable to prevent modification. That way, windows doesn't need to load the entire image into memory, and can page it in on demand.