Link to home
Start Free TrialLog in
Avatar of scottd1978_2
scottd1978_2

asked on

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
Avatar of jkr
jkr
Flag of Germany image

The only way I know: Kill that process; all file handles will be closed then.
Avatar of scottd1978_2
scottd1978_2

ASKER

The process is no longer running.
Reboot.
try unlink().  If that doesn't work, then reboot is about all you can do.
>>try unlink().  If that doesn't work, then reboot is
>>about all you can do.

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

>>Reboot.

The fact that you don't like the answer is not relevant.
That answer does not solve my problem.
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.
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.

Did you think that these were trivial details?

Are there any other things you've not said?

Send more points.
nothing more.  I think this increases the points too.
Can you rename the file?  Worth a try....

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

Thanks.
Yes, I've tried all suggestions you guys have given.

Thanks.
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?
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.
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
jhance,

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

Are you sure? :o)
>>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
deal?
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It works like a charm!  Thanks so much!
Ah, points!   The universal lubricant...the opiate of the masses!

-- Dan