BSOne.SFC/eCrmHE/EXE_eCrmHomeEdition/Thread/ThdScreenRecord.pas

194 lines
4.8 KiB
Plaintext

{*******************************************************}
{ }
{ ThdScreenRecord }
{ }
{ Copyright (C) 2025 kku }
{ }
{*******************************************************}
unit ThdScreenRecord;
interface
uses
Tocsg.Thread, System.SysUtils, System.Classes, superobject, Winapi.Windows,
Tocsg.Win32;
type
TThdScreenRecord = class(TTgThread)
protected
hRcvWnd_,
hBs1rcdWnd_: HWND;
ProcInfo_: TProcessInformation;
sWorkDir_: String;
Mtx_: TTgMutex;
sTgApps_: String;
nMaxMin_: Integer;
procedure Execute; override;
public
Constructor Create(hRcvWnd: HWND);
Destructor Destroy; override;
end;
implementation
uses
Tocsg.Exception, Tocsg.Path, GlobalDefine, Tocsg.Process, ManagerService,
Tocsg.Registry, ManagerModel, Tocsg.WndUtil;
{ TThdScreenRecord }
Constructor TThdScreenRecord.Create(hRcvWnd: HWND);
begin
Inherited Create;
hRcvWnd_ := hRcvWnd;
hBs1rcdWnd_ := 0;
ZeroMemory(@ProcInfo_, SizeOf(ProcInfo_));
sWorkDir_ := '';
sTgApps_ := '';
nMaxMin_ := 3;
Mtx_ := TTgMutex.Create('Global\251120scrcd2');
if Mtx_.MutexState = msAlreadyExist then
_Trace('Create() .. Fail .. AlreadyExist mutex');
end;
Destructor TThdScreenRecord.Destroy;
begin
FreeAndNil(Mtx_);
Inherited;
end;
procedure TThdScreenRecord.Execute;
var
sRcdExe: String;
procedure CreateProcessSR;
var
hProcess: THandle;
O: ISuperObject;
sParam: String;
begin
hBs1rcdWnd_ := 0;
_Trace('CreateProcess .. bs1rcd');
if not FileExists(sRcdExe) then
begin
_Trace('CreateProcessSR() .. Fail .. not found file, Path=%s', [sRcdExe]);
exit;
end;
try
if ProcInfo_.dwProcessId <> 0 then
TerminateProcessByPid(ProcInfo_.dwProcessId);
if not ForceDirectories(sWorkDir_) then
begin
_Trace('Fail .. CreateWorkDir()');
SetWorkState(tsFail);
exit;
end;
ZeroMemory(@ProcInfo_, SizeOf(ProcInfo_));
O := SO;
O.I['Cmd'] := 1;
O.I['RcvWnd'] := hRcvWnd_;
O.S['Apps'] := sTgApps_;
O.S['TaskDir'] := sWorkDir_;
O.I['MaxMin'] := nMaxMin_;
O.S['OwMtx'] := Mtx_.MutexName;
sParam := ExtractFilePath(sRcdExe) + '#prcd.dat';
if SaveJsonObjToFile(O, sParam) then
begin
ProcInfo_ := ExecuteApp(sRcdExe, Format('-p "%s"', [sParam]), SW_SHOWNORMAL);
if ProcInfo_.dwProcessId = 0 then
begin
{$IFDEF DEBUG}
ASSERT(false);
{$ELSE}
_Trace('Fail .. CreateProcessSR() .. ExecuteApp()');
{$ENDIF}
end else begin
Sleep(2000);
_Trace('CreateProcess .. bs1rcd .. OK');
end;
end else
_Trace('Fail .. CreateProcessSR() .. SaveJsonObjToFile()');
except
on E: Exception do
ETgException.TraceException(Self, E, 'Fail .. CreateProcessSR()');
end;
end;
var
PO: TPrefModel;
sTgApps: String;
nMaxMin: Integer;
bUpdate: Boolean;
O: ISuperObject;
begin
//{$IFDEF DEBUG}
// sRcdExe := 'C:\taskToCSG\eCrmHE\EXE_bs1rcd\OUT_Debug - Win32\' + EXE_ScrRcd;
//{$ELSE}
sRcdExe := GetRunExePathDir + DIR_CONF + EXE_ScrRcd;
//{$ENDIF}
sWorkDir_ := GetRunExePathDir + 'Task\';// 'C:\ProgramData\HE\Task\';
PO := gMgSvc.ModePolicy;
sTgApps_ := PO.AppRcdApps;
nMaxMin_ := PO.AppRcdMaxMin;
while not Terminated and not GetWorkStop do
begin
try
if GetProcessPidByName(EXE_ScrRcd) = 0 then
begin
CreateProcessSR;
end else begin
if hBs1rcdWnd_ = 0 then
begin
hBs1rcdWnd_ := StrToIntDef(GetRegValueAsString(HKEY_LOCAL_MACHINE, REG_HE32, 'bs1rcd'), 0);
if hBs1rcdWnd_ <> 0 then
DelRegValue(HKEY_LOCAL_MACHINE, REG_HE32, 'bs1rcd');
end;
if hBs1rcdWnd_ > 0 then
begin
bUpdate := false;
// PO := gMgSvc.ModePolicy;
sTgApps := PO.AppRcdApps;
nMaxMin := PO.AppRcdMaxMin;
if sTgApps_ <> sTgApps then
begin
sTgApps_ := sTgApps;
bUpdate := true;
end;
if nMaxMin_ <> nMaxMin then
begin
nMaxMin_ := nMaxMin;
bUpdate := true;
end;
if bUpdate then
begin
O := SO;
O.S['Apps'] := sTgApps_;
O.I['MaxMin'] := nMaxMin_;
SendData(hBs1rcdWnd_, 9091, O.AsJSon);
_Trace('APP 동영상 녹화 : 정책 업데이트', 2);
end;
end;
end;
Sleep(1000);
except
on E: Exception do
ETgException.TraceException(Self, E, 'Fail .. Execute()');
end;
end;
end;
end.