-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnmclient.lpr
More file actions
135 lines (107 loc) · 3.39 KB
/
nmclient.lpr
File metadata and controls
135 lines (107 loc) · 3.39 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
program nmclient;
{$mode objfpc}{$H+}
uses
{$ifdef WINDOWS}
Windows,
{$endif}
{$ifdef UNIX}
{$ifdef UseCThreads}
cthreads,
{$endif}
BaseUnix,
{$endif}
Dialogs,
Process,
FileUtil,
LCLIntf,
SysUtils,
Classes,
Interfaces,
Forms, Main, lnetvisual, request, device, about, option, time, Login,
mJSON, Widget;
{$R *.res}
var
Temp: String;
Fle: TFileStream;
PID: String;
FilePath: String;
FileName: String;
Prc: TProcess;
function ProcessExists(APID: String): Boolean;
{$ifdef WINDOWS}
var
ProcessHandle: THandle;
{$endif}
begin
{$ifdef WINDOWS}
ProcessHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, StrToInt(APID));
if ProcessHandle = 0 then begin
Result := False;
end else begin
CloseHandle(ProcessHandle);
Result := True;
end;
{$endif}
{$ifdef UNIX}
Result := DirectoryExists(Format('/proc/%s', [APID]));
{$endif}
end;
begin
Randomize;
Application.Title := 'Народный мониторинг - Клиент';
Application.Initialize;
RequireDerivedFormResource := True;
FilePath := ExtractFilePath(Application.ExeName);
if ParamStr(1) = '/u' then begin
Sleep(1000);
FileName := ParamStr(2);
if FileExists(FilePath + FileName) then begin
DeleteFile(FilePath + FileName);
RenameFile(FilePath + 'nmclient.upd', FilePath + FileName);
{$ifdef UNIX}FpChmod(FilePath + FileName, &777);{$endif}
Prc := TProcess.Create(nil);
Prc.CurrentDirectory := FilePath;
Prc.Executable := FileName;
Prc.Options := Prc.Options - [poWaitOnExit];
Prc.Execute;
Prc.Free;
Application.Terminate;
Exit;
end;
end;
if (ParamStr(1) <> '/noupdate') and FileExists('nmclient.upd') then begin
CopyFile(Application.ExeName, FilePath + {$ifdef WINDOWS}'update.exe'{$endif}{$ifdef UNIX}'update'{$endif});
{$ifdef UNIX}FpChmod(FilePath + 'update', &777);{$endif}
Prc := TProcess.Create(nil);
Prc.CurrentDirectory := FilePath;
Prc.Executable := {$ifdef WINDOWS}'update.exe'{$endif}{$ifdef UNIX}'update'{$endif};
Prc.Parameters.Add('/u');
Prc.Parameters.Add(ExtractFileName(Application.ExeName));
Prc.Options := Prc.Options - [poWaitOnExit];
Prc.Execute;
Prc.Free;
Application.Terminate;
Exit;
end;
DeleteFile(FilePath + {$ifdef WINDOWS}'update.exe'{$endif}{$ifdef UNIX}'update'{$endif});
DeleteFile(FilePath + 'nmclient.upd');
Temp := GetTempDir;
if FileExists(Temp + '/nmclient.pid') then begin
Fle := TFileStream.Create(Temp + '/nmclient.pid', fmOpenRead);
SetLength(PID, Fle.Size);
Fle.Read(PID[1], Fle.Size);
Fle.Free;
if ProcessExists(PID) then begin
MessageDlgEx(Application.Title, 'Еще один экземпляр программы "' + Application.Title + '" уже запущен.'#13#10'Запуск второго экземляра программы не возможен!', mtError, [mbClose]);
Halt;
end;
end;
PID := IntToStr(GetProcessID);
Fle := TFileStream.Create(Temp + '/nmclient.pid', fmCreate);
Fle.Write(PID[1], Length(PID));
Fle.Free;
Application.CreateForm(TfrmMain, frmMain);
frmMain.onStart;
Application.Run;
DeleteFile(Temp + '/nmclient.pid');
end.