News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

File handle for multiple use

Started by Danesh, May 09, 2005, 05:22:12 PM

Previous topic - Next topic

Danesh

Hi all,

I am working on a multi thread program which should share some files for some users who will connect to it. The problem is each user can open a file a get the handle and has read/write access, but other users can not open a file which is already open. I know that this is completely reseanable because the opened file has been locked for the user who is using it, but I want to know, is it possible to open a file (maybe with changing inout flags or...) in a way so when other users try to open it which is already open, the CreateFile function return file handle which is in use by first user instead of error message ? In other words, can a file be opened and handle be able to be shared between many users ?

Thanks,

Danesh


SamiP

I think that two users can't get same handle. But I think that they can get both their own handle to the same file at the same time. Check CreateFile API and specifically dwShareMode parameter of it.


QvasiModo

You have to use the SHARE_READ_ACCESS and/or SHARE_WRITE_ACCESS flags on CreateFile, if I'm not wrong. Check out MSDN just in case:

http://msdn.microsoft.com

Cheers :)

Tedd

Sharing file handles between processes isn't safe. You can share memory mapped files using a named file-mapping.
However, two users can open the same same as long as you specify FILE_SHARE_READ and/or FILE_SHARE_WRITE - depeding on the effect you want,but be careful if you have two users writing to the file at the same time :wink
No snowflake in an avalanche feels responsible.

Danesh


Thanks QvasiModo and SamiP, yes now it works. But Tedd's tips was very intressting. Yeah, it is not safe, but what would happen if two users try to write to a file at a same time ? I mean, practically it would not harm my system but would it make an OS error or crash or whatever ? Well, for example in database servers it would happen as a normal process, but how did they solve this problem ? I think they don't care about writing data however two (or even more) users try to write to a file, parallel  but what I am not sure about is if OS is that smart to queue two writing request and then perform one by one. Otherwise I should manage it and create a writing queue and then write one by one, isn't it ?


Tedd

A file handle opened in one process is not a valid file handle in another process. Or at least, this is what should happen; but it depends on the OS design. Relying on any other behaviour because it happens to work at the time is a bad idea - a handle should be considered process specific; if you're lucky then the second process will simply fail to access the file due to an invalid handle, if not then an number of effects could be produced including corrupting the file :bdg
If two separate processes open the same file for writing (with FILE_SHARE_READ), they should be okay to write to it and each write should make it to the file. The best thing to do is just test it. In the worst case, I expect what could happen is the last handle closed will flush the buffers and only 'his' writes will make it into the file.
File-Mappings are designed to give a coherrent and up-to-date view of the file, so if all else fails, this should work.
No snowflake in an avalanche feels responsible.