?
Solved

Unlock locked file after systemcrash

Posted on 2007-12-04
14
Medium Priority
?
669 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:Stef Merlijn
  • 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:Stef Merlijn
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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:Stef Merlijn
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 2000 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
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 2000 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:Stef Merlijn
ID: 20406192
SteveBay: I still can't see where you refer to the locked file.
0
 
LVL 14

Assisted Solution

by:SteveBay
SteveBay earned 2000 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:Stef Merlijn
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:Stef Merlijn
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:Stef Merlijn
ID: 31412573
Thank you very much.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

590 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