Author: Jérôme Tremblay
Sometimes I need to handle files that are used by windows before they are loaded in
the boot process, like a DLL or a VxD for example. How do I do that?
Answer:
Windows NT have a function called MoveFileEx that deletes files at reboot if used
with the MOVEFILE_DELAY_UNTIL_REBOOT flag. Unfortunately, Windows 9x doesn't
support this flag. So what do we do?
Every time you reboot, windows look for a file called WININIT.INI in the Windows
directory. This file can contains Delete / Rename / Copy directives that will be
excuted before anything is loaded (or almost). You can place commands in the
[Rename] section using the syntax DESTINATION=SOURCE. If Destination is NUL, then
the file will be deleted. Filenames and paths must use SHORT FILENAMES (because
this file is processed before long filenames support is even loaded).
Please note that contrary to the example found in win32.hlp, you cannot use
WritePrivateProfileString() or TIniFile to access this file because there might be
duplicates values. If there is already one NUL value, TIniFile would overwrite it
instead of creating a new one. So you better use TStringList instead.
Here are some example entries:
[rename]
NUL=C:\TEMP.TXT
NUL=C:\TEMP2.TXT
C:\NEW_DIR\EXISTING.TXT=C:\EXISTING.TXT
C:\NEW_DIR\NEWNAME.TXT=C:\OLDNAME.TXT
C:\EXISTING.TXT=C:\TEMP\NEWFILE.TXT
Below is the function DeleteLater that will just add NUL=Filename to wininit.ini,
create the file if it doesn't exist, and also create the section if needed.
1 procedure DeleteLater(Filename: string);
2 var
3 Wininit: string;
4 Buffer: array[0..MAX_PATH] of char;
5 I, J: integer;
6 Ini: TStringList;
7 begin
8 FillChar(Buffer, SizeOf(Buffer), 0);
9 GetWindowsDirectory(Buffer, SizeOf(Buffer));
10 Wininit := IncludeTrailingBackslash(Buffer) + 'Wininit.ini';
11
12 Ini := TStringList.Create;
13 try
14 if FileExists(Wininit) then
15 Ini.LoadFromFile(Wininit);
16 for I := 0 to Ini.Count - 1 do
17 Ini[I] := Uppercase(Ini[I]);
18
19 J := Ini.IndexOf('[RENAME]');
20 if J = -1 then
21 begin
22 Ini.Add('[Rename]');
23 J := 0;
24 end;
25 FillChar(Buffer, SizeOf(Buffer), 0);
26 GetShortPathName(PChar(Filename), Buffer, SizeOf(Buffer));
27 Ini.Insert(J + 1, 'NUL=' + Buffer);
28 Ini.SaveToFile(Wininit);
29 finally
30 Ini.Free;
31 end;
32 end;
|