Solved

How can I unlock a file without file handle

Posted on 2002-04-05
44
277 Views
Last Modified: 2010-04-02
I have a lockfile that is locked, its creating process is dead.  Is there an API call I can make to unlock a locked file knowing only its path/name?

windows 2000
0
Comment
Question by:scottd1978_2
  • 17
  • 10
  • 6
  • +4
44 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6921663
The only way I know: Kill that process; all file handles will be closed then.
0
 

Author Comment

by:scottd1978_2
ID: 6921681
The process is no longer running.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6921853
Reboot.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6921885
try unlink().  If that doesn't work, then reboot is about all you can do.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6922010
>>try unlink().  If that doesn't work, then reboot is
>>about all you can do.

Using unlink, would delete the file
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6922125
I am listening.  This happens to me with the ClassWizard files -- when VC++ crashes, they get locked and stay locked until a reboot.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6922222
BTW, there _might_ be a solution - using 'ZwUnlockFile()' on the handle, but that requires going through the global handle table... Not an easy task at all.
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6923133
Axter,
He said it was a lockfile.  Should be fine to delete the lockfile when the process is dead.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6923142
>>Should be fine to delete the lockfile when the process >>is dead.
Not if he still want's the contents of that file.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6923145
A small problem with deleting the file:  Some files contain data that might come in handy later on.  I know that Ms WORD randomly deletes files that I have been working on for many hours, so perhaps the programmers ant Microsoft use this approach.  Innovative, but ultimately unsatisfying.

-- Dan
0
 

Author Comment

by:scottd1978_2
ID: 6923172
jkr ... can you tell me where to find more info on this global handle table?  I think you are on the right track.  

Guys ... I do not need any info from this lock file.  I simply need to delete it.  I get an error when trying to delete it through the standard delete api.  The error indicates that the file is locked.
0
 

Author Comment

by:scottd1978_2
ID: 6962678
It seems as if this one is not going to be answered.  Can I get my points back?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6962765
You have an answer:

>>Reboot.

The fact that you don't like the answer is not relevant.
0
 

Author Comment

by:scottd1978_2
ID: 6962786
That answer does not solve my problem.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6962824
Are you saying that the file remains locked even after a reboot?

If so, then there is an application that is opening and locking it.  Fix that program or service so that it doesn't start and the reboot again.  You should be able to delete.


Otherwise, REBOOT the system and then delete the file.

There is no other option.  Sometimes files remain locked even though the locking application is long gone.  Apps don't always terminate it the way we would hope and Windows clearly doesn't always do the right thing.

I'm sure that buried deep down in the NTFS file system driver is some flag that marks the file as locked but finding and fixing it is essentially impossible.  The fix is to reboot.
0
 

Author Comment

by:scottd1978_2
ID: 6963127
I can't very well reboot a citrix server with 100 people working on it whenever this happens.  I need to do this programatically.  

Using the Server Manager that came with the Windows 2000 Professional Resource Kit, this file can be deleted.  I need to use the same API's as this resource kit.

0
 
LVL 32

Expert Comment

by:jhance
ID: 6963178
Did you think that these were trivial details?

Are there any other things you've not said?

Send more points.
0
 

Author Comment

by:scottd1978_2
ID: 6963282
nothing more.  I think this increases the points too.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6963596
Can you rename the file?  Worth a try....

-- Dan
0
 

Author Comment

by:scottd1978_2
ID: 6963612
no
0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6963646
scott,
did you try unlink()?
0
 

Author Comment

by:scottd1978_2
ID: 6963656
Yes, I've tried all suggestions you guys have given.

Thanks.
0
 

Author Comment

by:scottd1978_2
ID: 6963764
Yes, I've tried all suggestions you guys have given.

Thanks.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6963814
Have you tried the NT Native API?

See:

http://www.sysinternals.com/ntw2k/info/ntdll.shtml

and specifically the:

NtDeleteFile()

and possibly the:

NtUnlockFile()

functions?
0
 

Author Comment

by:scottd1978_2
ID: 6963897
I actually bought this book in hopes that it would help me solve this problem.  I've been having a terrible time getting the example code to compile.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6963904
great site jhance!
 
scottd,
if you could figure out which program is keeping the file locked, you might be halfway home.  These utilities promise to reveal that info.

http://www.sysinternals.com/ntw2k/freeware/handle.shtml
http://www.sysinternals.com/ntw2k/freeware/procexp.shtml

-- Dan
0
 
LVL 86

Expert Comment

by:jkr
ID: 6963915
jhance,

'NtUnlockFile()' is in fact 'ZwUnlockFile()'
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6963921
Long shot, but... if the file was locked by a process on a client computer, then perhaps you can delete it from that computer.  -- Dan
0
 

Author Comment

by:scottd1978_2
ID: 6963928
I know which program is locking the file, but the process is dead.
0
 

Author Comment

by:scottd1978_2
ID: 6963932
I know which program is locking the file, but the process is dead.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6966996
So what does that utility program show as the owner of the lock?

So what does that utility program show as the owner of the lock?

-- Dan
0
 

Author Comment

by:scottd1978_2
ID: 6991179
The program shows the handles of processes.  If the process is dead, I'm not going to see its handles.

Give me my points back.
0
 
LVL 11

Expert Comment

by:griessh
ID: 7009369
Dear scottd1978_2

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to

     "refund the points and PAQ at 0 points"

since nobody had a satisfying answer for you.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 

Author Comment

by:scottd1978_2
ID: 7009389
Thanks griessh.
0
 
LVL 32

Expert Comment

by:jhance
ID: 7009458
I object.  The answer here is that this system must be rebooted to correct this problem.  Asker rejects that as not an option but there is no other answer.

The comments here are as helpful as possible but asker has placed impossible conditions on the solution.
0
 

Author Comment

by:scottd1978_2
ID: 7009464
There is obviously a solution to this problem.

Using the Server Manager that came with the Windows 2000 Professional Resource Kit, this file can be
deleted.  

You, jhance, are just bitter because you cannot solve the problem.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7009497
I have to agree that if Service Manager can do it, then a solution other than rebooting does exist.  jkr hints at a potential solution, but I don't think anybody here has solved this problem.

scottd1978_2,
If you up the points to 300, and I will look into how Service Manager does it.  I can't make any promises though.

-- Dan
0
 

Author Comment

by:scottd1978_2
ID: 7009501
I only have 105 more points ... will 200 do?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7009508
>>but I don't think anybody here has solved this problem

Are you sure? :o)
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7009564
>>Are you sure?
The last time I was really certain of something, I lost $300 on KissMeQuick in the fourth race at Santa Anita :) So my track record sux!

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
ID: 7009625
deal?
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 7009859
This link looks promising:

   http://codeguru.earthweb.com/files/ForceDel.shtml

It is a utility designed to do just what you want.  It uses undocumented NT dll functions.  At least one person got it to work to clean up after a network-related problem.  The only way I could think of testing it was have MsWORD open a file, then use FORCEDEL to try to delete it.  Alas, it fails in that case (at least on my Win2K system).

Even though I can't delete it, after running FORCEDEL, iw as able to *rename* it and then create a new file with the original name!.  In your case, it sounds like that would be enough -- even though the system is still locking the file, as far as other programs are concerned, the file no longer exists.

-- Dan
0
 

Author Comment

by:scottd1978_2
ID: 7011086
It works like a charm!  Thanks so much!
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7011427
Ah, points!   The universal lubricant...the opiate of the masses!

-- Dan
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

809 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