Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-05
5
Medium Priority
?
545 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 500 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

926 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