• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

How can I unlock a file without file handle

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
scottd1978_2
Asked:
scottd1978_2
  • 17
  • 10
  • 6
  • +4
1 Solution
 
jkrCommented:
The only way I know: Kill that process; all file handles will be closed then.
0
 
scottd1978_2Author Commented:
The process is no longer running.
0
 
jhanceCommented:
Reboot.
0
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.

 
BlackDiamondCommented:
try unlink().  If that doesn't work, then reboot is about all you can do.
0
 
AxterCommented:
>>try unlink().  If that doesn't work, then reboot is
>>about all you can do.

Using unlink, would delete the file
0
 
DanRollinsCommented:
I am listening.  This happens to me with the ClassWizard files -- when VC++ crashes, they get locked and stay locked until a reboot.
0
 
jkrCommented:
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
 
BlackDiamondCommented:
Axter,
He said it was a lockfile.  Should be fine to delete the lockfile when the process is dead.
0
 
AxterCommented:
>>Should be fine to delete the lockfile when the process >>is dead.
Not if he still want's the contents of that file.
0
 
DanRollinsCommented:
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
 
scottd1978_2Author Commented:
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
 
scottd1978_2Author Commented:
It seems as if this one is not going to be answered.  Can I get my points back?
0
 
jhanceCommented:
You have an answer:

>>Reboot.

The fact that you don't like the answer is not relevant.
0
 
scottd1978_2Author Commented:
That answer does not solve my problem.
0
 
jhanceCommented:
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
 
scottd1978_2Author Commented:
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
 
jhanceCommented:
Did you think that these were trivial details?

Are there any other things you've not said?

Send more points.
0
 
scottd1978_2Author Commented:
nothing more.  I think this increases the points too.
0
 
DanRollinsCommented:
Can you rename the file?  Worth a try....

-- Dan
0
 
scottd1978_2Author Commented:
no
0
 
BlackDiamondCommented:
scott,
did you try unlink()?
0
 
scottd1978_2Author Commented:
Yes, I've tried all suggestions you guys have given.

Thanks.
0
 
scottd1978_2Author Commented:
Yes, I've tried all suggestions you guys have given.

Thanks.
0
 
jhanceCommented:
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
 
scottd1978_2Author Commented:
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
 
DanRollinsCommented:
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
 
jkrCommented:
jhance,

'NtUnlockFile()' is in fact 'ZwUnlockFile()'
0
 
DanRollinsCommented:
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
 
scottd1978_2Author Commented:
I know which program is locking the file, but the process is dead.
0
 
scottd1978_2Author Commented:
I know which program is locking the file, but the process is dead.
0
 
DanRollinsCommented:
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
 
scottd1978_2Author Commented:
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
 
griesshCommented:
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
 
scottd1978_2Author Commented:
Thanks griessh.
0
 
jhanceCommented:
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
 
scottd1978_2Author Commented:
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
 
DanRollinsCommented:
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
 
scottd1978_2Author Commented:
I only have 105 more points ... will 200 do?
0
 
jkrCommented:
>>but I don't think anybody here has solved this problem

Are you sure? :o)
0
 
DanRollinsCommented:
>>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
 
griesshCommented:
deal?
0
 
DanRollinsCommented:
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
 
scottd1978_2Author Commented:
It works like a charm!  Thanks so much!
0
 
DanRollinsCommented:
Ah, points!   The universal lubricant...the opiate of the masses!

-- Dan
0
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 17
  • 10
  • 6
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now