Skip to content
Search

WindowsApi:CreateDirectory

lastmod 2018-08-17created 2016-03-314 edits

Creates a new directory.

Remarks

Takes a variable of type SECURITY_ATTRIBUTES; if it is NULL, the parent folder's value is used as is.

All access permission

An example that grants all ACCESS permissions on a folder using a SECURITY_ATTRIBUTES value in the CreateDirectory function.

 SECURITY_ATTRIBUTES sa;
 SECURITY_DESCRIPTOR sd;
 PSID pEveryoneSID = NULL;
 SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY;
 EXPLICIT_ACCESS ea[2];
 PACL pacl = NULL;
 if(!AllocateAndInitializeSid(&SIDAuthWorld,1, SECURITY_WORLD_RID,0,0,0,0,0,0,0,&pEveryoneSID))
 {
  AfxMessageBox("Fail to get Everyone SID!!!",0,0);
  return;
 }
 ZeroMemory(&ea, 2*sizeof(EXPLICIT_ACCESS));
 ea[0].grfAccessPermissions = GENERIC_ALL;
 ea[0].grfAccessMode = SET_ACCESS;
 ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
 ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
 ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID;

 SetEntriesInAcl(1,ea, NULL, &pacl);
 InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
 SetSecurityDescriptorDacl(&sd, TRUE,pacl , FALSE);
 //SetSecurityDescriptorSacl(&sd, TRUE,pacl , FALSE);


 sa.nLength = sizeof (SECURITY_ATTRIBUTES);
 sa.lpSecurityDescriptor = &sd;
 sa.bInheritHandle = TRUE;

 ::CreateDirectory(sysDirBuffer_tmp,&sa);
 if(pEveryoneSID){
  FreeSid(pEveryoneSID);
 }
 if(pacl){
  LocalFree(pacl);
 }

See also