简体   繁体   中英

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?

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.

根据您的需求,您也许可以调用一次CreateFile()来创建/打开文件,然后让每个对象使用CreateFileMapping()MapViewOfFile()来使用该单个文件句柄来访问文件的不同部分,如下所示:基地。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM