简体   繁体   English

MFC C ++:如何锁定仅在过程中使用的文件?

[英]MFC C++: how can I lock a file to be used only in my process?

I'd like to lock a couple of files to be only used by my process, denying any other application access to these files while my program is running. 我想锁定几个只供我的进程使用的文件,在程序运行时拒绝任何其他应用程序访问这些文件。 Of course I know that I can get exclusive access to a file using Createfile, but my application works differently, I read a bunch of filenames froma config, and process these files with a Lib linked to my application, ie one of the functions in my lib accesses the files, but I don't get a filoehandle or something similar in return. 当然,我知道我可以使用Createfile来独占访问文件,但是我的应用程序工作方式不同,我从配置中读取了一堆文件名,并使用链接到我的应用程序的Lib处理这些文件,即我的功能之一。 lib访问文件,但是我没有得到filoehandle或类似的东西作为回报。

So what I want to acchieve is that while my app is processing these files, no other application can modify them. 因此,我想实现的是,当我的应用程序正在处理这些文件时,没有其他应用程序可以修改它们。 Is this somehow possible? 这可能吗? I am developing using MFC in Visual Studio 8. 我正在Visual Studio 8中使用MFC进行开发。

我从未使用过它们,但是LockFile/LockFileEx文档说: 锁定指定的文件以供调用进程进行独占访问。

You need cooperation from the OS, because that's the only way to influence other processes. 您需要操作系统的合作,因为这是影响其他流程的唯一方法。

The OS requires that you use handles to refer to files. 操作系统要求您使用句柄来引用文件。 It's really the only practical way for the OS; 这实际上是操作系统的唯一实用方法。 using pathnames would be far too complex. 使用路径名太复杂了。 So, you will need to call CreateFile . 因此,您将需要调用CreateFile At that point, just request exclusive access. 此时,只需请求独占访问即可。

Why doens't the CreateFile()'s exclusive flag achieve this? 为什么CreateFile()的独占标志无法实现此目的? It looks like you don't need anything fancy. 看起来您不需要任何花哨的东西。 If your library opens the file with CFile::shareDenyRead and CFile::shareDenyWrite, no other process can read your files as long as they are open by your library. 如果您的库使用CFile :: shareDenyRead和CFile :: shareDenyWrite打开文件,则其他任何进程都无法读取您的文件,只要它们由您的库打开即可。

What you're asking can't be done. 您的要求无法完成。

Because exclusive access is granted per handle, not per process, if you open a file with exclusive access once, every subsequent attempt to open it will fail, even if it is from the same process. 因为排他访问是按句柄而不是按进程授予的,所以如果一次打开一个具有排他访问权限的文件,则以后每次打开该文件的尝试都会失败,即使该文件来自同一进程。 Exclusive access here means your handle is the only valid one, not that only your process can access it. 这里的独占访问意味着您的句柄是唯一有效的句柄,而不是只有您的进程才能访问它。

So even if you lock a file, your lib won't be able to open it, so it's useless to you. 因此,即使您锁定了文件,您的lib也将无法打开它,因此对您来说毫无用处。 The only way is to lock a file and pass the handle to your lib, which you can't do because your lib wants a filename. 唯一的方法是锁定文件并将句柄传递给您的lib,您不能这样做,因为您的lib需要文件名。 Likewise you can't lock the file once it's open by the lib because it won't give you the handle. 同样,由lib打开文件后,您将无法锁定它,因为它不会为您提供句柄。 If you don't have access to the source code of the lib, you're stuck. 如果您无权访问该库的源代码,那么您将陷入困境。

You possibly could try something with user permissions, having you're process run from it's own user account and changing the ownership of the files you're about to modify and then changing it back when you're done. 您可以尝试使用具有用户权限的方法来进行操作,让进程从其自己的用户帐户运行,并更改将要修改的文件的所有权,然后在完成后将其更改回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在两个C ++ MFC插件之间通信? - How can I communicate between two C++ MFC plugins? 如何在数组中存储十六进制? C ++ MFC - How can I store hexadecimals inside an array? C++ MFC 如何在 C++ 中检查文件是否被另一个进程使用? - How to check if a file is used by another process in C++? 如果我只能改变C ++代码并检查控制台输出,如何确定使用哪个C ++编译器? - How can I determine which C++ compiler is used if I only can alter the C++ code and inspect console output? 如何仅删除非数字字符串的第一个字符? (MFC,C ++) - How do I remove only the first character of a string that is not a digit? (MFC, C++) 编写一个可以用作静态但需要锁定的C ++类 - Writing a C++ class that can be used as static, but needs a lock C ++ MFC对话框-如何将连接应用于应用程序的所有对话框? - C++ MFC Dialog - How do I apply a connection to all the dialogs of my application? 如何在C ++中随时查找我的应用程序使用了多少内存 - How can I find how much memory my application is used any any time in c++ 如何根据要处理的文件更改void *的大小 - c++ How can I change the size of a void* according to a file I want to process 如何确保对我的GNU / C ++程序和相关库使用最少的CPU优化? - How can I ensure minimal CPU optimizations will be used for my GNU/C++ program and associated libraries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM