Link to home
Start Free TrialLog in
Avatar of Stef Merlijn
Stef MerlijnFlag for Netherlands

asked on

Unlock locked file after systemcrash

Hi,

I want to build a very basic File-Unlock application that will unlock a specific file that remains locked after unexpected closing of my application (f.e. after a systemcrash). It should support commandline possibilities.

Here is why:
To make sure only one user is allowed to have access to my application I use a locked file method (see code part).

But when the system crashes or my app runs into a unexpected error, this file might remain locked. Therefor I want to allow a user to unlock the file, so he can get access to the application again.

{Two application that do what I want are Unpacker and FileAssassin. But these applications must be installed on the enduser's computer, therefor I would rather build it into my own application. I you know of any commandprompt application that will do it all for me, than that is fine for me too.}

Can anybody give me a small sample on how to get this working?
// Added to the source of the application
var vFileStream : TFileStream;
 
begin
  try
    vFileStream := TFileStream.Create( ExtractFilePath(Application.ExeName) + 'InUse.fil',fmCreate or fmShareExclusive);
  except
    MessageDlg('Program is already in use.', mtInformation, [mbOK], 0);
    Exit;
  end;
  Application.Initialize;
  ETC....
end;

Open in new window

Avatar of Johnjces
Johnjces
Flag of United States of America image

DelphiWizard,

In your original post regarding this I added my 2 cents about if a program crashed would this file still be locked... and asked the author of the accepted answer this question as well.

My question is; did that occur? A program/system crash? The file is now locked?

I am just curious about what you may have found and what Windows may do in such a situation.

Thanks!

John

Avatar of Stef Merlijn

ASKER

Hi John: Well I don't know if it will actually happen. Windows might still lock the file after some crash?
The thing is: I can't effort not to have a workaround for my end-users when they are confronted with such a situation.
I fully understand and thanks!

I am monitoring this Q as I am curious too!

John
Avatar of SteveBay
If the file is locked on a server perhaps you can release it  using something like this command line:
Net User <username> /Active:No.
This should release any file locks.
Upon further review... It looks as though
NET Session [\\computername] /DELETE /Y
is the command for you.
http://www.geocities.com/rick_lively/MANUALS/COMMANDS/N/NETSESSI.HTM
Hi Stevebay: The file is locked on a server or on any computer in the network (dependend of choices during the initial installation).
The solution you give is using a <username>. This asumes the username is available. But maybe one out of 5 users had locked. And how would my application know which usernames to check?
Also I don't want to release all file lock, but only for my own file:
ExtractFilePath(Application.ExeName) + 'InUse.fil'

If your solution can handle this could you than please clarify it's usage.
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SteveBay: I still can't see where you refer to the locked file.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oke I understand it now. I will do some testing with it.
Can you tell me which situations aren't coverred by this solution? Just to have an idea on the risc and find a solution for it if really required.
I honestly dont think you are going to run into this very often. The file will always be unlocked when the application closes normally. If the file were lock by an application or system crash, logout/login would always unlock the file. If the user would not or could not logout/login thats when running this batch file will force the issue by ending their session. The real problem with this solution is that the batch file that clears the users session needs to be run on the system where the file resides. However, if it is it will always take clear the lock. This may not be the idea solution; however the key to clearing an unwanted file is to delete the session. Im sure there are ways this could be done remotely thought API calls with administrator privileges, though I am not immediately familiar with the specifics.
I tested the solution in a way that might not reflect a real situation, as I opened the application on one computer and kept it open while trying to unlock the file (by running the batchfile). Basically that is what happens when the lock is still on after the application has crashed (I think - maybe I'm wrong).
The application remained locked. In a way this is even better, because when my application is still in use, it shouldn't be unlocked anyway.
So how to create a testcase where the 'inuse.fil' remains locked and the application is closed?
Thank you very much.