A: 

I am deleting the file which is used by the same process

Just close the file first...

Ed Swangren
and if your file is used by other processes you may try CreateRemoteThread() to close the handle.
x86shadow
The above code is for display the error only.I have an hosted swf file which uses the wav file.I have a back end application which modifies the same wave file which is being used by the swf. I want the updated wave to be rendered in swf. And the only way is to replace the existing file.
Amit Shah
You assume that this is the "only way".
Ed Swangren
+1  A: 

Yes, this is a known Windows security concern, known as a "handle recycle attack". It is technically possible to walk the undocumented kernel handle table, inject code in the process that owns the handle and call CloseHandle() to close the file handle. Very hard to exploit, you need admin rights to do this. Much better ways to screw up a process if you have that right.

What results is more of a Denial Of Service attack, one that randomly fecks up the machine with no way to find out how it happened. The process is not aware that the file handle was closed, it didn't close it, and just keeps writing to the handle. The written data falls into the bit bucket. Ignoring the return value of WriteFile() is pretty standard in most unmanaged programs.

Until the process opens another handle to write to, say, a database. Handles are recycled, it can get the same handle value back. Now the process is writing the normal database data, intermixed with the data that was supposed to go in the now-closed file.

Hilarity ensues when anybody tries to read the data back from that database. Or whatever other resource got destroyed, it's random. You are kinda covered though, nobody will be able to figure out that it was your program that destroyed the machine.

Hans Passant