Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unlock locked file after systemcrash

Posted on 2007-12-04
14
Medium Priority
?
663 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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 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: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 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: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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

722 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