Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File locking...

Posted on 1999-07-16
11
Medium Priority
?
148 Views
Last Modified: 2010-04-06
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...
0
Comment
Question by:Cariarer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1388999
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.


0
 

Author Comment

by:Cariarer
ID: 1389000
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...
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1389001
Sounds fair to me!

Cheers,

Raymond.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:Epsylon
ID: 1389002
Take a look at LockFile, LockFileEx UnlockFile and UnlockFileEx.

Regards,

Epsylon.
0
 
LVL 17

Accepted Solution

by:
inthe earned 300 total points
ID: 1389003
hi,
you could call ExitWindowsEx(EWX_REBOOT, 0); after doing something like the following procedure.the following procedure would rename your app on the next windows start by using the wininit.ini file.(dont worry if you dont already have this file it is used for doing the restart stuff by apps which create it then can be deleted after).


procedure TForm1.Button1Click(Sender: TObject);
 var tmp:array[0..1024] of char;
      appshortname,windir,buffer:string;
      f:textfile;
      backupfile:tstringlist;
      c:word;
      doit:boolean;
begin
   getshortpathname(pchar(application.exename),tmp,1025);
    appshortname:=string(tmp);
    getwindowsdirectory(tmp,1025);
    windir:=string(tmp);
    if length(windir)>3 then windir:=windir+'\';
    assignfile(f,windir+'wininit.ini');
    if fileexists(windir+'wininit.ini')=false then
    begin
      rewrite(f);
      writeln(f,'[Rename]');
      writeln(f,'nul='+appshortname);
    end else
    begin
      backupfile:=tstringlist.create;
      reset(f);
      while not eof(f) do
      begin
        readln(f,buffer);
        backupfile.add(buffer);
      end;
      closefile(f);
      doit:=false;
      for c:=0 to backupfile.count-1 do
      if uppercase(backupfile.strings[c])='[RENAME]' then
      begin
        backupfile.insert(c+1,'nul='+appshortname);
        doit:=true;
        break;
      end;
      rewrite(f);
      for c:=0 to backupfile.count-1 do writeln(f,backupfile.strings[c]);
      if doit=false then
      begin
        writeln(f,'[Rename]');
        writeln(f,'nul='+appshortname);
      end;
      backupfile.free;
    end;
    closefile(f);

end;

does that help?
Regards Barry
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1389004
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.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1389005
AAAH - Barry, you were faster...   :-(

But you forgot the NT solution...
0
 
LVL 17

Expert Comment

by:inthe
ID: 1389006
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;

0
 
LVL 17

Expert Comment

by:inthe
ID: 1389007
associated reg key i think is this one:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\PendingFileRenameOperations
 
0
 

Author Comment

by:Cariarer
ID: 1389008
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...
0
 
LVL 17

Expert Comment

by:inthe
ID: 1389009
well you couls try
copyfile(thefilename(pchar),thenewfilename(pchar),ifnewfileexistsalready(boolean) );

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question