Mega Code Archive

 
Categories / Delphi / Files
 

Determine the owner of a file

Title: Determine the owner of a file Question: How the get the domain and file owner. Answer: unit Unit1; {------------------------------------------------------------------------------- This article is just a quick reference. -------------------------------------------------------------------------------} interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; {------------------------------------------------------------------------------} type TForm1 = class(TForm) OpenDialog1: TOpenDialog; btOpen: TButton; edtDomain: TEdit; edtOwner: TEdit; Label1: TLabel; Label2: TLabel; edtFileName: TEdit; Label3: TLabel; procedure btOpenClick(Sender: TObject); private function GetFileOwner(FileName: string; var Domain, Username: string): Boolean; end; var Form1: TForm1; implementation {$R *.dfm} {------------------------------------------------------------------------------} procedure TForm1.btOpenClick(Sender: TObject); var Domain, Username: string; begin if not OpenDialog1.Execute then exit; if GetFileOwner( OpenDialog1.FileName, Domain, Username ) then begin edtFileName.Text := OpenDialog1.FileName; edtDomain.Text := Domain; edtOwner.Text := Username; end else begin ShowMessage( Format( 'Could not obtain file information for: %s ' , [OpenDialog1.FileName]) ); end; end; {------------------------------------------------------------------------------- Who created this file? -------------------------------------------------------------------------------} function TForm1.GetFileOwner(FileName: string; var Domain, Username: string): Boolean; var SecDescr : PSecurityDescriptor; { Structure for the security descriptors } SizeNeeded , SizeNeeded2 : DWORD; OwnerSID : PSID; { PSID = pointer (SID = security identifier) } OwnerDefault: BOOL; OwnerName , Group : PChar; OwnerType : SID_NAME_USE; { SID_NAME_USE is a SID-Type structure } begin { alloc and init } GetMem( SecDescr , 255 ); GetMem( OwnerSID , SizeOf( PSID ) ); GetMem( OwnerName, 255 ); GetMem( Group , 255 ); try { Are we allowed to obtain informations? } Result := GetFileSecurity( PChar(FileName), OWNER_SECURITY_INFORMATION , SecDescr, 255, SizeNeeded ); if Result then begin { Getting the owner of the security descriptor ( get SID ) } Result := GetSecurityDescriptorOwner( SecDescr, OwnerSID, OwnerDefault ); if Result then begin SizeNeeded := 255; SizeNeeded2 := 255; { Getting user and domain informations by using the SID. The first param is lpSystemName which can be the local or a remote system. } Result := LookupAccountSID( nil, OwnerSID, OwnerName , SizeNeeded, Group, SizeNeeded2, OwnerType ); if Result then begin Domain := Group; Username := OwnerName; end; end; end; if not Result then RaiseLastOSError; finally FreeMem( Group ); FreeMem( OwnerName ); OwnerSID := nil ; { FreeMem( OwnerSID ); Do not try this you will fail ;) } FreeMem( SecDescr ); end; end; {------------------------------------------------------------------------------} end.