Jump to content
Tuts 4 You

Handles of a given file


Recommended Posts

I am designing a delete function for a file manager i am coding however, i want to be able to delete a file even if you get that stupid windows "cant not delete due to blah" message. programs like unlocker. http://ccollomb.free.fr/unlocker/ search for all open handles for a given file and close them how is this acheived? i know after all handles are closed the obvious DeleteFileA is callled to delete the file.

example, file1.txt has handle 1 and handle 2 open.

search and find the handes CloseHandle(1) close handle(2)


Delphi prefered but would settle with other langs and convert my self.


Link to comment


1- Enumerate all processes opened handles via NtQuerySystemInformation .

2- Query all returned handles and check if it belogs to the victim file .

3- Close the victim file handles via NtDuplicateObject + DUPLICATE_CLOSE_SOURCE .

4- Delete the file .

Or :

You can modify the filesystem directly and delete the file entry .

Good luck,


Link to comment


Every file is represented as an entry in the filesystem on physical disk, the referred method is achieved by 1st knowing the filesystem type of the partition (NTFS/FAT32/FAT12 .. etc) .

Second you need to walk through the files entries and manipulate it by directly writing data to the disk and mark the victim file entry as "deleted", this depends on the filesystem type .

You can start from here :

Good luck,


Link to comment
  • 4 weeks later...

I once did something similar using a GetFileSize loop, but you will need to know the size of the file you are trying to obtain the handle for.

I am not sure if this will work, I am not even sure if my code will work but maybe you can use the idea. I think can convert the code to delphi easily enough.

int i, filesize;
int expected_file_size=1234; // The size of he file you are looking forfor (i=0; i < 65536; i++)
filesize = GetFileSize(i, NULL);
if (filesize == expected_file_size)
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...