function ShareDirectoryNT(const ADir, AName, APassword: WideString; ReadOnly: Boolean): Boolean;
type
TShareInfo502 = record
shi502_netname: PWideChar;
shi502_type: DWORD;
shi502_remark: PWideChar;
shi502_permissions: DWORD;
shi502_max_uses: DWORD;
shi502_current_uses: DWORD;
shi502_path: PWideChar;
shi502_passwd: PWideChar;
shi502_reserved: DWORD;
shi502_security_descriptor: PSECURITY_DESCRIPTOR;
end;
const
ACL_REVISION = 2;
SECURITY_WORLD_SID_AUTHORITY: TSidIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 1));
SECURITY_WORLD_RID = ($00000000);
SECURITY_NT_AUTHORITY : TSidIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = ($00000020);
DOMAIN_ALIAS_RID_ADMINS = ($00000220);
var
NetShareAddNT : function(servername: PWideChar;
level: DWORD;
buf: Pointer;
parm_err: LPDWORD): DWORD; stdcall;
ShareNT : TShareInfo502;
FLibHandle : THandle;
pSd : PSECURITY_DESCRIPTOR;
pDacl : PACL;
EveryoneSid, AdminSid : Pointer;
begin
Result := False;
FLibHandle := LoadLibrary('NETAPI32.DLL');
if FLibHandle = 0 then Exit;
try
NetShareAddNT := GetProcAddress(FLibHandle, 'NetShareAdd');
if not Assigned(NetShareAddNT) then Exit;
FillChar(ShareNT, SizeOf(ShareNT), 0);
ShareNT.shi502_netname := PWideChar(AName);
ShareNT.shi502_type := STYPE_DISKTREE;
ShareNT.shi502_remark := nil;
ShareNT.shi502_permissions := 0;
ShareNT.shi502_max_uses := Longword(-1);
ShareNT.shi502_current_uses := 0;
ShareNT.shi502_path := PWideChar(ADir);
ShareNT.shi502_passwd := PWideChar(APassword);
ShareNT.shi502_reserved := 0;
GetMem(pDacl, 256);
InitializeAcl(pDacl^, 256, ACL_REVISION);
EveryoneSid := nil;
AdminSid := nil;
AllocateAndInitializeSid(SECURITY_WORLD_SID_AUTHORITY, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, EveryoneSid);
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, AdminSid);
AddAccessAllowedAce(pDacl^, ACL_REVISION, GENERIC_ALL, AdminSid);
if ReadOnly then
AddAccessAllowedAce(pDacl^, ACL_REVISION, (GENERIC_READ or GENERIC_EXECUTE or READ_CONTROL or STANDARD_RIGHTS_READ), EveryoneSid)
else
AddAccessAllowedAce(pDacl^, ACL_REVISION, GENERIC_ALL, EveryoneSid);
GetMem(pSd, SECURITY_DESCRIPTOR_MIN_LENGTH);
InitializeSecurityDescriptor(pSd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(pSd, TRUE, pDacl, False);
ShareNT.shi502_security_descriptor := pSd;
Result := NetShareAddNT(nil, 502, @ShareNT, nil) = Nerr_Success;
if Assigned(EveryoneSid) then
FreeSid(EveryoneSid);
if Assigned(AdminSid) then
FreeSid(AdminSid);
FreeMem(pDacl);
FreeMem(pSd);
finally
FreeLibrary(FLibHandle);
end;
end;