I would like to receive some sort of notification when a file closes (If I
have shellex'ed the file, it will open in a new process right)...
Basically, I want to be able to delete the file when the application
releases that file so the way I thought I'd do it is as follows:
Get the file Handle from the other process by querying windows somehow... (I
don't know how to do this, can this be done)
WaitForMultiple...
delete when applicable...
Any help would be great
Thanks
BCV
It might not open a new process. For example, if you ShellExecute a
Word document, and Word is already running, the existing instance will
load the new document.
>Basically, I want to be able to delete the file when the application
>releases that file
What if the application does this:
Opens the file, reads the contents into memory, closes the file.
User works on the application changing the document and eventually
saves it.
File is opened for writing, contents written, file closed.
If you delete the file after the first close, some applications will
be confused when they come to save the document.
Now, is your requirement quite as clear cut, or do you only have to
handle 1 very specific situation?
Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Basically, I need to monitor files in use and delete them when they are no
longer in use...
The file is opened (shell executed) from my app, and then opened say in
word, excel, or whatever the registered application is.
I need then to delete the file when the application is finished with it.
So basically, there are 2 ways to do this...
I can periodically attempt to exclusively open then file. If I get an error
then I know the file is still in use. The downside of this is a waste of
resources polling the file.
-OR-
I get the file handle the say Word hsa to the file, spawn a thread, an in
that thread, waitformultipleobjects on that file handle, (plus some others
for notifications etc)... and then delete the file when the handle is
release. All of which I can do and it relatively simple, except getting the
file handle from Word.
Thanks
BCV
"David Lowndes" <dav...@mvps.org> wrote in message
news:q1h9svcdn4ko161lo...@4ax.com...
OK, but see my other remarks - different application handle files in
wildly different ways.
I've recently gone over the same ground myself, and there's no single
method you can use. For handling Word and Excel, I found the best way
was to monitor the ROT (running object table).
Monitoring by opening the file (or the file handle - if you can even
achieve that) won't work correctly for the scenario I mentioned.
a) I use a separate thread for the "client" app that uses my temp files. The
thread starts and then waits for the process to finish, and once the child
process finishes in it, I delete the file. This means that if a different
document is loaded in Word, I will not notice. But 90% of the times users
just read the temp file created by me...
b) When my app is being closed before all child threads are finished, I warn
that temp files will remain if the other app is not closed before leaving.
Works like charm for my purposes, and I guess Outlook does the same (with
mail attachments).
Christian
"David Lowndes" <dav...@mvps.org> schrieb im Newsbeitrag
news:5gudsvssk1j3dc3su...@4ax.com...
Just to let you know what I currently do...(This may help you a little)
I spawn a new process which monitors the list of files (and is self
contained and will terminate when either all files are closed or the system
shuts down)
The process attempts to get an exclusive lock on each file which is create
because this means that if it can, the file is not in use anywhere...
The down side is the amount of resources I have to use to keep polling the
file (even if it simply sleeps most of the time)...
Basic logic
Loop
{
sleep
checkfiles
}
Idealy, I would like to only break from sleeping when the handle is
released, a nice waitformultipleobjects loop... But I simply cannot find out
how to get the file handle of a file that is already open in another
process...
ie.
WaitForMultiple(FileHandles[] + TerminationMutex)
{
KillReleaseHandle
Waitformultiple...
}
"Christian Kaiser" <bc...@gmx.de> wrote in message
news:O6VvezYt...@tk2msftngp13.phx.gbl...
My understanding is that a handle in one process is not accessible to
another process. The only way handles can be passed between processes is by
inheritence. But that doesn't happen here because Word (or whatever) opens
the file after it has been spawned. Even if you could get the handle's
value, it would not be of any use in a different process. After all, a
handle is just a pointer to an opaque data structure. ie, it points
somewhere in the processes address space, and would point somewhere invalid
if used in a different address space.
I don't think you can do what you want in this way. Maybe you could watch
for a change in the file's attributes (like the last modified date) and do
what you want on change.
Loz.
I think you'll find that some applications play around with the
modified timestamp in a way that makes it an unreliable mechanism - I
seem to recall that Excel changes the modified timestamp when it opens
a (non-read only) file, and then resets it when the user closes it
without making changes!
You'll also fall foul of Word already being running (either a visible
instance of Word, or if someone's using Outlook with Word as their
editor). In this situation, the shell doesn't start another process!
In my experience there is no foolproof single way of monitoring when a
user has finished using a file with an application. The best solution
I could recommend is one where you know the characteristics of the
application whose document you're invoking, and monitor it with
whichever method is applicable to that application. Monitoring the ROT
is the best method for the main Office application - IMHO.