Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've been working for a .Net app/program in VB 2008 that will backup/copy file(s) and Folder(s) with the specified path. My Brother tested it and he found out that when the app try to Copy a file that is currently in used by the Other Process , it returns an error that it was currently used. therefore the Copy Procedure will fail.

My Objective is to Close the 'Handle' of the process that is currently using the File

Example is the SQL Server. when the Service is running , the Database file, the .mdf and .ldf file is LOCKED. when the app try to copy the file and fail at first , the app will make a way to copy the file bye closing the handle of the other process(s) in that file , then when all Handle(s) are closed , the app will try to copy it again.

I'm currently trying my best but I'm stock , I already use the Low-Level API NtQuerySystemInformation to Enumerate the Handles in the System, but still far from my Objective. I should determine what type of Handle is being enumerated, and then if the app knew that is was a File Handle , then it will check if it was the Target File Handle , then determine the owner of the handle , then obtain a handle to the Owner of the Handle , the Owner's Handle returned by OpenProcess Function of Kernel32 will be used to Call CreateRemoteThread then the Function to be execute by the Remote Thread is use To Close the Target File Handle.

But this is only my idea. I don't know if this will work. Actually I'm stock in the part where I want to determine what is the type of the Handle.

Please help me, I hope someone know the solution. :(
Posted

1 solution

Bad idea. It looks like big potential abuse. Of course, you can just kill the process, this is the way to release the file handle. But this is brutal; if a process holds the file, it's done on purpose. If you do it, are you sure you need such file? If you forcefully "release" it, it can have lack of some data or corrupt data, because the data wasn't committed to disk, because of your enforcement.

Please see my past answer on somewhat related topics: how to compress the error 'it is already used by another process' in vb.net[^].

—SA
 
Share this answer
 
v2
Comments
Dave Kreskowiak 31-Mar-15 18:56pm    
And by forcing the handle to close oyu can also cause the process that holds it to crash.
Sergey Alexandrovich Kryukov 31-Mar-15 19:29pm    
Sure, but the inquirer may care only about the file (after all, any process can be re-started, but the data loss can be a real loss); and it's hopeless.

By the way I would like to invite your to see my new 1 of April publication and have some fun:
Some Programming Approaches to "Neuro-Linguistic Programming".
Participation in this game in Comments and Discussions is especially encouraged.

Thank you.
—SA
uRLuVRJoNNeL 31-Mar-15 23:31pm    
I count the number of images appear , i thought there are something I could answer at the end of clip , haha You got me , Thanks , it lessen my Stress about the Problem i've encounter. :) Thanks , I appreciate it
uRLuVRJoNNeL 31-Mar-15 23:26pm    
I already tell it to my brother that it can cause a very Unusual Error , I mean a BSOD and a data loss , actually the main Objective is to allow my app to copy a SQL Server Database File(s) even the Service is running. But Thanks for your answers. I appreciate it.
Sergey Alexandrovich Kryukov 1-Apr-15 0:34am    
Sure. Will you accept the answer? Frankly, the only right idea is to properly sort out what exactly is going on. It sounds like you are trying to abuse the technology in general. Instead of discussing your situation, we would need to discuss your ultimate goals.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900