Solved

Unlock locked file after systemcrash

Posted on 2007-12-04
14
648 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:Delphiwizard
  • 6
  • 6
  • 2
14 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 20402825
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

0
 

Author Comment

by:Delphiwizard
ID: 20403701
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.
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20403718
I fully understand and thanks!

I am monitoring this Q as I am curious too!

John
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 20403880
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.
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 20403983
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
0
 

Author Comment

by:Delphiwizard
ID: 20404045
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.
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 20404399
Ok.. this may be a bit of a kludge and I know it does not address all of the issues but maybe this will give you some ideas.

You could have the computer that creates the lock file also create a batch file in the same location. You could call the batch file something like ReleaseApplicationLock.bat The batch file contains the commend line necessary to disconnect the session that created the lock file. It would look something like this

NET Session \\<computername> /DELETE /Y

Then when a lock occurs, just run the batch file.

function 	GetMachineName : string;

var buffer:array[0..20] of char;

  size:Dword;

begin

	fillchar(buffer,0,sizeof(buffer));        	//just for sure

	size:=sizeof(buffer);                     	//save buffersize

	GetComputerName(buffer,size);            	//fill buffer, bytes read saved in size

	Result := trim(copy(strpas(buffer),1,size));	//convert to pascal style
 

end
 
 

procedure  CreateLockRelease

var sl : TStringlist;

begin

	sl := TStringList.Create;

	sl.add(Net Session \\ + GetMachineName + /DELETE /Y);

	sl.SaveToFile(ReleaseApplicationLock.bat); // may need to include the full path

	sl.free;

end;

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 500 total points
ID: 20404435
Here is some slightly cleaner code:
procedure  CreateLockRelease ;

var sl : TStringlist;

begin

	sl := TStringList.Create;

	sl.add('Net Session \\' + GetMachineName +' /DELETE /Y');

	sl.SaveToFile('ReleaseApplicationLock.bat'); // may need to include the full path

	sl.free;

end;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 20406192
SteveBay: I still can't see where you refer to the locked file.
0
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 500 total points
ID: 20406262
There is no need to reference the file. When you discontinue the session that holds the lock all files held open by the session will be unlocked.
When you run the batch file created by that machine you will end that machines session and unlock all files held by it.
0
 

Author Comment

by:Delphiwizard
ID: 20406322
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.
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 20408000
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.
0
 

Author Comment

by:Delphiwizard
ID: 20409943
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?
0
 

Author Closing Comment

by:Delphiwizard
ID: 31412573
Thank you very much.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

20 Experts available now in Live!

Get 1:1 Help Now