Article View: comp.lang.pascal.delphi.misc
Article #48Re: How2 delete a running EXE when quitting (as Installshield do)?
From: hz37
Date: Sun, 10 Jan 1999 00:00
Date: Sun, 10 Jan 1999 00:00
143 lines
7108 bytes
7108 bytes
Jean Fages wrote us:
>How can I delete my running EXE file when I'm closing my application?
>I'd like to do something like InstallShield does when it installs a program
>in your machine.
You can't, but there is a way cool trick to make Windows do it for you.
I'll leave you with the Knowledge Base article that discusses how to do it.
Hens Zimmerman
PSS ID Number: Q140570
---------------------------------------------------------------------
The information in this article applies to:
- Microsoft Win32 Application Programming Interface included with:
- Microsoft Windows NT versions 3.1, 3.5, 3.51
- Microsoft Windows 95 version 4.0
---------------------------------------------------------------------
SUMMARY
=======
Sometimes Win32 applications need to delete, rename, or move files that are
currently being used by the system. One common example is that setup
programs need to remove themselves from the user's hard disk when they are
finished setting up a software package. Sometimes, they also need to move
device drivers that are currently being used by the system. Applications
need help from the operating system to delete or move these files.
Windows 95 and Windows NT each provide a unique method for helping
applications to remove, replace, or rename files and directories that are
in use. Although the two platforms differ in how they implement these
methods, both share an overall strategy where the application specifies
which files to process, and the system processes them when it reboots. This
article explains how applications can use the method provided by each
Windows platform.
MORE INFORMATION
================
Moving Files in Windows NT
--------------------------
Win32-based applications running in Windows NT should use MoveFileEx() with
the MOVEFILE_DELAY_UNTIL_REBOOT flag to move, replace, or delete files and
directories currently being used. The next time the system is rebooted, the
Windows NT bootup program will move, replace, or delete the specified files
and directories.
To move or replace a file or directory that is in use, an application must
specify both a source and destination path on the same volume (for example,
drive C:). If the destination path is an existing file, it will be
overwritten. If the destination path is an existing directory, it will not
be overwritten and both the source and destination paths will remain
unchanged. Here is an example call to move or replace a file or move a
directory:
// Move szSrcFile to szDstFile next time system is rebooted
MoveFileEx(szSrcFile, szDstFile, MOVEFILE_DELAY_UNTIL_REBOOT);
To delete a file or directory, the application must set the destination
path to NULL. If the source path is a directory, it will be removed only if
it is empty. Note that if you must use MoveFileEx() to remove files from a
directory, you must reboot the computer before you can call MoveFileEx() to
remove the directory. Here is an example of how to delete a file or empty a
directory:
// Delete szSrcFile next time system is rebooted
MoveFileEx(szSrcFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
Moving Files in Windows 95
--------------------------
Windows 95 does not implement MoveFileEx() but does provide an alternate
way for all Win32-based, 16-bit Windows-based, and MS-DOS-based
applications to move, replace, or delete files (but not directories) that
are currently in use. This capability is implemented through the [rename]
section of a file named Wininit.ini. If Wininit.ini is present in the
Windows directory, Wininit.exe processes it when the system boots. Once
Wininit.ini has been processed, Wininit.exe renames it to Wininit.bak.
The syntax of the [rename] section is:
DestinationFileName=SourceFileName
DestinationFileName and SourceFileName must be short (8.3) file names
because Wininit.ini is processed before the protected mode disk system is
loaded, and long file names are only available when the protected mode disk
system is running. Destination and source files specified in Wininit.ini
with long file names are ignored.
The [rename] section can have multiple lines with one file per line. To
delete a file, specify NUL as the DestinationFileName. Here are some
example entries:
[rename]
NUL=C:\TEMP.TXT
C:\NEW_DIR\EXISTING.TXT=C:\EXISTING.TXT
C:\NEW_DIR\NEWNAME.TXT=C:\OLDNAME.TXT
C:\EXISTING.TXT=C:\TEMP\NEWFILE.TXT
The first line causes Temp.txt to be deleted. The second causes
Existing.txt to be moved to a new directory. The third causes Oldname.txt
to be moved and renamed. The fourth causes an existing file to be
overwritten by Newfile.txt.
Applications should not use WritePrivateProfileString() to write entries to
the [rename] section because there can be multiple lines with the same
DestinationFileName, especially if DestinationFileName is "NUL." Instead,
they should add entries by parsing Wininit.ini and appending the entries to
the end of the [rename] section.
NOTE: Always use a case-insensitive search to parse Wininit.ini because the
title of the [rename] section and the file names inside it may have any
combination of uppercase and lowercase letters.
Applications that use Wininit.ini should check for its existence in the
Windows directory. If Wininit.ini is present, then another application has
written to it since the system was last restarted. Therefore, the
application should open it and add entries to the [rename] section. If
Wininit.ini isn't present, the application should create it and add to the
[rename] section. Doing so ensures that entries from other applications
won't be deleted accidentally by your application.
To undo a file rename operation before the system is rebooted, you must
remove the corresponding line from the [rename] section of the Wininit.ini
file.
Additional reference words: 3.51 4.00 update install setup
KBCategory: kbprg kbhowto
KBSubcategory: BseFileio
Technical Details
Message-ID:
<36989c9d.1744783@news0.xs4all.nl>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<77985k$lqn$1@news3.isdnet.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1091e5,c1e85533501ee863 X-Google-Attributes: gid1091e5,public X-Google-Thread: f5ca7,c1e85533501ee863 X-Google-Attributes: gidf5ca7,public X-Google-Thread: 10baee,c1e85533501ee863 X-Google-Attributes: gid10baee,public X-Google-Thread: 103c8d,c1e85533501ee863 X-Google-Attributes: gid103c8d,public From: hz37@xs4all.nl Subject: Re: How2 delete a running EXE when quitting (as Installshield do)? Date: 1999/01/10 Message-ID: <36989c9d.1744783@news0.xs4all.nl> X-Deja-AN: 430862793 X-NNTP-Posting-Host: dc2-modem1933.dial.xs4all.nl [194.109.135.141] Content-Transfer-Encoding: 7bit Sender: hz37@dc2-modem1933.dial.xs4all.nl References: <77985k$lqn$1@news3.isdnet.net> X-XS4ALL-Date: Sun, 10 Jan 1999 13:59:41 CET X-XS4ALL-User: hz37 Content-Type: text/plain; charset=us-ascii Organization: ___NO CARRIER Mime-Version: 1.0 Reply-To: hens@gmx.net Newsgroups: comp.lang.pascal.delphi.misc,borland.public.cppbuilder.language,borland.public.cppbuilder.winapi,borland.public.delphi.winapi