Solved

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

Posted on 2004-09-05
5
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

630 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