Setentriesinacl scheitert und GetLasterror gibt 87 zurückC++

Programme in C++. Entwicklerforum
Anonymous
 Setentriesinacl scheitert und GetLasterror gibt 87 zurück

Post by Anonymous »

Ich versuche, Schreibberechtigungen für eine bestimmte Datei wegzunehmen. Ich verwende den Beispielcode von MSDN dafür: https://learn.microsoft.com/en-us/windo ... ject-in-c-
Dieses Beispiel funktioniert jedoch nicht. Der Anruf in die Funktion Setentriesinacl schlägt fehl und getLasterror gibt 87 . 48 . Ich verwende Windows 11 .
Wie kann ich dieses Problem lösen? Um den folgenden Code auszuführen, schreiben Sie den richtigen SID in die Variable und den Pfad zu Ihrer Datei in den ObjectName Variable.

Code: Select all

#include 
#include 
#include 

#include 

DWORD AddAceToObjectsSecurityDescriptor(
LPTSTR pszObjName,          // name of object
SE_OBJECT_TYPE ObjectType,  // type of object
LPTSTR pszTrustee,          // trustee for new ACE
TRUSTEE_FORM TrusteeForm,   // format of trustee structure
DWORD dwAccessRights,       // access mask for new ACE
ACCESS_MODE AccessMode,     // type of ACE
DWORD dwInheritance         // inheritance flags for new ACE
)
{
DWORD dwRes = 0;
PACL pOldDACL = NULL, pNewDACL = NULL;
PSECURITY_DESCRIPTOR pSD = NULL;
EXPLICIT_ACCESS ea;

if (NULL == pszObjName)
return ERROR_INVALID_PARAMETER;

// Get a pointer to the existing DACL.

dwRes = GetNamedSecurityInfo(pszObjName, ObjectType,
DACL_SECURITY_INFORMATION,
NULL, NULL, &pOldDACL, NULL, &pSD);
if (ERROR_SUCCESS != dwRes) {
printf("GetNamedSecurityInfo Error %u\n", dwRes);
goto Cleanup;
}

// Initialize an EXPLICIT_ACCESS structure for the new ACE.

ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
ea.grfAccessPermissions = dwAccessRights;
ea.grfAccessMode = AccessMode;
ea.grfInheritance = dwInheritance;
ea.Trustee.TrusteeForm = TrusteeForm;
ea.Trustee.ptstrName = pszTrustee;

// Create a new ACL that merges the new ACE
// into the existing DACL.

dwRes = SetEntriesInAcl(1, &ea, pOldDACL, &pNewDACL);
if (ERROR_SUCCESS != dwRes) {
printf("SetEntriesInAcl Error %u\n", dwRes);
goto Cleanup;
}

// Attach the new ACL as the object's DACL.

dwRes = SetNamedSecurityInfo(pszObjName, ObjectType,
DACL_SECURITY_INFORMATION,
NULL, NULL, pNewDACL, NULL);
if (ERROR_SUCCESS != dwRes) {
printf("SetNamedSecurityInfo Error %u\n", dwRes);
goto Cleanup;
}

Cleanup:

if (pSD != NULL)
LocalFree((HLOCAL)pSD);
if (pNewDACL != NULL)
LocalFree((HLOCAL)pNewDACL);

return dwRes;
}

int main()
{
wchar_t trustee[] = L"S-1-5-21-#-#-#-#";
wchar_t objectName[] = L"C:\\Users\\Username\\Desktop\\acl.txt";
AddAceToObjectsSecurityDescriptor(objectName, SE_FILE_OBJECT, trustee, TRUSTEE_IS_SID, FILE_GENERIC_WRITE, DENY_ACCESS, NO_INHERITANCE);
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post