How to delete a file after user close it

Hi, Expert,

Now I'm implementing a encrypt software, all file is encrypted in a folder,
But when user wanna excute the file, I need decrypt the file and use CreateFile and WriteFile function copy it to a temp file. Then use ShellExecute let user can excute the file. when user edit and save the temp one, I'll encrypt back to the origin encrypt file.

Now my problem is, After user close the file, I need delete the temp one, because all it's content is not encrypt.

I search long time on internet, Can't use "open exclusive" to check is the file opened, because it can't detect notepad or ultraedit, also can't search the window's title to check have the file name or not, because some program may not show file name on window's title.

I found CreateFile have a param FILE_FLAG_DELETE_ON_CLOSE, but can't figure out how to solve my problem, or can you provide any other solution? Thanks!!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahesh1402IT ProfessionalCommented:
Here is useful relavant link :
Self Deleting Executable :


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mahesh1402IT ProfessionalCommented:
>> Can't use "open exclusive" to check is the file opened, because it can't detect notepad or ultraedit.

I don't have complete solution, but about this point: you can delete file while user edits it in Notepad. Run Notepad and wait for file loading using WaitForInputIdle. Then delete file. User cal still save this file and doesn't know anything about this. When Notepat exits, you can encrypt and delete file.

However, there are a lot of problems in your approach. User can save file with other name, and you don't know about this. User can save file but remain in Notepad. If you work with external programs, this is not secure.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Just found the function ShellExecuteEx.  It will give you the instance handle of the program it opened.  You could install a system-wide hook DLL and have it call a function in your application whenever the window gets the WM_NCDESTROY message.  I'm not sure how to get the window from the instance handle though.
Oh, my above suggestion probably won't work.  MSDN says of hInstApp:

"Although hInstApp is declared as an HINSTANCE for compatibility with 16-bit Windows applications, it is not a true HINSTANCE."
AndyAinscowFreelance programmer / ConsultantCommented:
I agree with Alex - your approach is not secure.
You would be better letting the user edit the file inside your app.  You could display the file contents inside a multilline edit control for example.  No need to create a temporary file.  Just decrypt the file and display inside an edit control in your app.  After the user has changed it then encrypt the contents of the edit control and resave the encrypted file back to disk.
> I need decrypt the file and use CreateFile and WriteFile function copy it to a temp file.

What's going to stop the user from making a copy of the unencrypted file while it sits in the temp directory? What if they cut and paste the entire contents of the file from notepad into some other program? Plus Save-as, as already mentioned.

Your approach is extremely easy to circumvent, and provides virtually no security.

You need to rethink how to do what you want.

Ideally you should be writing a filesystem filter driver - pain !!

May be you can try system wide API hooks (dll inject or so)

You should hook all file operations such as CreateFile, WriteFile, ReadFile etc
Let your encryption layer handle all these file requests.
ReadFile handler --> decrypt
WriteFile handler --> encrypt
In that way you don't have to create a temp copy - file execution and your encode/decode will work transparently.

Securing cut-paste operations is kind of tricky. There is no easy methods to solve it.
Its a fine line between secutity and usability.
Usually people will end-up making their security either too restrictive or too open :)

good luck anyway.
AndyAinscowFreelance programmer / ConsultantCommented:
Out of interest how did you solve it?

yeah.. wow! how did you solve it so quickly ? :)
If u're using self-deleting executables, remember they cannot delete themselves if they crash !!

good luck anyway..

AndyAinscowFreelance programmer / ConsultantCommented:
And one doesn't usually open an exe in notepad to view/edit it either.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.