Solved

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

  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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now