Link to home
Start Free TrialLog in
Avatar of Cariarer
Cariarer

asked on

File locking...

Hi everyone... Í have actually two questions:
1. How can I create a file, which is completly locked for other apps. Meaning no reading or writing permited (like the Windows swap file).

2. If a file is locked (read only), and I want to replace it, I have to replace it outside of Windows. I know that Windows has some mechanisem to replace this files (the famous "Windows is updating your system files"). How can I place this file in a list to be copied on the next Windows start?

Thanks, Car...
Avatar of rwilson032697
rwilson032697

1. Set filemode to fmShareExclusive:

savemode := filemode;
filemode := fmShareExclusive;
reset(f);
filemode := savemode;

2. This involves adding entries to an ini file (WIN.INI?) directing windows to copy the files. I am sure another expert will fill this void in...

Cheers,

Raymond.


Avatar of Cariarer

ASKER

Hi Raymund...

thanks for your answer, but I reject it for now (since it's only one question answered :-)... but I will give you 40 points for it. I will open a new question, which you can answer... I hope that is fair enough for you...

Thanks, Car...
Sounds fair to me!

Cheers,

Raymond.

Take a look at LockFile, LockFileEx UnlockFile and UnlockFileEx.

Regards,

Epsylon.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Cariarer,

winNT: Use MoveFileEx(src,dst,MOVEFILE_DELAY_UNTIL_REBOOT).
win9x: Create (or extend the existing) "c:\windows\winInit.ini". Must look like this:

[Rename]
c:\destfile.txt=c:\sourcefile.txt

Since the winInit.ini is evaluated in DOS, you can only use short filenames there...  :-(

Regards, Madshi.
AAAH - Barry, you were faster...   :-(

But you forgot the NT solution...
ok i suppose we better be compatable with nt :-)
If the Win32Platform = VER_PLATFORM_WIN32_NT then use

      MoveFileEx( Pchar( tempFilenameWithPath ),
                  Pchar( newfilenamewithpath ),
                  MOVEFILE_REPLACE_EXISTING or
MOVEFILE_DELAY_UNTIL_REBOOT )
    Else
      WritePrivateProfileString(
        'rename',
        Pchar( newfilenamewithpath ),
        Pchar( tempFilenameWithPath ),
        'wininit.ini' );
 
After that you do an ExitWindowsEx( EWX_REBOOT, 0 );
 
On NT that requires some fiddling with the security attributes of the current process:
 
here is an example posted originally by Lars Marquart:
 
function GetShutdownPrivilege:Boolean;
const
  SHN    : PChar = 'SeShutdownPrivilege';
  EMP    : PChar = '';
var
  hToken : THandle;
  tkp,p  : TTokenPrivileges;
  RetLen : Integer;
  Err    : DWord;
begin
// Get a token for this process.
{$IFDEF VER100}
  if not OpenProcessToken(GetCurrentProcess,
                          TOKEN_ADJUST_PRIVILEGES or
                          TOKEN_QUERY,hToken)        then begin
{$ELSE}
  if not OpenProcessToken(GetCurrentProcess,
                          TOKEN_ADJUST_PRIVILEGES or
                          TOKEN_QUERY,@hToken)      then begin
{$ENDIF}
    Result := False; // 'Error: OpenProcessToken:
'+IntToStr(GetLastError)
    Exit;
  end;
// Get the LUID for the shutdown privilege.
  if not LookupPrivilegeValue(EMP,SHN,tkp.Privileges[0].Luid) then begin     Result := False; // 'Error: LookupPrivilegeValue:
'+IntToStr(GetLastError)
    Exit;
  end;
  tkp.PrivilegeCount          := 1; // One privilege to set
  tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
  AdjustTokenPrivileges(
hToken,False,tkp,SizeOf(TTokenPrivileges),p,RetLen);
// Cannot test the return value of AdjustTokenPrivileges.
  Err := GetLastError;
  if Err<>ERROR_SUCCESS then begin
//    Err=1300: 'You do not have the right to shut the system down' //    else      'Error: AdjustTokenPrivileges: '+IntToStr(Err)
    Result := False;
    Exit;
  end;
// Current user have privileges to shutdown the system.
  Result := True;
end;

associated reg key i think is this one:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations
 
Hi everyone... does anyone know if there is a way to just copy the file instead of moving it? I want to copy the file FROM the windows folder to a backup folder. It's not realy helpfull, when the file is removed ;-))

Thanks...
well you couls try
copyfile(thefilename(pchar),thenewfilename(pchar),ifnewfileexistsalready(boolean) );

but i doubt it gonna work on a locked file ..