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...
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...
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...
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.
Cheers,
Raymond.
Take a look at LockFile, LockFileEx UnlockFile and UnlockFileEx.
Regards,
Epsylon.
Regards,
Epsylon.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Cariarer,
winNT: Use MoveFileEx(src,dst,MOVEFIL E_DELAY_UN TIL_REBOOT ).
win9x: Create (or extend the existing) "c:\windows\winInit.ini". Must look like this:
[Rename]
c:\destfile.txt=c:\sourcef ile.txt
Since the winInit.ini is evaluated in DOS, you can only use short filenames there... :-(
Regards, Madshi.
winNT: Use MoveFileEx(src,dst,MOVEFIL
win9x: Create (or extend the existing) "c:\windows\winInit.ini". Must look like this:
[Rename]
c:\destfile.txt=c:\sourcef
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...
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_REBOO T )
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:Boole an;
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(GetCurren tProcess,
TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY,hToken) then begin
{$ELSE}
if not OpenProcessToken(GetCurren tProcess,
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,S HN,tkp.Pri vileges[0] .Luid) then begin Result := False; // 'Error: LookupPrivilegeValue:
'+IntToStr(GetLastError)
Exit;
end;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0].Attribut es := SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(
hToken,False,tkp,SizeOf(TT okenPrivil eges),p,Re tLen);
// 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;
If the Win32Platform = VER_PLATFORM_WIN32_NT then use
MoveFileEx( Pchar( tempFilenameWithPath ),
Pchar( newfilenamewithpath ),
MOVEFILE_REPLACE_EXISTING or
MOVEFILE_DELAY_UNTIL_REBOO
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:Boole
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(GetCurren
TOKEN_ADJUST_PRIVILEGES or
TOKEN_QUERY,hToken) then begin
{$ELSE}
if not OpenProcessToken(GetCurren
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,S
'+IntToStr(GetLastError)
Exit;
end;
tkp.PrivilegeCount := 1; // One privilege to set
tkp.Privileges[0].Attribut
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(
hToken,False,tkp,SizeOf(TT
// 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\ CurrentCon trolSet\Co ntrol\Sess ion
Manager\PendingFileRenameO perations
HKEY_LOCAL_MACHINE\SYSTEM\
Manager\PendingFileRenameO
ASKER
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...
Thanks...
well you couls try
copyfile(thefilename(pchar ),thenewfi lename(pch ar),ifnewf ileexistsa lready(boo lean) );
but i doubt it gonna work on a locked file ..
copyfile(thefilename(pchar
but i doubt it gonna work on a locked file ..
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.