Solved

Checking whether a file is already in use or not.

Posted on 2003-12-09
5
213 Views
Last Modified: 2013-11-20
Hi,

I am trying to write a rotuine, which will check whether a given file is already in use or not.

For writing this, I am using the routine, OpenFile with OF_SHARE_EXCLUSIVE mode.

When OpenFile is used on a file which is already opened by any other application, then it is returning error message (as it should) which is indicating that the file is already in use.

This seems to work fine only with Binary files.

when a text based file ( with extensions .txt, .cpp, .c and etc) , is opened by another applicaiton (for e.g. if file.c is opened in MSDEV) OpenFile is still able to get access to the file in OF_SHARE_EXCLUSIVE mode. Even from windows explorer also, the text based files opened in any other application can be deleted or modified without any problem.

Can someone tell me a way of checking both Text Based files and Binary files which are already in use ?

Thanks!
Gangisetti

0
Comment
Question by:gangisetti
5 Comments
 
LVL 9

Accepted Solution

by:
Priyesh earned 100 total points
Comment Utility
Try this:

      HANDLE hFile = CreateFile("c:\\1.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
      if(hFile == INVALID_HANDLE_VALUE)
      {
            LPVOID lpMsgBuf;
            if (!FormatMessage(
                  FORMAT_MESSAGE_ALLOCATE_BUFFER |
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL,
                  GetLastError(),
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                  (LPTSTR) &lpMsgBuf,
                  0,
                  NULL ))
            {
               // Handle the error.
               return TRUE ;
            }

            // Process any inserts in lpMsgBuf.
            // ...

            // Display the string.
            AfxMessageBox((LPCTSTR)lpMsgBuf) ;

            // Free the buffer.
            LocalFree( lpMsgBuf ) ;
      }

If any open handles exists to the above file, there will be an OS error.  This is true for all files. Editors might just read the file into memory and then close handle, that's why you could delete the file even when it is open in an editor.

If this does not work for you, you can look into file/ directory watch (NT and higher)
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
>> when a text based file ( with extensions .txt, .cpp, .c and etc) , is opened by another applicaiton (for e.g. if file.c is opened in MSDEV) OpenFile is still able to get access to the file in OF_SHARE_EXCLUSIVE mode.

This happens because Visual Studio doesn't keep opened file handle. It opens file only to load it into memory and to save it. So, your program is OK.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now