Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7987413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:08:31+00:00 2026-06-04T12:08:31+00:00

I needed to grant access to a file on Windows in a c++ program.

  • 0

I needed to grant access to a file on Windows in a c++ program. I browsed around and copy/pasted code from MSDN and came up with the following. It has been working as far as I can tell.

But then today I stumbled across a warning in MSDN for the use of AddAccessAllowedAceEx, which says: “The caller must ensure that ACEs are added to the DACL in the correct order.”. It then refers the reader to this: http://msdn.microsoft.com/en-us/library/windows/desktop/aa379298(v=vs.85).aspx

So, my request is for any seasoned Windows programmer to review my code below and tell me if I am going to have problems vis-a-vis ACE ordering within the DACL of the file I am modifying (which is passed in via szPath in my function). I will say that I simply added my new ACE to the end of the DACL. If this will be a problem, must I really read out all the ACE’s from the DACL, inspect them and then add them back one at a time being sure to insert my new ACE in the correct position to respect the correct ordering?

char* whoOps::ACLAmigo::AddACEToDACL(char* szPath, char* szSecurityPrincipal, DWORD dwPermission)
{
ACL_SIZE_INFORMATION ACLInfo; 
memset(&ACLInfo, 0, sizeof(ACL_SIZE_INFORMATION));
UCHAR   BuffSid[256];
PSID pSID = (PSID)BuffSid;
int returnCode = ResolveSID(szSecurityPrincipal, pSID);
SE_OBJECT_TYPE SEObjType = SE_FILE_OBJECT;
PACL pOldDACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
SECURITY_INFORMATION ACLSecInfo = DACL_SECURITY_INFORMATION;
returnCode = GetNamedSecurityInfoA(szPath, SEObjType, ACLSecInfo, NULL, NULL, &pOldDACL, NULL, &pSD);  
char* szReturn = NULL;
if (returnCode != ERROR_SUCCESS) {
    szReturn = "GetNamedSecurityInfoA() failed.";
} else {
    BOOL getACLResult = GetAclInformation(pOldDACL, &ACLInfo, sizeof(ACLInfo), AclSizeInformation); 
    if (!getACLResult) {
        szReturn = "GetAclInformation() failed.";
    } else {
        DWORD cb = 0;
        DWORD cbExtra = sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) + GetLengthSid(pSID); 
        cb = ACLInfo.AclBytesInUse + cbExtra; 
        PACL pNewDACL = static_cast<PACL>(HeapAlloc(GetProcessHeap(),0,cb)); 
        BOOL initACLResult = InitializeAcl(pNewDACL, cb, ACL_REVISION); 
        if (!initACLResult) {
            szReturn = "InitializeAcl() failed.";
        } else {
            for (DWORD i = 0; i < ACLInfo.AceCount; ++i)  { 
                ACE_HEADER * pACE = 0; 
                GetAce(pOldDACL, i, reinterpret_cast<void**>(&pACE)); 
                pACE->AceFlags = CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE;
                pACE->AceType = ACCESS_ALLOWED_ACE_TYPE;
                AddAce(pNewDACL, ACL_REVISION, MAXDWORD, pACE, pACE->AceSize); 
            } 
            BOOL addACEResult = AddAccessAllowedAceEx(pNewDACL, ACL_REVISION, CONTAINER_INHERIT_ACE | OBJECT_INHERIT_ACE, dwPermission, pSID);
            if (!addACEResult) {
                szReturn = "AddAccessAllowedAceEx() failed.";
            } else {
                DWORD setSIResult = SetNamedSecurityInfoA(szPath, SEObjType, ACLSecInfo, NULL, NULL, pNewDACL, NULL); 
                if (!setSIResult) {
                    szReturn = "SetNamedSecurityInfoA() failed.";
                } else {
                    szReturn = "AddACEToDACL() succesful.";
                }
            }
        }
        if (pNewDACL) HeapFree(GetProcessHeap(),0, pNewDACL);
    }
    if (pSD) LocalFree(pSD);
}
return szReturn;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:08:33+00:00Added an answer on June 4, 2026 at 12:08 pm

    ACE ordering is really important! Yes, best solution would be

    1. read the content of the ACL before modifying
    2. programmatically inspect each element (ACE) in the Security Descriptor
    3. place your entry appropriatelly (denied before allow)

    This serie has a good amount of samples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I needed some simple string encryption, so I wrote the following code (with a
I needed to use in my old Git similar settings to the following to
I needed to create an application using Struts2 as MVC,Hibernate for data access and
I needed to write a script to enter multi-line input to a program (
I needed to reinstall a package, DrRX.bpl. I removed it from the package list,
I have developed a .NET Windows Service (in VS2010) that needs to: Access shared
I needed to move some src/test/java to src/main/java according to this recommandation from maven-jar-plugin
I needed to change a work item field from PlainText -> String. As I
Needed Finfo but deleted msi package, so uninstalled php 5.3.0, downloaded 5.3.2 and installed.
I needed to update the system tray icon's text value, of my application, whenever

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.