windows 使用普通 c++ 或 java 设置文件夹权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8149407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set folder permission using plain c++, or java
提问by Mustafa Magdy
I'm maintaining a software developed using J2SE, (but i'm c# developer actually not have big experience in Java). This software uses access as datastore, this access database is stored on db folder. When the user install this application from "Standard User", not administrator, in Windows 7 or Vista, he cannot get permission on db folder. To make the software run, we need to add "Modify" permission for the current user (which is Standard User).
我正在维护使用 J2SE 开发的软件,(但我是 c# 开发人员,实际上在 Java 方面没有丰富的经验)。该软件使用 access 作为数据存储,该 access 数据库存储在 db 文件夹中。当用户从“标准用户”而不是管理员安装此应用程序时,在 Windows 7 或 Vista 中,他无法获得 db 文件夹的权限。要使软件运行,我们需要为当前用户(即标准用户)添加“修改”权限。
Actually I searched to how to do that using Java, but found nothing, but i found little resources, but not enough. The question is "How can I grant 'Modify' Permission to the current logged user, in either c++ (old c++ not .net) or using Java)?
实际上我搜索了如何使用 Java 来做到这一点,但一无所获,但我发现的资源很少,但还不够。问题是“如何在 c++(旧 c++ 而非 .net)或使用 Java 中向当前登录的用户授予‘修改’权限?
回答by Bojan Komazec
Function presented in MSDN article "Modifying the ACLs of an Object in C++"does the job. GetNamedSecurityInforetrieves discretionary access control list (DACL) for the object (directory in your case). SetEntriesInAclcreates new access control list (ACL) by merging new entries (including permissions) with existing ones. SetNamedSecurityInfoassigns modified DACL back to the object.
MSDN 文章“Modifying the ACLs of an Object in C++”中介绍的函数可以完成这项工作。GetNamedSecurityInfo检索对象(在您的情况下为目录)的自由访问控制列表 (DACL)。SetEntriesInAcl通过将新条目(包括权限)与现有条目合并来创建新的访问控制列表 (ACL)。SetNamedSecurityInfo将修改后的 DACL 分配回对象。
Regarding that Modify permission is a combination of following rights: FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE you can call this function like here:
关于修改权限是以下权限的组合: FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE 你可以像这里一样调用这个函数:
std::string strFullPath("C:\test");
DWORD dwRes = AddAceToObjectsSecurityDescriptor(
const_cast<LPTSTR>(strFullPath.c_str()),
SE_FILE_OBJECT,
"StandardUser",
TRUSTEE_IS_NAME,
FILE_GENERIC_READ | FILE_GENERIC_WRITE | FILE_GENERIC_EXECUTE | DELETE,
GRANT_ACCESS,
NO_INHERITANCE);
回答by Bj?rn Pollex
This questionmight help you for C++. This oneexplains that this is not possible in Java until Java 7. For a more detailed answer, please ask a more specific question (what have you tried so far, why doesn't it work).
这个问题可能对 C++ 有帮助。这解释了在 Java 7 之前这在 Java 中是不可能的。要获得更详细的答案,请提出更具体的问题(到目前为止您尝试过什么,为什么不起作用)。