does the windows file handle change?

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

does the windows file handle change?

are file handles unique to the whole operating system or just the current
directoy? if a file is opened then closed then opened again, does the file
handle remain the same?
 
Daniel said:
does the windows file handle change?

are file handles unique to the whole operating system or just the
current directoy? if a file is opened then closed then opened again,
does the file handle remain the same?

Do you mean the handle returned from something like the Win32
CreateFile? The handle you are given is essentially an index into a
table of memory addresses. The memory address is pointer to a structure
in the process's memory that the OS uses to access the file. This handle
is only valid in this process for the lifetime of the process (or until
CloseHandle is called).

After you call CloseHandle that handle becomes invalid because the
memory structure it points to will have been released. For this reason
there is no point in passing a file handle to another machine, nor
persisting it in a database or a file. You can pass a handle to another
process, but to do so, you have to call DuplicateHandle, which in effect
creates a memory structure in the other process and copies the data
across.

When a file is created the memory structure referred to by the handle
will be information about the file. If you close the file (CloseHandle)
the memory will be released and the handle will be invalid. If you open
the file again, a new memory structure will be created with essentially
the same data, but it will most likely be in a different memory address
and most likely the index of it in the process's table will be different
and hence the handle returned will most likely be different.

Richard
 
Back
Top