tags:

views:

63

answers:

3

I want to call the Windows API OpenProcess function on another process running on the machine. Will this always cause the file whose process I am opening to be write locked? Or does it depend on the access rights I request?

A: 

What makes you think that OpenProcess is ever the reason the file is write locked?

SamB
This could be possible because the outstanding reference from OpenProcess keeps the process object alive, and that might keep the file reference alive as well. I'd need to run some experiments to confirm.
Michael
A: 

The process file is locked while the process is running; it doesn't have anything to do with OpenProcess. The file is unlocked when the process terminates.

Luke
+1  A: 

Yes, it is a fundamental property of Windows. When an executable file gets loaded (EXE or DLL), Windows creates a memory mapped view of the file. Chunks of code or data from the executable file get page-faulted into RAM, as needed to keep the program running. It works the other way around too, when Windows needs to make RAM available for another program then it throws chunks of mapped pages away, the ones that weren't used in a while. Those pages don't take up space in the paging file if they are code, they can be reloaded from the executable file.

Very efficient, code that was written when 16 megabytes of RAM was a luxury. The memory mapped section keeps a write lock on the file. Still useful in this day and age, it prevents some kind of malware with fecking with the code of a running process.

Hans Passant
I did some experiments, and this matches what I am seeing. Thanks.
Vanessa MacDougal