[帮助] Enumarate,确定类型和关闭句柄 [英] [HELP]Enumarate , Determine Type , and Closing Handles

查看:82
本文介绍了[帮助] Enumarate,确定类型和关闭句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为VB 2008中的.Net app / program工作,它将使用指定的路径备份/复制文件和文件夹。我的兄弟测试了它,他发现当应用程序尝试复制当前正由其他进程使用的文件时,它会返回当前使用的错误。因此复制过程将失败。



我的目标是关闭当前使用文件的过程的句柄



示例是SQL Server。当服务运行时,数据库文件,.mdf和.ldf文件是LOCKED。当应用程序尝试复制文件并首先失败时,应用程序将创建一种方法来复制文件,关闭该文件中其他进程的句柄,然后当所有句柄关闭时,应用程序将尝试再次复制它。



我目前正在尽我所能,但我已经存货,我已经使用低级API NtQuerySystemInformation来枚举句柄系统,但仍远离我的目标。我应该确定枚举什么类型的句柄,然后如果应用程序知道它是文件句柄,那么它将检查它是否是目标文件句柄,然后确定句柄的所有者,然后获取句柄句柄的所有者,由Kernel32的OpenProcess函数返回的所有者句柄将用于调用CreateRemoteThread,然后远程线程要执行的函数用于关闭目标文件句柄。



但这只是我的想法。我不知道这是否有效。实际上我在库存的部分我想确定Handle的类型。



请帮帮我,希望有人知道解决方案。 :(

解决方案

不好主意。它看起来像是一个很大的潜在滥用。当然,你可以杀死进程,这是释放文件句柄的方法。但是这是残酷的;如果一个进程保存文件,它就是故意完成的。如果你这样做,你确定需要这样的文件吗?如果你强行释放它,它可能缺少一些数据或损坏的数据,因为由于你的执法,数据没有提交到磁盘。



请参阅我过去在某些相关主题上的答案:如何压缩错误'它已被vb.net中的另一个进程'使用 [ ^ ]。



-SA

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. :(

解决方案

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


这篇关于[帮助] Enumarate,确定类型和关闭句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆