Solved

How to set a Folder's security/permission level through C++?

Posted on 2004-09-05
5
505 Views
Last Modified: 2012-06-21
HI,
    What are the available method/function that can be used to set a folders permission via C++? if Possible , please show some simple implementation of the method. The reason why i need it is because i need to restrict deletion of folders/files in a specified folder. Only allows certain users to read and copy files from the specified folder.


Thank you..

MAx..
0
Comment
Question by:maxemillion_max
5 Comments
 
LVL 23

Accepted Solution

by:
Mysidia earned 125 total points
ID: 11987131
Well, the C++ language provides nothing to do with file permissions.

Various Operating Systems provide system calls to manipulate file
permissions, for example most *ix OSes provide calls such as chmod(path, mode)
  chmod("/path/to/blah", 0700);

or   fchmod(filedes, mode)

Of course mode 0700 means only owner has access or 0750  means
owner can read/write, members of the group owning the file can read,
noone else can do anything

these are Unix system calls and you need
#include <sys/types.h>
#include <sys/stat.h>

(There are also  chown, fchown, lchown)

Every different kind of platform has its own system, which one are you trying to deal with file permissions on?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 125 total points
ID: 11989870
If you are referring to Win32, you'd use ACLs to grant a user read access to a directory

  LPTSTR FileName = "C:\\Path\\Directory";
  LPTSTR TrusteeName = "domain\\TheUser";

  DWORD InheritFlag = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE;
  ACCESS_MODE option = GRANT_ACCESS;
  EXPLICIT_ACCESS explicitaccess;

  PACL NewAcl = NULL;

  DWORD dwError;

  BuildExplicitAccessWithName(
        &explicitaccess,
        TrusteeName,
        GENERIC_READ | STANDARD_RIGHTS_READ,
        SET_ACCESS,
        InheritFlag
        );


  //
  // add specified access to the object
  //

  dwError = SetEntriesInAcl(
          1,
          &explicitaccess,
          NULL,
          &NewAcl
          );

  //
  // apply new security to file
  //

  dwError = SetNamedSecurityInfo(
                  FileName,
                  SE_FILE_OBJECT, // object type
                  DACL_SECURITY_INFORMATION,
                  NULL,
                  NULL,
                  NewAcl,
                  NULL
                  );

(This is a stripped down version of the MS' AclAPI sample)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question