Link to home
Start Free TrialLog in
Avatar of iddo_shoham
iddo_shoham

asked on

Detecting and preventing writing certain files (i.e .doc) to the hdd using c++.

I am writing an application using c++ and I have 2 questions.
a. How can I get a notification that a user is trying to write a file to the hard drive (any file and a specific file extension) ?
b. After detecting an attempt to write the file to the disk how can I prevent the operation ?
thanks.
Avatar of Infinity08
Infinity08
Flag of Belgium image

This is certainly not a trivial thing to do.

First of all, what operating system are we talking about ?

Why do you want to use it ? There might be a better way to achieve what you want.

Usually, you can write-protect drives, directories, files for certain users. Is that sufficient ?
>>>> any file and a specific file extension
Your approach will not work even if you get the wished notification. The problem is that when renaming a file you won't get a notification cause it is a change of the directory rather than a change of the file. Also moving a file from one directory to another will give no write action if the directroies were located at the same disk.

If it really makes sense to prevent people from creating files of a specific type, you should remove the programs where they can store such files or give them read-only access for all folders where they shouldn't write to.

Regards, Alex
Avatar of kode99
kode99

In order to do this sort of thing you will need to 'hook' into windows itself.  What this does is provide a notification which executes a callback function,  this is the 'hooked' into the OS.  So every time the OS performs a certain task your callback function will be executed.  It's kind of like writing a custom event to override a default event for a program,  except you can override windows API calls at a low level.

This is tricky business and you may need to filter through a lot of OS events to find the specific ones you want to prevent.  This can lead to performance penalties etc.

Here is a link to a toolkit for C++ and Delphi which can simplify the coding required,

http://www.madshi.net/madCodeHookDescription.htm

Also there is form and some good information about code hooking techniques,

http://help.madshi.net/madCodeHook.htm

It's not too hard to do, Windows provides API's for hooking file and directory operations.

That is, if the user is "friendly".  The security system in Windows makes it all too easy for a user to find these hooks and undo them.

Avatar of iddo_shoham

ASKER

Thanks for all of your replies.
Infinity08: the operating system is WinXp, I am writing an application that should be able to allow the administrator to prevent not admin users from saving certain file types to the hdd .
Kode99: thanks for the interesting links, can you point me to the direction to override windows API calls at a low level without using the toolkit?.
grg99: can you please elaborate, what API should I use, can you post some example code .
ASKER CERTIFIED SOLUTION
Avatar of kode99
kode99

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