-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathacSysUtils.~pas
322 lines (292 loc) · 8.46 KB
/
acSysUtils.~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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
unit acSysUtils;
interface
uses
Classes, Printers, SysUtils, Windows, ShlObj, ActiveX, Forms, dialogs;
procedure ensureDirOnWindowsPath(dir: string);
procedure getPrinterList(PList: TStrings);
procedure deltree(dir: string);
procedure listdirectorycontents(basedir: string; s: TStrings);
procedure listdirectories(basedir: string; s: TStrings);
function GetSpecialFolderLocation(HWnd:HWnd; Index:integer):string;
function GetTempDir: string;
function getWindowsTempFileName(prefix: string): string;
function getWindowsTempPath: string;
procedure blockInput;
procedure unblockInput;
function functionAvailable(dllName, funcName: string; var p: pointer): boolean;
function CreateProcessSimple(cmd: string; wait: boolean = false): boolean;
function getWindowsProgramDataDir(subdir: string = ''): string;
procedure bloqueiaTecladoMouse;
procedure desbloqueiaTecladoMouse;
function FuncAvail(dllName, funcName: string; var p: pointer): boolean;
function GetWindowsTempFolder: string;
function isCtrlDown: boolean;
implementation
const
CSIDL_COMMON_APPDATA = $0023;
procedure getPrinterList(PList: TStrings);
var
prnindex: Integer;
begin
PList.AddStrings(Printer.Printers);
Printer.PrinterIndex := -1;
prnindex := Printer.PrinterIndex;
PList.Move(prnindex,0);
end;
procedure deltree(dir: string);
var
s: TStringList;
i: longint;
begin
if (dir[2]=':') and (dir[3]='\') and (length(dir)=3) then
exit;
s := TStringList.create;
try
listdirectorycontents(dir, s);
// first we delete all the directory files
for i := 0 to s.count - 1 do
begin
DeleteFile(PChar(s[i]));
end;
s.clear;
listdirectories(dir, s);
// then we can delete all the directories because are empty
for i := s.count - 1 downto 0 do
begin
removedir(s[i]);
end;
// finally we remove the passed dir
removedir(dir);
finally
s.Free;
end;
end;
procedure listdirectorycontents(basedir: string; s: TStrings);
var
sr: TSearchRec;
begin
if findfirst(basedir + '/*', faAnyFile, sr) = 0 then
begin
if ((faDirectory and sr.Attr) = faDirectory) and (sr.Name <> '.') and (sr.name <> '..') then
begin
listdirectorycontents(basedir + '/' + sr.name, s);
end
else
if (sr.Name <> '.') and (sr.name <> '..') then s.Add(basedir + '/' + sr.Name);
while findnext(sr) = 0 do
begin
if ((faDirectory and sr.Attr) = faDirectory) and (sr.Name <> '.') and (sr.name <> '..') then
begin
listdirectorycontents(basedir + '/' + sr.name, s);
end
else
if (sr.Name <> '.') and (sr.name <> '..') then s.Add(basedir + '/' + sr.Name);
end;
//findclose(sr);
end;
end;
procedure listdirectories(basedir: string; s: TStrings);
var
sr: TSearchRec;
begin
if findfirst(basedir + '/*', faAnyFile, sr) = 0 then
begin
if ((faDirectory and sr.Attr) = faDirectory) and (sr.Name <> '.') and (sr.name <> '..') then
begin
s.add(basedir + '/' + sr.name);
listdirectories(basedir + '/' + sr.name, s);
end;
while findnext(sr) = 0 do
begin
if ((faDirectory and sr.Attr) = faDirectory) and (sr.Name <>'.') and (sr.name <> '..') then
begin
s.add(basedir + '/' + sr.name);
listdirectories(basedir + '/' + sr.name, s);
end;
end;
//findclose(sr);
end;
end;
function GetSpecialFolderLocation(HWnd:HWnd; Index:integer):string;
var
I:PItemIDList;
FIDesktopFolder:IShellFolder;
StrRet:TStrRet;
P:PChar;
M:IMalloc;
begin
Result := '';
SHGetSpecialFolderLocation(HWnd, Index, I);
if I <> nil then begin
SetLength(Result, MAX_PATH);
if SHGetPathFromIdList(I, PChar(Result)) then
SetLength(Result,Strlen(PChar(Result)))
else
Result := '';
SHGetMalloc(M);
if Result='' then begin
SHGetDesktopFolder(FIDesktopFolder);
FIDesktopFolder.GetDisplayNameOf(I, SHGDN_FORPARSING, StrRet);
case StrRet.uType of
STRRET_CSTR:
begin
{$IFDEF VER150}
SetString(Result, StrRet.cStr, lStrLen(StrRet.cStr));
{$ELSE}
SetString(Result, StrRet.cStr, StrLen(StrRet.cStr));
{$ENDIF}
end;
STRRET_OFFSET:
begin
P := @I.mkid.abID[StrRet.uOffset - SizeOf(I.mkid.cb)];
SetString(Result, P, I.mkid.cb - StrRet.uOffset);
end;
STRRET_WSTR:
begin
Result := WideCharToString(StrRet.pOleStr);
if M.DidAlloc(StrRet.pOleStr)>0 then
M.Free(StrRet.pOleStr)
end;
end;
end;
M.Free(I);
end;
end;
function GetTempDir: string;
begin
result := getSpecialFolderLocation(
Application.Handle, CSIDL_COMMON_APPDATA) + '\ACRASSGL\';
end;
function getWindowsTempPath: string;
var
lng: DWORD;
thePath: string;
begin
SetLength(thePath, MAX_PATH);
lng := GetTempPath(MAX_PATH, PChar(thePath));
SetLength(thePath, lng);
result := thePath;
end;
function getWindowsTempFileName(prefix: string): string;
var
lng: DWORD;
pc: PChar;
theFileNameWithPath: array[0..MAX_PATH] of char;
begin
pc := @theFileNameWithPath[0];
lng := GetTempFileName(PChar(getWindowsTempPath), PChar(prefix), 0, pc);
Win32Check(lng <> 0);
SetString(result, pc, strLen(pc));
result := result;
end;
procedure blockInput;
var
BlockInput : function(Block: BOOL): BOOL; stdcall;
begin
if functionAvailable('USER32.DLL', 'BlockInput', @BlockInput) then
BlockInput(true) ;
end;
procedure unblockInput;
var
BlockInput : function(Block: BOOL): BOOL; stdcall;
begin
if functionAvailable('USER32.DLL', 'BlockInput', @BlockInput) then
BlockInput(false) ;
end;
function functionAvailable(dllName, funcName: string; var p: pointer): boolean;
var
lib: THandle;
begin
result := false;
p := nil;
if LoadLibrary(PChar(dllName)) = 0 then exit;
lib := GetModuleHandle(PChar(dllName)) ;
if lib <> 0 then
begin
p := GetProcAddress(lib, PChar(funcName)) ;
if p <> nil then Result := true;
end;
end;
function CreateProcessSimple(cmd: string; wait: boolean = false): boolean;
var
SUInfo: TStartupInfo;
ProcInfo: TProcessInformation;
begin
FillChar(SUInfo, SizeOf(SUInfo), #0);
SUInfo.cb := SizeOf(SUInfo);
SUInfo.dwFlags := STARTF_USESHOWWINDOW;
SUInfo.wShowWindow := SW_HIDE;
Result := CreateProcess(nil,
PChar(cmd),
nil,
nil,
false,
CREATE_NEW_CONSOLE or
NORMAL_PRIORITY_CLASS,
nil,
nil,
SUInfo,
ProcInfo);
if (Result and wait) then
begin
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
end;
end;
function getWindowsProgramDataDir(subdir: string = ''): string;
begin
result := getSpecialFolderLocation(Application.Handle, CSIDL_COMMON_APPDATA) + '\' + subdir;
if not DirectoryExists(result) then
CreateDir(result);
end;
procedure bloqueiaTecladoMouse;
var
BlockInput : function(Block: BOOL): BOOL; stdcall;
begin
if FuncAvail('USER32.DLL', 'BlockInput', @BlockInput) then
BlockInput(true) ;
end;
procedure desbloqueiaTecladoMouse;
var
BlockInput : function(Block: BOOL): BOOL; stdcall;
begin
if FuncAvail('USER32.DLL', 'BlockInput', @BlockInput) then
BlockInput(false) ;
end;
function FuncAvail(dllName, funcName: string; var p: pointer): boolean;
var
lib: THandle;
begin
result := false;
p := nil;
if LoadLibrary(PChar(dllName)) = 0 then exit;
lib := GetModuleHandle(PChar(dllName)) ;
if lib <> 0 then
begin
p := GetProcAddress(lib, PChar(funcName)) ;
if p <> nil then Result := true;
end;
end;
function GetWindowsTempFolder: string;
var
tempFolder: array[0..MAX_PATH] of Char;
begin
GetTempPath(MAX_PATH, @tempFolder);
result := StrPas(tempFolder);
end;
function isCtrlDown: boolean;
var
s: TKeyboardState;
begin
GetKeyboardState(s);
result := ((s[VK_CONTROL] and 128)<>0);
end;
procedure ensureDirOnWindowsPath(dir: string);
var
newPath: String;
begin
newPath := dir + ';' + GetEnvironmentVariable('PATH');
SetEnvironmentVariable(PWideChar('PATH'), PwideChar(newPath));
end;
end.