Embodiment
Below in conjunction with the accompanying drawing in the embodiment of the invention, the technical scheme in the embodiment of the invention is clearly and completely described, obviously, described embodiment only is the present invention's part embodiment, rather than whole embodiment.Based on the embodiment among the present invention, those of ordinary skills belong to the scope of protection of the invention not making the every other embodiment that is obtained under the creative work prerequisite.
In the process of application program, when needs are opened application file, need to open application file by OpenFile (opening file) function.The OpenFile function is after successfully opening an application file, in system kernel, generate the handle information of this application file, and return described handle information to process that should program, the process that this should program all will realize by handle all operations of this application file afterwards.If other process is arranged to be wanted this application file is made amendment or deleted, system will judge the handle information that whether has this application file in the kernel objects, if in kernel objects, there has been the handle information of this application file, represent that this handle information is not released, the requirement of above-mentioned other process is made amendment or the operation of deleting is refused by system.
And the module of calling for the process of system level, in the process of system level, open described module, need be by calling LoadLibrary (for the process executable module of packing into) function, and open the module of appointment by the 1pFilename of LoadLibrary function (pointing to the address of filename) parameter, shine upon this module then in the address space of calling process, after finishing, mapping returns the module handle information that is loaded by LoadLibrary, when after this process of system level is called module, can quote this handle information and seek the position of the module that the process of system level need quote, the same with reference document, when correctly not discharging this handle information, this module can not be deleted.
The embodiment of the invention is by searching handle in process, and comes releasing handle by process itself, thereby realizes the correct release of handle, avoided handle to be forced to discharge in kernel objects.Be described in detail below in conjunction with the method and the device of accompanying drawing embodiment of the invention releasing handle.
Embodiment 1:
Present embodiment provides a kind of method of releasing handle, can be correctly releasing handle safely so that can delete corresponding file.As shown in Figure 1, the method for concrete releasing handle comprises the steps:
101, as can be known, the handle information corresponding file that requires to discharge in order to discharge the handle of this document, is promptly treated releasing handle just by process transfer, at first obtains the name information and the handle information of the file correspondence of process transfer from above-mentioned analysis.
102, judge whether accessed name information is identical with the name information for the treatment of the releasing handle respective file, if described name information is identical, the handle information requirements of the file correspondence of expression process transfer are released, and then execution in step 103; Otherwise the handle information of the file correspondence of expression process transfer does not require to be released execution in step 104.
103, the handle information of notifying above-mentioned process to discharge described name information respective file.
104, owing to do not need to discharge described handle information, this step can be reselected a process, and returns execution in step 101.
The handle information specific implementation of notifying above-mentioned process to discharge described name information respective file described in the above-mentioned steps 103 is: inject the code that discharges described handle information in described process.That is: the technology of using thread far away to notify is injected one section code to described process, and will be loaded into the running space of described process after the injection of this section code, so this section code will return described process to own.In this section injecting codes, only need to use respective function to discharge corresponding handle information, for the process institute calling module of system level above-mentioned, generally adopt FreeLibrary (executable module that the release process loads) function to close the release that handle information can realize handle information; For application file above-mentioned, generally adopt CloseHandle (closing handle) function to close the release that handle information can reach handle information to picture.
Present embodiment is when searching the handle information that needs release, be in the file of process transfer, to search, in kernel objects, do not search, and in the time of last releasing handle information, also be that notification process is come releasing handle information by process itself, can guarantee that handle information is normally discharged by process itself, with directly in kernel hard closing handle information compare, can avoid the undesired release of handle information, withdraw from thereby avoid reporting an error by the process that the undesired release of handle information causes.
After above-mentioned process is finished release to handle information, just can directly delete this handle information corresponding file, do not need to restart system and delete this document again.
Embodiment 2:
Be that the module of system loads is an example with the file of process transfer below, specify the method that the embodiment of the invention provides releasing handle, as shown in Figure 2, this method comprises the steps:
201, after the process number in the acquisition system, open described process and set up snapshot for described process, this step can be finished by the interface function CreateToolhelp32Snapshot (setting up the reflection of appointment process in system) that system provides, CreateToolhelp32Snapshot can set up a snapshot for process in system, this snapshot comprises module and the thread information that this process is all.
202, the function Module32First (module that process begins most) that provides by system searches the information of first module in the above-mentioned snapshot.
203, obtain name information and handle information in the information that finds first module.
204, judge whether accessed name information is identical with the name information for the treatment of the releasing handle respective file, if described name information is identical, represent that the handle information requirements of this module correspondence are released, then execution in step 205; Otherwise the handle information of expression this document correspondence does not require to be released execution in step 206.
205, the handle information requirements owing to first module correspondence that is found are released, so preserve the handle information of described name information respective file, so that carry out releasing operation.
Generally speaking, this handle information is to be kept in the chained list, for example: the chained list of fifo queue formula.
206, judge whether module in the snapshot searches and finish, do not finish execution in step 207 if search; Finish execution in step 208 if search.
207, the function Module32Next that provides by system (the next module of process) searches the information of the next module in the above-mentioned snapshot, and execution in step 203.
208, judge whether to obtain next process number in the system, if next process number in the system that can obtain, then execution in step 201; Otherwise execution in step 209.
209, notice said system process discharges and has preserved handle information, and the handle information of having preserved is the handle information of the identical file correspondence of described name information.Specifically can use the technology of thread far away notice to realize the release of handle information, as: to as described in process inject one section code, and this section code will be loaded into the running space of described process after injecting.For system process calling module above-mentioned, in this section injecting codes, generally adopt FreeLibrary (executable module that the release process loads) function to close the release that handle information can realize handle information.
Be to adopt the handle information of preserving earlier in the present embodiment, in the reporting system process it is discharged, when practice, can be the formation of first in first out with the handle information setting of preserving, like this can be when searching the handle information that needs to discharge, notice corresponding system process is discharged the handle information that has found, accelerated processing speed.
In the present embodiment, the handle information of when the delivery system process insmods, calling, at first pass through the information of the mode acquisition module of snapshot, from the information of module, search the handle information that needs release then, after finding all handle information, discharge the handle information of this module by system process itself, avoided system process to answer the undesired release of handle information and report an error and withdraw from.
After system process is finished release to handle information, just can directly delete the module of this handle information correspondence, do not need to restart system and delete this module again.
Embodiment 3:
File with process transfer is that application file is an example below, specifies the method that the embodiment of the invention provides releasing handle, and as shown in Figure 3, this method comprises the steps:
301, directly obtain all application files that present all application processes of moving are called.Because calling the process of application file is general application process, can finish the operation of multi-process and multithreading by specific function: at first use ZwQuerySystemInformation (obtaining different system information type) function to enumerate the handle of the All Files of opening, next need to use a undocumented function ZwQueryInformationFile (obtaining the file information type different) to picture, by SYSTEM_INFORMATION_CLASS (system information enumeration type) structure wherein, finish the operation of multi-process and thread, to obtain all application files.
302, open a structure of the above-mentioned application file that gets access to, specifically describe a structure by the structure of title SYSTEM_INFORMATION_CLASS.
303, obtain the name information and the handle information of application file correspondence in the described opened structure.Use SystemHandleInformation (the system's handle information) parametric description of SYSTEM_INFORMATION_CLASS structure in the method, finish the operation of obtaining all application files.
304, judge whether accessed name information is identical with the name information for the treatment of the releasing handle respective file, if described name information is identical, represent that the handle information requirements of this module correspondence are released, then execution in step 305; Otherwise the handle information of expression this document correspondence does not require to be released execution in step 306.
305, preserve the handle information of described name information respective file, so that carry out the operation of releasing handle information.
306, judge in the application file got access to whether have the structure of opening or not, if the structure of not opening in addition, then execution in step 307, otherwise execution in step 308.
307, navigate to the next structure of not opening, and execution in step 302.
308, notify above-mentioned process to discharge the handle information of having preserved.This step can adopt the technology of thread notice far away to inject one section code to described process, and will be loaded into the running space of described process after the injection of this section code.For application file above-mentioned, in this section injecting codes, generally adopt CloseHandle (closing handle) function to close the release that handle information can reach handle information to picture.
Be to adopt the handle information of preserving earlier in the present embodiment, reinforming application process discharges it, when practice, can be the formation of first in first out with the handle information setting of preserving, like this can be when searching the handle information that needs to discharge, notice respective application process is discharged the handle information that has found, accelerated processing speed.
After application process is finished release to handle information, just can directly delete the application file of this handle information correspondence, do not need to restart system and delete this application file again.
Embodiment 4:
Corresponding to the foregoing description 1, present embodiment provides the device of first kind of releasing handle, and as shown in Figure 4, this device comprises: acquiring unit 41, judging unit 42 and notification unit 43.
The concrete function of each unit is as follows in this device: acquiring unit 41 is used to obtain the name information and the handle information of the file correspondence of process transfer; After getting access to corresponding name information and handle information, judging unit 42 is used to judge whether accessed name information is identical with the name information for the treatment of the releasing handle respective file; If described name information is identical, represent that then the handle information requirements in this document are released, described notification unit 43 is used for when name information is identical, the handle information of notifying described process to discharge described name information respective file.Described notification unit 43 realized by thread notification technique far away, as: by to as described in inject in the process discharge as described in the code of handle information, with the handle information of notifying described process to discharge described name information respective file.
Corresponding to the foregoing description 2, present embodiment also provides the device of second kind of releasing handle, and as shown in Figure 5, this device comprises: acquiring unit 51, judging unit 52, storage unit 53 and notification unit 54.The file that this device is mainly used to handle at described process transfer is the situation of the module of system loads, and in this case, the function of acquiring unit 51 is finished jointly by snapshot module 511 and acquisition module 512.The concrete function of each unit and module is described below:
Described snapshot module 511 is used to process to set up snapshot, comprises described module information in this snapshot, and described acquisition module 512 is used for obtaining the name information and the handle information of described module information.After getting access to corresponding name information and handle information, judging unit 52 is used to judge whether accessed name information is identical with the name information for the treatment of the releasing handle respective file; If described name information is identical, represent that then the handle information requirements in this document are released, described storage unit 53 is used for when described name information is identical, preserves the handle information of described name information respective file.Described notification unit 54 is used for when name information is identical, notify described process to discharge the handle information of having preserved, by the function that storage unit 53 and notification unit 54 are finished jointly, be equivalent to when described name information is identical, notify corresponding system process to discharge the handle information of respective file.
Corresponding to the foregoing description 3, present embodiment also provides a kind of device of releasing handle, and as shown in Figure 6, this device comprises: acquiring unit 61, judging unit 62, storage unit 63 and notification unit 64.The file that this device is mainly used to handle at described process transfer is the situation of application file, and in this case, the function of acquiring unit 61 is by opening module 611 and acquisition module 612 is finished jointly.The concrete function of each unit and module is described below:
Describedly open the structure that module 611 is used to open the application file of process transfer, described acquisition module 612 is used for obtaining the name information and the handle information of described structure application file correspondence.After getting access to corresponding name information and handle information, judging unit 62 is used to judge whether accessed name information is identical with the name information for the treatment of the releasing handle respective file; If described name information is identical, represent that then the handle information requirements in this document are released.Described storage unit 63 is used for when described name information is identical, preserves the handle information of described name information respective file.Described notification unit 64 is used for when name information is identical, notify described process to discharge the handle information of having preserved, by the function that storage unit 63 and notification unit 64 are finished jointly, be equivalent to when described name information is identical, notify corresponding process to discharge the handle information of respective file.
The notification unit that is adopted in the device of three kinds of releasing handles in the present embodiment (43,54 or 64) all can adopt thread notification technique far away to realize, as: by to as described in inject in the process discharge as described in the code of handle information, with the handle information of notifying described process to discharge described name information respective file.
The storage unit that is adopted in the device of back two kinds of releasing handles in the present embodiment can realize by fifo queue, so just can be when searching the handle information that needs to discharge, with notifying corresponding process to discharge the handle information that has found, accelerated processing speed.
Embodiment 5:
As shown in Figure 7, present embodiment provides a kind of file deleting system, and this system comprises: filename deriving means 71, handle releasing means 72 and file delete device 73.
Filename deriving means 71 in the present embodiment is used to obtain the name information for the treatment of deleted file; Handle releasing means 72 is used to obtain the name information and the handle information of the file correspondence of process transfer; Judge whether the name information for the treatment of deleted file that accessed name information and filename deriving means 71 get access to is identical; And when described name information is identical, the handle information of notifying described process to discharge described name information respective file; File delete device 73 is used for treating the deleted file deletion with described, because handle releasing means 72 will treat that the handle information of deleted file has correctly discharged, so file delete device 73 can be deleted this document smoothly.
Discharge all handles that need discharge in order to make handle releasing means 72 to concentrate, handle releasing means 72 in the present embodiment also is used for when described name information is identical, preserve the handle information of described name information respective file, so just can concentrate release, can improve the reliability that handle discharges the handle information of having preserved.
If handle releasing means 72 adopts the mode of first in first out to preserve the handle information of described name information respective file, then can when getting access to handle information, the handle information of having preserved be discharged, can improve the efficient that handle discharges.
The operation of above-mentioned releasing handle is a kind of normal handle dispose procedure, and correctly releasing handle can not cause corresponding process to report an error because of the undesired release of handle and withdraw from.After handle information is finished in release, just the deletion of corresponding file safety can not needed to restart system.
The embodiment of the invention mainly is used in the various deletions that need to carry out in the system file, is used in especially in the antivirus software system, discharges corresponding handle information before deletion infective virus file.
Through the above description of the embodiments, the those skilled in the art can be well understood to the present invention and can realize by the mode that software adds essential general hardware platform, can certainly pass through hardware, but the former is better embodiment under a lot of situation.Based on such understanding, the part that technical scheme of the present invention contributes to prior art in essence in other words can embody with the form of software product, this computer software product is stored in the storage medium that can read, floppy disk as computing machine, hard disk or CD etc., comprise some instructions with so that equipment (can be server, perhaps the network equipment etc.) carry out the described method of each embodiment of the present invention.
The above; only be the specific embodiment of the present invention, but protection scope of the present invention is not limited thereto, anyly is familiar with those skilled in the art in the technical scope that the present invention discloses; the variation that can expect easily or replacement all should be encompassed within protection scope of the present invention.Therefore, protection scope of the present invention should be as the criterion with the protection domain of described claim.