forked from doublecmd/doublecmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffileunlock.pas
172 lines (146 loc) · 4.48 KB
/
ffileunlock.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
unit fFileUnlock;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Grids, uFileUnlock;
type
{ TfrmFileUnlock }
TfrmFileUnlock = class(TForm)
btnUnlockAll: TButton;
btnUnlock: TButton;
btnClose: TButton;
btnTerminate: TButton;
stgFileHandles: TStringGrid;
procedure btnTerminateClick(Sender: TObject);
procedure btnUnlockAllClick(Sender: TObject);
procedure btnUnlockClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure stgFileHandlesDblClick(Sender: TObject);
procedure stgFileHandlesSelection(Sender: TObject; aCol, aRow: Integer);
private
procedure ShowThread;
procedure UnlockRow(Index: Integer);
public
end;
function ShowUnlockForm(ProcessInfo: TProcessInfoArray): Boolean;
implementation
{$R *.lfm}
uses
Windows, Math, LCLStrConsts, DCConvertEncoding, fMain, uMyWindows, uLng;
function ShowUnlockForm(ProcessInfo: TProcessInfoArray): Boolean;
var
Index: Integer;
UnlockEnabled: Boolean = False;
begin
with TfrmFileUnlock.Create(frmMain) do
try
stgFileHandles.RowCount:= Length(ProcessInfo) + 1;
for Index:= 1 to stgFileHandles.RowCount - 1 do
begin
if (ProcessInfo[Index - 1].FileHandle <> 0) then begin
UnlockEnabled:= True;
stgFileHandles.Cells[0, Index]:= IntToStr(ProcessInfo[Index - 1].FileHandle);
end;
stgFileHandles.Cells[1, Index]:= IntToStr(ProcessInfo[Index - 1].ProcessId);
stgFileHandles.Cells[2, Index]:= ProcessInfo[Index - 1].ExecutablePath;
end;
btnUnlockAll.Enabled:= UnlockEnabled;
btnTerminate.Enabled:= Length(ProcessInfo) > 0;
stgFileHandles.Row:= IfThen(UnlockEnabled, 1, 0);
btnUnlock.Enabled:= UnlockEnabled and (Length(stgFileHandles.Cells[0, 1]) > 0);
TThread.Synchronize(nil, @ShowThread);
Result:= (ModalResult = mrOK);
finally
Free;
end;
end;
{ TfrmFileUnlock }
procedure TfrmFileUnlock.stgFileHandlesSelection(Sender: TObject; aCol, aRow: Integer);
begin
btnUnlock.Enabled:= (aRow > 0) and (Length(stgFileHandles.Cells[0, aRow]) > 0);
end;
procedure TfrmFileUnlock.btnUnlockClick(Sender: TObject);
begin
UnlockRow(stgFileHandles.Row);
if (stgFileHandles.RowCount = 1) then
begin
Close;
ModalResult:= mrOK;
end;
end;
procedure TfrmFileUnlock.FormShow(Sender: TObject);
var
Index: Integer;
begin
for Index:= 0 to stgFileHandles.Columns.Count - 1 do
begin
stgFileHandles.Columns[Index].Width:= stgFileHandles.Canvas.TextWidth(stgFileHandles.Columns[Index].Title.Caption + 'W');
end;
end;
procedure TfrmFileUnlock.stgFileHandlesDblClick(Sender: TObject);
var
AHandle: HWND;
ProcessId: DWORD;
begin
if (stgFileHandles.Row > 0) then
begin
ProcessId:= StrToDWord(stgFileHandles.Cells[1, stgFileHandles.Row]);
AHandle:= FindMainWindow(ProcessId);
if AHandle <> 0 then ShowWindowEx(AHandle);
end;
end;
procedure TfrmFileUnlock.btnUnlockAllClick(Sender: TObject);
var
Index: Integer;
begin
for Index:= stgFileHandles.RowCount - 1 downto 1 do
begin
UnlockRow(Index);
end;
if (stgFileHandles.RowCount = 1) then
begin
Close;
ModalResult:= mrOK;
end;
end;
procedure TfrmFileUnlock.btnTerminateClick(Sender: TObject);
var
Index: Integer;
ProcessId: DWORD;
begin
if (stgFileHandles.Row > 0) then
begin
if MessageBoxW(Handle, PWideChar(CeUtf8ToUtf16(rsMsgTerminateProcess)), PWideChar(CeUtf8ToUtf16(rsMtWarning)), MB_YESNO or MB_ICONWARNING) = IDYES then
begin
ProcessId:= StrToDWord(stgFileHandles.Cells[1, stgFileHandles.Row]);
if uFileUnlock.TerminateProcess(ProcessId) then
begin
for Index:= stgFileHandles.RowCount - 1 downto 1 do
begin
if (ProcessId = StrToDWord(stgFileHandles.Cells[1, Index])) then
stgFileHandles.DeleteRow(Index);
end;
if (stgFileHandles.RowCount = 1) then
begin
Close;
ModalResult:= mrOK;
end;
end;
end;
end;
end;
procedure TfrmFileUnlock.ShowThread;
begin
ShowModal;
end;
procedure TfrmFileUnlock.UnlockRow(Index: Integer);
var
ProcessId: DWORD;
FileHandle: HANDLE;
begin
ProcessId:= StrToDWord(stgFileHandles.Cells[1, Index]);
FileHandle:= StrToQWord(stgFileHandles.Cells[0, Index]);
if FileUnlock(ProcessId, FileHandle) then stgFileHandles.DeleteRow(Index);
end;
end.