Collin Dauphinee
Collin Dauphinee

Reputation: 13993

How can I share share read/write access to a file within the same process, but forbid it externally?

I have a few objects that hold a handle to the same file. However, each of these objects is reading/writing a different part the file. I need to prevent external access to the file, but still be able to create new handles to it from within my process.

If I don't share read/write access when invoking CreateFile, I can't obtain new handles to the file from within my process. If I use DuplicateHandle, all of the handles will share the same file position.

I can work around this by making a proxy to the file that seeks all over the place as needed, but this makes for terrible design. Is there any way to accomplish internal sharing like this?

Upvotes: 1

Views: 247

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596497

Depending on your needs, you might be able to call CreateFile() once to create/open the file, and then have each of your objects use CreateFileMapping() and MapViewOfFile() to access different sections of the file using that single file handle as the base.

Upvotes: 1

DragonZero
DragonZero

Reputation: 845

Files are generally not for multi-user interactions such as this. A database is what is needed here. Then the data you need written can be saved to the database and all the synchronous handling would now be the databases responsibility. If you still require a file, after the data has been input, it would be an idea to generate the file when needed based off the database data.

Upvotes: 1

Related Questions