Checking if the file is in use and by which application?
Asked Answered
I

2

7

Trying to use the below mentioned approach to get more details about the locked file.

Is file in use

function GetFileInUseInfo(const FileName : WideString) : IFileIsInUse;
var
  ROT : IRunningObjectTable;
  mFile, enumIndex, Prefix : IMoniker;
  enumMoniker : IEnumMoniker;
  MonikerType : LongInt;
  unkInt  : IInterface;
begin
  result := nil;

  OleCheck(GetRunningObjectTable(0, ROT));
  OleCheck(CreateFileMoniker(PWideChar(FileName), mFile));

  OleCheck(ROT.EnumRunning(enumMoniker));

  while (enumMoniker.Next(1, enumIndex, nil) = S_OK) do
  begin
    OleCheck(enumIndex.IsSystemMoniker(MonikerType));
    if MonikerType = MKSYS_FILEMONIKER then
    begin
      if Succeeded(mFile.CommonPrefixWith(enumIndex, Prefix)) and
         (mFile.IsEqual(Prefix) = S_OK) then
      begin
       if Succeeded(ROT.GetObject(enumIndex, unkInt)) then
        begin
          if Succeeded(unkInt.QueryInterface(IID_IFileIsInUse, result)) then
          begin
            result := unkInt as IFileIsInUse;
            exit;
          end;
        end;
      end;
    end;
  end;
end;

But the call to

unkInt.QueryInterface(IID_IFileIsInUse, result) 

always returns E_NOINTERFACE.

Platform: Windows 7 32 bit-OS, opening word files and .msg files.

Checked opening files from the explorer and trying to delete. It shows proper details about the application in which the file is opened. In my application, I am try to display the information about application in which the file is opened. But when trying to cast the pointer to IFileIsInUse interface, QueryInterface calls fails with return code E_NOINTERFACE which means the object in ROT does not implement IFileIsInUse. AFASIK, MS Office files implements IFileIsInUse

Any idea what is wrong here?

Involuted answered 11/2, 2013 at 14:55 Comment(1)
Similar question at #2480233, but for .NET. Might be a help in answering your own question.Kevel
R
7

In fact your code works fine. The problem is that the programs you are testing against really do not implement IFileIsInUse. When the system returns E_NOINTERFACE it is accurate. The interface is not implemented.

I tested this with the File Is In Use Sample from the SDK. Files that are added to the ROT by that application, which does implement IFileIsInUse, were picked up by your code. On the other hand, files opened by Acrobat 8 and Word 2010 were not.

The conclusion that I draw from this is that IFileIsInUse is a fine idea in principle, but not much use if applications don't support it. And it appears that there are major applications that do not.

It is clear that you will need to use one or more of the other mechanisms to detect which application has a file locked when you find that IFileIsInUse is not implemented.

Russianize answered 11/2, 2013 at 18:2 Comment(1)
Yes. Thank you. A fallback approach is used to fetch the data when object does not implement IFileIsInUse.Involuted
J
0

SysInternals Process Explorer worked for me to delete a locked .msg file that was causing system problems like locking up the desktop.

  • Run Process Explorer, use the Find menu,
  • enter the full path file name,
  • hit Search.

For deleting a locked file, I opened a cmd window and tried to del the locked file, but the delete hung on the lock.

Then I used Process Explorer to restart the process holding the lock - Explorer.exe.

The del then completed successfully.

Joellajoelle answered 15/11, 2019 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.