Mustafa Magdy
Mustafa Magdy

Reputation: 1330

Set folder permission using plain c++, or java

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).

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)?

Upvotes: 1

Views: 6550

Answers (2)

Bojan Komazec
Bojan Komazec

Reputation: 9536

Function presented in MSDN article "Modifying the ACLs of an Object in C++" does the job. GetNamedSecurityInfo retrieves discretionary access control list (DACL) for the object (directory in your case). SetEntriesInAcl creates new access control list (ACL) by merging new entries (including permissions) with existing ones. SetNamedSecurityInfo assigns modified DACL back to the object.

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:

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);

Upvotes: 2

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76886

This question might help you for C++. This one explains 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).

Upvotes: 0

Related Questions