Solved

How can I unlock a file without file handle

Posted on 2002-04-05
44
272 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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