Make(delphi) ver(4.04 build 184) Add(MainForm,2953706,98,147) { Width=655 Height=452 } Add(InlineCode,14836903,182,119) { WorkPoints=#14:doAllOpenFiles| EventPoints=#8:onResult|5:onEnd|10:onProgress|13:onMaxProgress|7:onError| Code=#15:unit HiAsmUnit;|0:|9:interface|0:|32:uses Windows, kol, Share, Debug;|0:|4:type|23: NT_STATUS = Cardinal;|0:|43: TFileDirectoryInformation = packed record|27: NextEntryOffset: ULONG;|21: FileIndex: ULONG;|32: CreationTime: LARGE_INTEGER;|34: LastAccessTime: LARGE_INTEGER;|33: LastWriteTime: LARGE_INTEGER;|30: ChangeTime: LARGE_INTEGER;|29: EndOfFile: LARGE_INTEGER;|34: AllocationSize: LARGE_INTEGER;|26: FileAttributes: ULONG;|26: FileNameLength: ULONG;|38: FileName: array[0..0] of WideChar;|6: end;|57: FILE_DIRECTORY_INFORMATION = TFileDirectoryInformation;|57: PFileDirectoryInformation = ^TFileDirectoryInformation;|58: PFILE_DIRECTORY_INFORMATION = PFileDirectoryInformation;|0:|36: PSYSTEM_THREADS = ^SYSTEM_THREADS;|33: SYSTEM_THREADS = packed record|30: KernelTime: LARGE_INTEGER;|28: UserTime: LARGE_INTEGER;|30: CreateTime: LARGE_INTEGER;|20: WaitTime: ULONG;|26: StartAddress: Pointer;|25: UniqueProcess: DWORD;|24: UniqueThread: DWORD;|22: Priority: Integer;|26: BasePriority: Integer;|30: ContextSwitchCount: ULONG;|19: State: Longint;|24: WaitReason: Longint;|6: end;|0:|60: PSYSTEM_PROCESS_INFORMATION = ^SYSTEM_PROCESS_INFORMATION;|44: SYSTEM_PROCESS_INFORMATION = packed record|22: NextOffset: ULONG;|23: ThreadCount: ULONG;|70: Reserved1: array [0..5] of ULONG; // Что такое, пока не понятно...|25: CreateTime: FILETIME;|23: UserTime: FILETIME;|25: KernelTime: FILETIME;|27: ModuleNameLength: WORD;|30: ModuleNameMaxLength: WORD;|26: ModuleName: PWideChar;|24: BasePriority: ULONG;|21: ProcessID: ULONG;|40: InheritedFromUniqueProcessID: ULONG;|23: HandleCount: ULONG;|70: Reserved2 : array[0..1] of ULONG; // Что такое, пока не понятно...|28: PeakVirtualSize : ULONG;|24: VirtualSize : ULONG;|27: PageFaultCount : ULONG;|31: PeakWorkingSetSize : ULONG;|27: WorkingSetSize : ULONG;|36: QuotaPeakPagedPoolUsage : ULONG;|32: QuotaPagedPoolUsage : ULONG;|39: QuotaPeakNonPagedPoolUsage : ULONG;|35: QuotaNonPagedPoolUsage : ULONG;|26: PageFileUsage : ULONG;|30: PeakPageFileUsage : ULONG;|29: PrivatePageCount : ULONG;|39: ReadOperationCount : LARGE_INTEGER;|40: WriteOperationCount : LARGE_INTEGER;|40: OtherOperationCount : LARGE_INTEGER;|38: ReadTransferCount : LARGE_INTEGER;|39: WriteTransferCount : LARGE_INTEGER;|39: OtherTransferCount : LARGE_INTEGER;|47: ThreadInfo: array [0..0] of SYSTEM_THREADS;|6: end;|0:|58: PSYSTEM_HANDLE_INFORMATION = ^SYSTEM_HANDLE_INFORMATION;|43: SYSTEM_HANDLE_INFORMATION = packed record|21: ProcessId: DWORD;|27: ObjectTypeNumber: Byte;|16: Flags: Byte;|17: Handle: Word;|21: pObject: Pointer;|25: GrantedAccess: DWORD;|6: end;|0:|64: PSYSTEM_HANDLE_INFORMATION_EX = ^SYSTEM_HANDLE_INFORMATION_EX;|46: SYSTEM_HANDLE_INFORMATION_EX = packed record|27: NumberOfHandles: dword;|59: Information: array [0..0] of SYSTEM_HANDLE_INFORMATION;|6: end;|0:|50: PFILE_NAME_INFORMATION = ^FILE_NAME_INFORMATION;|39: FILE_NAME_INFORMATION = packed record|26: FileNameLength: ULONG;|50: FileName: array [0..MAX_PATH - 1] of WideChar;|6: end;|0:|37: PUNICODE_STRING = ^TUNICODE_STRING;|33: TUNICODE_STRING = packed record|18: Length : WORD;|25: MaximumLength : WORD;|49: Buffer : array [0..MAX_PATH - 1] of WideChar;|6: end;|0:|55: POBJECT_NAME_INFORMATION = ^TOBJECT_NAME_INFORMATION;|42: TOBJECT_NAME_INFORMATION = packed record|27: Name : TUNICODE_STRING;|6: end;|0:|38: PIO_STATUS_BLOCK = ^IO_STATUS_BLOCK;|33: IO_STATUS_BLOCK = packed record|22: Status: NT_STATUS;|23: Information: DWORD;|6: end;|0:|53: PGetFileNameThreadParam = ^TGetFileNameThreadParam;|41: TGetFileNameThreadParam = packed record|19: hFile: THandle;|42: Data: array [0..MAX_PATH - 1] of Char;|22: Status: NT_STATUS;|6: end;|0:|5:const|40: STATUS_SUCCESS = NT_STATUS($00000000);|51: STATUS_INVALID_INFO_CLASS = NT_STATUS($C0000003);|53: STATUS_INFO_LENGTH_MISMATCH = NT_STATUS($C0000004);|55: STATUS_INVALID_DEVICE_REQUEST = NT_STATUS($C0000010);|28: ObjectNameInformation = 1;|31: FileDirectoryInformation = 1;|26: FileNameInformation = 9;|43: SystemProcessesAndThreadsInformation = 5;|31: SystemHandleInformation = 16;|0:|4:type|28: THiAsmClass = class(TDebug)|10: private|23: Progress: integer;|51: procedure ShowLockedProcess(FileName: String);|9: public|25: onResult: THI_Event;|21: onEnd:THI_Event;|24: onError: THI_Event;|27: onProgress: THI_Event;|35: onMaxProgress: THI_Event; |59: procedure doAllOpenFiles(var _Data:TData; Index:word);|5: end;|0:|14:implementation|0:|67: function ZwQuerySystemInformation(ASystemInformationClass: DWORD;|65: ASystemInformation: Pointer; ASystemInformationLength: DWORD;|69: AReturnLength: PDWORD): NT_STATUS; stdcall; external 'ntdll.dll';|0:|54: function NtQueryInformationFile(FileHandle: THandle;|62: IoStatusBlock: PIO_STATUS_BLOCK; FileInformation: Pointer;|59: Length: DWORD; FileInformationClass: DWORD): NT_STATUS;|34: stdcall; external 'ntdll.dll';|0:|47: function NtQueryObject(ObjectHandle: THandle;|62: ObjectInformationClass: DWORD; ObjectInformation: Pointer;|35: ObjectInformationLength: ULONG;|68: ReturnLength: PDWORD): NT_STATUS; stdcall; external 'ntdll.dll';|0:|63: function GetLongPathNameA(lpszShortPath, lpszLongPath: PChar;|57: cchBuffer: DWORD): DWORD; stdcall; external kernel32;|0:|40:procedure THiAsmClass.ShowLockedProcess;|0:|52: function GetInfoTable(ATableType: DWORD): Pointer;|5: var|18: dwSize: DWORD;|18: pPtr: Pointer;|24: ntStatus: NT_STATUS;|7: begin|18: Result := nil;|23: dwSize := WORD(-1);|25: GetMem(pPtr, dwSize);|72: ntStatus := ZwQuerySystemInformation(ATableType, pPtr, dwSize, nil);|51: while ntStatus = STATUS_INFO_LENGTH_MISMATCH do|9: begin|27: dwSize := dwSize * 2;|31: ReallocMem(pPtr, dwSize);|74: ntStatus := ZwQuerySystemInformation(ATableType, pPtr, dwSize, nil);|8: end;|37: if ntStatus = STATUS_SUCCESS then|20: Result := pPtr|8: else|20: FreeMem(pPtr);|6: end;|0:|68: function GetFileNameThread(lpParameters: Pointer): DWORD; stdcall;|5: var|40: FileNameInfo: FILE_NAME_INFORMATION;|45: ObjectNameInfo: TOBJECT_NAME_INFORMATION;|35: IoStatusBlock: IO_STATUS_BLOCK;|42: pThreadParam: TGetFileNameThreadParam;|20: dwReturn: DWORD;|7: begin|61: ZeroMemory(@FileNameInfo, SizeOf(FILE_NAME_INFORMATION));|59: pThreadParam := PGetFileNameThreadParam(lpParameters)^;|72: Result := NtQueryInformationFile(pThreadParam.hFile, @IoStatusBlock,|56: @FileNameInfo, MAX_PATH * 2, FileNameInformation);|35: if Result = STATUS_SUCCESS then|9: begin|72: Result := NtQueryObject(pThreadParam.hFile, ObjectNameInformation,|50: @ObjectNameInfo, MAX_PATH * 2, @dwReturn);|37: if Result = STATUS_SUCCESS then|11: begin|38: pThreadParam.Status := Result;|38: WideCharToMultiByte(CP_ACP, 0,|73: @ObjectNameInfo.Name.Buffer[ObjectNameInfo.Name.MaximumLength -|38: ObjectNameInfo.Name.Length],|60: ObjectNameInfo.Name.Length, @pThreadParam.Data[0],|30: MAX_PATH, nil, nil);|9: end|10: else|11: begin|46: pThreadParam.Status := STATUS_SUCCESS;|33: Result := STATUS_SUCCESS;|38: WideCharToMultiByte(CP_ACP, 0,|63: @FileNameInfo.FileName[0], IoStatusBlock.Information,|32: @pThreadParam.Data[0],|30: MAX_PATH, nil, nil);|10: end;|8: end;|59: PGetFileNameThreadParam(lpParameters)^ := pThreadParam;|23: ExitThread(Result);|6: end;|0:|57: function GetFileNameFromHandle(hFile: THandle): String;|5: var|22: lpExitCode: DWORD;|42: pThreadParam: TGetFileNameThreadParam;|21: hThread: THandle;|7: begin|17: Result := '';|63: ZeroMemory(@pThreadParam, SizeOf(TGetFileNameThreadParam));|32: pThreadParam.hFile := hFile;|88: hThread := CreateThread(nil, 0, @GetFileNameThread, @pThreadParam, 0, PDWORD(nil)^);|24: if hThread <> 0 then|7: try|47: case WaitForSingleObject(hThread, 100) of|22: WAIT_OBJECT_0:|13: begin|49: GetExitCodeThread(hThread, lpExitCode);|45: if lpExitCode = STATUS_SUCCESS then|40: Result := pThreadParam.Data;|12: end;|21: WAIT_TIMEOUT:|38: TerminateThread(hThread, 0);|10: end;|11: finally|27: CloseHandle(hThread);|8: end;|6: end;|0:|33: function SetDebugPriv: Boolean;|5: var|19: Token: THandle;|26: tkp: TTokenPrivileges;|7: begin|20: Result := false;|42: if OpenProcessToken(GetCurrentProcess,|57: TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token) then|9: begin|61: if LookupPrivilegeValue(nil, PChar('SeDebugPrivilege'),|36: tkp.Privileges[0].Luid) then|11: begin|32: tkp.PrivilegeCount := 1;|61: tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;|53: Result := AdjustTokenPrivileges(Token, False,|56: tkp, 0, PTokenPrivileges(nil)^, PDWord(nil)^);|10: end;|8: end;|6: end;|0:|4:type|25: DriveQueryData = record|22: DiskLabel: String;|25: DiskDosQuery: String;|25: DosQueryLen: Integer;|6: end;|0:|3:var|27: hFile, hProcess: THandle;|45: pHandleInfo: PSYSTEM_HANDLE_INFORMATION_EX;|20: I, Drive: Integer;|25: ObjectTypeNumber: Byte;|47: FileDirectory, FilePath, ProcessName: String;|57: SystemInformation, TempSI: PSYSTEM_PROCESS_INFORMATION;|46: DosDevices: array [0..25] of DriveQueryData;|36: LongFileName, TmpFileName: String;|5:begin|36: SetLength(LongFileName, MAX_PATH);|64: GetLongPathNameA(PChar(FileName), @LongFileName[1], MAX_PATH);|0:|25: for Drive := 0 to 25 do|7: begin|63: DosDevices[Drive].DiskLabel := Chr(Drive + Ord('a')) + ':';|55: SetLength(DosDevices[Drive].DiskDosQuery, MAXCHAR);|60: ZeroMemory(@DosDevices[Drive].DiskDosQuery[1], MAXCHAR);|54: QueryDosDevice(PChar(DosDevices[Drive].DiskLabel),|51: @DosDevices[Drive].DiskDosQuery[1], MAXCHAR);|83: DosDevices[Drive].DosQueryLen := Length(PChar(DosDevices[Drive].DiskDosQuery));|77: SetLength(DosDevices[Drive].DiskDosQuery, DosDevices[Drive].DosQueryLen);|6: end;|0:|24: ObjectTypeNumber := 0;|15: SetDebugPriv;|72: hFile := CreateFile('NUL', GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0);|38: if hFile = INVALID_HANDLE_VALUE then|7: begin|25: _hi_onEvent(onError);|9: exit;|6: end;|5: try|57: pHandleInfo := GetInfoTable(SystemHandleInformation);|29: if pHandleInfo = nil then|9: begin|27: _hi_onEvent(onError);|11: exit;|8: end;|7: try|55: for I := 0 to pHandleInfo^.NumberOfHandles - 1 do|58: if pHandleInfo^.Information[I].Handle = hFile then|77: if pHandleInfo^.Information[I].ProcessId = GetCurrentProcessId then|15: begin|77: ObjectTypeNumber := pHandleInfo^.Information[I].ObjectTypeNumber;|18: Break;|14: end;|11: finally|27: FreeMem(pHandleInfo);|8: end;|9: finally|23: CloseHandle(hFile);|6: end;|0:|74: SystemInformation := GetInfoTable(SystemProcessesAndThreadsInformation);|34: if SystemInformation <> nil then|5: try|57: pHandleInfo := GetInfoTable(SystemHandleInformation);|30: if pHandleInfo <> nil then|7: try|20: Progress := 0;|64: _hi_onEvent(onMaxProgress, pHandleInfo^.NumberOfHandles); |55: for I := 0 to pHandleInfo^.NumberOfHandles - 1 do|11: begin|79: if pHandleInfo^.Information[I].ObjectTypeNumber = ObjectTypeNumber then|13: begin|59: hProcess := OpenProcess(PROCESS_DUP_HANDLE, True,|51: pHandleInfo^.Information[I].ProcessId);|30: if hProcess > 0 then|13: try|76: if DuplicateHandle(hProcess, pHandleInfo^.Information[I].Handle,|78: GetCurrentProcess, @hFile, 0, False, DUPLICATE_SAME_ACCESS) then|15: try|65: if Assigned(Applet) and AppletTerminated then exit;|14: |55: FilePath := GetFileNameFromHandle(hFile);|36: if FilePath <> '' then|19: begin|36: FileDirectory := '';|39: for Drive := 0 to 25 do|59: if DosDevices[Drive].DosQueryLen > 0 then|73: if Copy(FilePath, 1, DosDevices[Drive].DosQueryLen) =|57: DosDevices[Drive].DiskDosQuery then|25: begin|67: FileDirectory := DosDevices[Drive].DiskLabel;|73: Delete(FilePath, 1, DosDevices[Drive].DosQueryLen);|28: Break;|24: end;|0:|56: if FileDirectory = '' then Continue; |0:|44: TempSI := SystemInformation;|22: repeat|40: if TempSI^.ProcessID =|62: pHandleInfo^.Information[I].ProcessId then|23: begin|54: ProcessName := TempSI^.ModuleName;|26: Break;|22: end;|72: TempSI := Pointer(DWORD(TempSI) + TempSI^.NextOffset);|45: until TempSI^.NextOffset = 0;|0:|49: SetLength(TmpFileName, MAX_PATH);|93: GetLongPathNameA(PChar(FileDirectory + FilePath), @TmpFileName[1], MAX_PATH);|47: if FileExists(TmpFileName) then|74: _hi_onEvent(onResult, ProcessName + ': ' + TmpFileName);|18: end;|19: finally|33: CloseHandle(hFile);|16: end;|17: finally|34: CloseHandle(hProcess);|14: end;|12: end;|33: Progress := Progress + 1;|51: _hi_OnEvent(onProgress, integer(Progress));|10: end;|11: finally|27: FreeMem(pHandleInfo);|8: end;|9: finally|31: FreeMem(SystemInformation);|6: end;|21: _hi_onEvent(onEnd);|6:end; |0:|37:procedure THiAsmClass.doAllOpenFiles;|5:begin|24: ShowLockedProcess('');|4:end;|0:|4:end.| link(onResult,2032141:doAdd,[]) link(onEnd,2982350:doMessage,[]) link(onProgress,10990105:doEvent1,[]) link(onMaxProgress,9234157:doMax,[(233,146)(233,166)(292,166)(292,146)]) } Add(Memo,2032141,427,119) { Left=5 Top=5 Width=635 Height=355 ScrollBars=3 } Add(Button,16620665,98,98) { Left=300 Top=390 link(onClick,3729307:doEvent1,[]) } Add(ProgressBar,9234157,308,133) { Left=10 Top=365 Width=630 Smooth=1 Point(doMax) } Add(Hub,10990105,245,133) { link(onEvent1,9234157:doPosition,[]) link(onEvent2,1859675:doProcessMessages,[(283,146)(283,181)]) } Add(Application,1859675,308,175) { Wait=1 } Add(Hub,3729307,147,98) { link(onEvent1,2032141:doClear,[(410,104)(410,132)]) link(onEvent2,14836903:doAllOpenFiles,[(171,111)(171,125)]) } Add(Message,2982350,364,126) { Message="All handles found" }