-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitPublic.pas
More file actions
143 lines (130 loc) · 3.16 KB
/
UnitPublic.pas
File metadata and controls
143 lines (130 loc) · 3.16 KB
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
unit UnitPublic;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Forms, IniFiles, Registry,
VCLUnZip;
procedure InitializeApplication(app: TApplication);
procedure setAutoRun(app: TApplication; key: string; enabled: Boolean;
parameter: String)overload;
procedure setAutoRun(app: TApplication; key: string; enabled: Boolean)overload;
function isAutoRun(app: TApplication; key: string): Boolean;
// 得到计算机名称
function fnGetComputerName(): String;
function getFileNameFromURL(url: string): string;
function UnZipDir(sFile, sDir: string): Boolean;
var
gSysDir: String;
gAppName: String;
gServerUrl: String;
implementation
// -----------------------------------------------------------------
// 得到计算机名称
// add by jzh 2002-04-15
// -----------------------------------------------------------------
function fnGetComputerName(): String;
var
szComputerName: array [0 .. 255] of char;
nSize: Cardinal;
begin
nSize := 256;
FillChar(szComputerName, sizeof(szComputerName), 0);
GetComputerName(szComputerName, nSize);
if StrPas(szComputerName) = '' then
Result := ''
else
Result := StrPas(szComputerName);
end;
procedure InitializeApplication(app: TApplication);
var
MyIniFile: TIniFile;
begin
gSysDir := ExtractFilePath(app.ExeName);;
MyIniFile := TIniFile.Create(gSysDir + 'config.ini');
try
with MyIniFile do
begin
gAppName := ReadString('Config', 'AppName', 'WebDNS');
end;
finally
MyIniFile.Free;
end;
end;
procedure setAutoRun(app: TApplication; key: string; enabled: Boolean);
begin
setAutoRun(app, key, enabled, '');
end;
procedure setAutoRun(app: TApplication; key: string; enabled: Boolean;
parameter: String);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run', true);
if length(key) = 0 then
key := ExtractFileName(app.ExeName);
if enabled then
begin
Reg.WriteString(key, '"' + app.ExeName + '" ' + parameter);
end
else
begin
Reg.DeleteValue(key);
end;
Reg.CloseKey;
end;
function isAutoRun(app: TApplication; key: string): Boolean;
var
Reg: TRegistry;
enabled: Boolean;
value: String;
begin
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run', true);
if length(key) = 0 then
key := ExtractFileName(app.ExeName);
value := Reg.ReadString(key);
enabled := (Length(value) > 0);
Reg.CloseKey;
Result := enabled;
end;
function getFileNameFromURL(url: string): string;
var
ts: TStrings;
begin
// 从url取得文件名
ts := TStringList.Create;
try
ts.Delimiter := '/';
ts.DelimitedText := url;
if ts.Count > 0 then
Result := ts[ts.Count - 1];
finally
ts.Free;
end;
end;
function UnZipDir(sFile, sDir: string): Boolean;
var
unzip: TVCLUnZip;
begin
Result := true;
unzip := TVCLUnZip.Create(nil);
with unzip do
begin
ZipName := sFile;
ReadZip;
Destdir := sDir;
RecreateDirs := true;
FilesList.Add('*.*');
DoAll := true;
OverwriteMode := Always;
end;
try
unzip.unzip;
except
Result := False;
end;
unzip.Free;
end;
end.