I have the following problem:
I need to be able to see when a user has closed a file in another application (e.g. Ms Word). (we use this to store the file in a database).
I'm trying this using madKernel by checking wether a handle remains valid. However when the file is saved in Ms Word (ctrl+s) the handle is no longer valid. To avoid this problem i'm looping through all handles inside the process once more to see if a new handle has been created for the file.
I'm doing this with the following code.
THandleTracker is a thread with 2 private variables:
FProcess - pointer to the IProcess interface
FFileLoc - string to the location of the file that is being checked
Code: Select all
procedure THandleTracker.Execute;
var
lvHandle : IHandle;
function ValidProcess : Boolean;
begin
Result := Assigned(FProcess) and FProcess.IsStillValid;
end;
function ValidHandle : Boolean;
begin
Result := Assigned(lvHandle) and lvHandle.isstillvalid;
end;
function NewValidHandle : Boolean;
begin
//GetFileHandleFromProcess retrieves a new handle inside the FProcess (interface IProcess) using the filelocation.
lvHandle := GetFileHandleFromProcess(FProcess, FFileLoc);
Result := ValidHandle;
end;
begin
lvHandle := nil;
while (not Terminated) and ValidProcess and (ValidHandle or NewValidHandle)
do lvHandle.WaitFor( 100 );
if not Terminated
then Synchronize( DoHandleDestroyed );
end;
- Is there a better way to check when a file handle is released (and no new handle to the file is created by the process)?
- Can i retrieve the file handle by any other means besides looping through all handles inside the process?
- Is it possible the new handle is not created "immediately". i.o.w could it be that my DoHandleDestroyed method is called when MS Word is still creating a new handle to the file?
I hope i explained my situation well, i've literally invested hours into this project and must now decide wether its worth investing more time or take another route (though this is my best idea so far )[/code]