Click here to Skip to main content
15,071,729 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
   
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.
   
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
uRLuVRJoNNeL 1-Apr-15 0:42am
   
Sorry im just new here , Im still searching for other way , but if ever there is no other way , I tell him that he must Stop the SQL Service before doing the backup. That is the only solution and I know it was the best way. Sorry about the abusive Idea that I have.
   
Oh, no need to be sorry, we all learn things. Thank you for accepting the answer; it won't prevent others to add answers. But I think this is what you can do: re-formulate the question and get some general advice of backup, explaining the ultimate goals and the problems, requirements. You can ask the question separately. I don't to such things, but maybe other members can advise something better.
—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