?
Solved

is there a way to programmatically change folder permissions in windows vista?

Posted on 2007-07-25
4
Medium Priority
?
1,133 Views
Last Modified: 2013-12-04
either with mfc or .net (c++ or c# preferred)

Here's the scenario - I have an MFC dialog app that I am trying to make vista-compatible.  I need to be able to write to files in the installpath (and subdirs) at all time, so I figure that i need to give the "Everyone" account full access to the folder.  Can I do this with code, or will I need to have the end user change the permissions?

Thanks for any suggestions you can offer!!
0
Comment
Question by:aesdtt
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 19568896
Use

    LPTSTR FileName = "C:\\Temp\\MyDir";
    LPTSTR TrusteeName = "Everyone";

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


    PACL ExistingDacl;
    PACL NewAcl = NULL;
    PSECURITY_DESCRIPTOR psd = NULL;

    DWORD dwError;

    dwError = GetNamedSecurityInfo(
                        FileName,
                        SE_FILE_OBJECT,
                        DACL_SECURITY_INFORMATION,
                        NULL,
                        NULL,
                        &ExistingDacl,
                        NULL,
                        &psd
                        );

    BuildExplicitAccessWithName(
            &explicitaccess,
            TrusteeName,
            AccessMask,
            option,
            InheritFlag
            );

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

    dwError = SetEntriesInAcl(
            1,
            &explicitaccess,
            ExistingDacl,
            &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
 

Author Comment

by:aesdtt
ID: 19647143
sorry it took so long to respond - i actually was able to get this all done in c++, but you deserve the points anyway.  if anyone ever needs some help with this in c++, let me know
0
 

Expert Comment

by:VapiSoft
ID: 26088907
I implemented this code on Vista and it worked well.
But when I tried to set it from the VISTA to a remote folder on an XP computer
\\remotexp\c\folder
It recived an error ACCESS_DENIED (5)
0
 
LVL 86

Expert Comment

by:jkr
ID: 26088959
Sorry, you can't do that remotely this way that easily.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Article by: btan
The intent is not to repeat what many has know about Ransomware but more to join its dots of what is it, who are the victims, why it exists, when and how we respond on infection. Lastly, sum up in a glance to share such information with more to help…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses

850 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