CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: DeleteFile Hook

  1. #1
    Join Date
    Mar 2009
    Posts
    102

    DeleteFile Hook

    Hello All,

    I need to intercept everytime user use delete,shift-delete or use DEL command from command promt, I need to know what API get called when we use DELETE, SHIFT+DELETE and DEL command, I'll be very much obliged if anyone can redirect me to how to intercept-Hook those calls..

    Looking forward to see members reply, and i'm using VC 6.0 and WIN DDK 7.1.


    Thanks all

  2. #2
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: DeleteFile Hook

    You obviously have two requests: 1. Delete and SHIFT+DELETE are keystrokes that you can intercept using SetWindowsHookEx with WH_KEYBOARD or WH_KEYBOARD_LL hook. As for the DEL command (I'm assuming you mean that command from a Command Prompt), you can watch the folder/file of interest with the FindFirstChangeNotification/FindNextChangeNotification APIs for file removal.

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: DeleteFile Hook

    I don't think he is wanting to hook the keyboard. He wants to intercept calls to DeleteFile.

  4. #4
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Quote Originally Posted by ahmd View Post
    You obviously have two requests: 1. Delete and SHIFT+DELETE are keystrokes that you can intercept using SetWindowsHookEx with WH_KEYBOARD or WH_KEYBOARD_LL hook. As for the DEL command (I'm assuming you mean that command from a Command Prompt), you can watch the folder/file of interest with the FindFirstChangeNotification/FindNextChangeNotification APIs for file removal.


    Thanks ahmd and Chris,

    My requirement is that whenever any file get deleted i should get the message first and based on some checks on user i decide whether the user can delete the file or not, Now the check part is not in question, But as far as my knowledge we can delete any file using

    1. DELETE KEY
    2. SHIFT-DELETE KEY
    3. DELETE FROM MOUSE
    4. DEL COMMAND FROM COMMAND PROMPT
    5. DRAG THE FILE TO RECYCLE BIN

    Quote Originally Posted by ahmd View Post
    As for the DEL command (I'm assuming you mean that command from a Command Prompt), you can watch the folder/file of interest with the FindFirstChangeNotification/FindNextChangeNotification APIs for file removal.
    I think i don't get this point , Won't i receive any call instead of watching every single file in particular PC when DEL command from command prompt is fired?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: DeleteFile Hook

    And what if someone directly calls DeleteFile API?
    Victor Nijegorodov

  6. #6
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Quote Originally Posted by VictorN View Post
    And what if someone directly calls DeleteFile API?
    Lol, I don't know, I was thinking that pressing delete will invoke DeleteFile.. I might be thinking vague but how to do it?

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: DeleteFile Hook

    Quote Originally Posted by itsmeash View Post
    Lol, I don't know, I was thinking that pressing delete will invoke DeleteFile.. I might be thinking vague but how to do it?
    It may invoke SHFileOperation instead.


    Quote Originally Posted by itsmeash View Post
    .. I might be thinking vague but how to do it?
    To do what? Hook the SHFileOperation/DeleteFile calls? I don't know. Try to google...
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Quote Originally Posted by VictorN View Post
    It may invoke SHFileOperation instead.


    To do what? Hook the SHFileOperation/DeleteFile calls? I don't know. Try to google...
    Ok thanks..

  9. #9
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Say if i able to know that DEL key has been pressed, what logic should be there to override it? I mean how can i prevent it to deletion unless i performed some checks.. I hope you get my point..

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: DeleteFile Hook

    No, I didn't.
    Why do you think that pressing DEL key always generates the delete file operation?
    Victor Nijegorodov

  11. #11
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Quote Originally Posted by VictorN View Post
    No, I didn't.
    Why do you think that pressing DEL key always generates the delete file operation?
    I'm just guessing, Tried Google also, there are many scenarios when the file can be deleted, Right now i am not thinking of other way but DELETE key..

    So i guess it can be bifurcated in two parts..

    1. Intercept the delete button.
    2. Override the operation.

    I completed the part where someone press the DELETE button and i get the event and for checking i'm writing log, But i'm stuck at second part.

  12. #12
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: DeleteFile Hook

    Your approach of trying to find a base API responsible for deletion of a file is totally wrong. DeleteFile is far from the bottom of this hierarchy. Most certainly it is done by a kernel device driver responsible for the file system I/O operations. And even if you delve into the kernel DDK and find out how to intercept calls to it, there'll be no guarantee that in some version of Windows it won't be done differently.

    The way you need to address restrictions on a file deletion by a certain user is by employing security descriptors that are available on all Windows-NT based operating systems and the NTFS file system. Read here and here for more details.
    Last edited by ahmd; August 6th, 2010 at 01:53 PM.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: DeleteFile Hook

    Quote Originally Posted by ahmd View Post
    The way you need to address restrictions on a file deletion by a certain user is by employing security descriptors that are available on all Windows-NT based operating systems and the NTFS file system. Read here and here for more details.
    Yep. This is the way to do it. That way, you change it in one place and not have to worry about covering all 100 different edge cases to prevent the user from deleting a file.

  14. #14
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Quote Originally Posted by ahmd View Post
    Your approach of trying to find a base API responsible for deletion of a file is totally wrong. DeleteFile is far from the bottom of this hierarchy. Most certainly it is done by a kernel device driver responsible for the file system I/O operations. And even if you delve into the kernel DDK and find out how to intercept calls to it, there'll be no guarantee that in some version of Windows it won't be done differently.

    The way you need to address restrictions on a file deletion by a certain user is by employing security descriptors that are available on all Windows-NT based operating systems and the NTFS file system. Read here and here for more details.
    Thanks Ahmd,

    I am not concern about the different number of OS, The application will be used in win 2000 or 2003 only , So security descriptors is what i need..

    Well lot's of R&D then.. Anyways thanks for the information, I'm sure i'll be able to extract useful information from that

  15. #15
    Join Date
    Mar 2009
    Posts
    102

    Re: DeleteFile Hook

    Quote Originally Posted by ahmd View Post
    Most certainly it is done by a kernel device driver responsible for the file system I/O operations. And even if you delve into the kernel DDK and find out how to intercept calls to it, there'll be no guarantee that in some version of Windows it won't be done differently.
    Just being curious, How to do it? What's the approach to peek into file I/O operations, There's certainly not an issue of different OS as i've to run in win 2000 or 2003 only.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured