Solved

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

Posted on 2004-09-05
5
521 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
max float value 3 59
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 48
Outlook 13 77
How do I get Window Title of all opened process? 4 32
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

679 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