Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

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

0
Stef Merlijn
Asked:
Stef Merlijn
  • 6
  • 6
  • 2
3 Solutions
 
JohnjcesCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
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
 
JohnjcesCommented:
I fully understand and thanks!

I am monitoring this Q as I am curious too!

John
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SteveBayCommented:
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
 
SteveBayCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
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
 
SteveBayCommented:
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
 
SteveBayCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
SteveBay: I still can't see where you refer to the locked file.
0
 
SteveBayCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
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
 
SteveBayCommented:
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
 
Stef MerlijnDeveloperAuthor Commented:
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
 
Stef MerlijnDeveloperAuthor Commented:
Thank you very much.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now