{*******************************************************} { } { ProcessParam } { } { Copyright (C) 2022 kku } { } {*******************************************************} unit ProcessParam; interface uses System.SysUtils, Winapi.Windows, Winapi.Messages, Tocsg.Param, Vcl.Clipbrd, CttSchDefine, System.Classes; type TNetScanInfo = record llTasker: LONGLONG; FileScanOpt: TFileScanOpt; sTgNetDir: String; dwCustomType: DWORD; end; TDrmInfo = record sUName, sDept, sEmpNo, sPoName: String; bApproval: Boolean; end; TProcessParam = class(TTgParam) private hRcvWnd_: HWND; nParamCnt_: Integer; sOwnerMtx_: String; nRole_: Integer; NetSI_: TNetScanInfo; DrmI_: TDrmInfo; procedure SelectFileFromOpenDialog(sFilter: String; nCtrl: Integer); procedure SelectFolderFromFileOpenDialog(nCtrl: Integer); public Constructor Create; Destructor Destroy; override; function IsParamOK: Boolean; procedure SendData(dwCmd: DWORD; const sData: String); property RcvWnd: HWND read hRcvWnd_; property Role: Integer read nRole_; property OwnerMtx: String read sOwnerMtx_; property NetSI: TNetScanInfo read NetSI_; property DrmI: TDrmInfo read DrmI_; end; var gParam: TProcessParam = nil; implementation uses superobject, DefineHelper, Winapi.ShlObj, Vcl.Dialogs, Tocsg.Process, Tocsg.Path, Tocsg.Shell, GlobalOutAddInDefine, DLicense, DQnA, Define, Vcl.Forms, DAfterReport, DUserInfoAgree, Vcl.Imaging.pngimage, Tocsg.Safe, Vcl.Graphics, CrmUtil, Tocsg.Trace, Tocsg.Exception, Tocsg.Strings, Winapi.ShellAPI, Tocsg.Clipboard, DReqDevExcept, Tocsg.Json, DDecryptDrm, Condition, Tocsg.Network, DEncryptDrm, Tocsg.Hash, Soap.EncdDecd, Winapi.WinSpool, Tocsg.Printer; function GetSpecialDir(nFolder: Integer): String; var pidl: PItemIDList; hRes: HRESULT; bSuccess: Boolean; sRealPath: array [0..MAX_PATH] of {$IFDEF UNICODE} WideChar {$ELSE} AnsiChar {$ENDIF}; begin Result := ''; bSuccess := false; hRes := SHGetSpecialFolderLocation(0, nFolder, pidl); if hRes = NO_ERROR then bSuccess := SHGetPathFromIDList(pidl, sRealPath); if bSuccess = true then Result := IncludeTrailingPathDelimiter(sRealPath); end; function GetDesktopDir: String; begin Result := GetSpecialDir(CSIDL_DESKTOPDIRECTORY); end; function GetRunExePath: String; begin Result := ParamStr(0); end; function GetRunExePathDir: String; begin Result := ExtractFilePath(GetRunExePath); end; { TProcessParam } Constructor TProcessParam.Create; begin Inherited Create; ASSERT(gParam = nil); gParam := Self; nParamCnt_ := ParamCount; hRcvWnd_ := 0; nRole_ := 0; ZeroMemory(@NetSI_, SizeOf(NetSI_)); ZeroMemory(@DrmI_, SizeOf(DrmI_)); end; Destructor TProcessParam.Destroy; begin gParam := nil; Inherited; end; procedure TProcessParam.SendData(dwCmd: DWORD; const sData: String); var CopyData: TCopyDataStruct; begin CopyData.dwData := dwCmd; CopyData.cbData := (Length(sData) + 1) * 2; CopyData.lpData := PChar(sData); SendMessage(hRcvWnd_, WM_COPYDATA, 0, NativeInt(@CopyData)); end; procedure TProcessParam.SelectFileFromOpenDialog(sFilter: String; nCtrl: Integer); var OpenDialog: TOpenDialog; O: ISuperObject; i: Integer; bMulti: Boolean; begin OpenDialog := TOpenDialog.Create(nil); OpenDialog.Filter := sFilter; OpenDialog.FileName := ''; OpenDialog.InitialDir := GetDesktopDir; case nCtrl of 3, 99 : bMulti := true; else bMulti := false; end; if bMulti then OpenDialog.Options := OpenDialog.Options + [ofAllowMultiSelect]; if OpenDialog.Execute(0) then begin O := SO; O.S['Path'] := OpenDialog.FileName; O.I['Ctrl'] := nCtrl; if bMulti then begin var OA: ISuperObject := TSuperObject.Create(stArray); for i := 0 to OpenDialog.Files.Count - 1 do OA.AsArray.Add(OpenDialog.Files[i]); O.O['List'] := OA; end; SendData(HPCMD_SELECT_FILE, O.AsString); end; OpenDialog.Free; end; procedure TProcessParam.SelectFolderFromFileOpenDialog(nCtrl: Integer); var OpenDialog: TFileOpenDialog; O: ISuperObject; begin OpenDialog := TFileOpenDialog.Create(nil); OpenDialog.FileName := ''; OpenDialog.DefaultFolder := GetDesktopDir; OpenDialog.Options := OpenDialog.Options + [fdoPickFolders]; if OpenDialog.Execute(0) then begin O := SO; O.S['Path'] := OpenDialog.FileName; O.I['Ctrl'] := nCtrl; SendData(HPCMD_SELECT_FOLDER, O.AsString); end; OpenDialog.Free; end; function GetCbData(aO: ISuperObject; var sBody: String; var sOcrBody: String; var sImgPath: String): Boolean; var CB: TClipboard; hCb: THandle; bmp: Vcl.Graphics.TBitmap; png: TPngImage; sTemp: String; nLen: Integer; arrFName: array [0..MAX_PATH-1] of WideChar; i: Integer; bFail, // bIsExcel, bExtrImg: Boolean; nTry: Integer; Label LB_TryReadCB; begin Result := false; Guard(CB, TClipboard.Create); nTry := 0; // bIsExcel := aO.B['IsExcel2']; LB_TryReadCB : bFail := false; try bExtrImg := not aO.B['IgrImgCB']; if CB.HasFormat(CF_UNICODETEXT) then begin if CB.GetSize < 10000 then begin sTemp := Trim(CB.AsText); // if bIsExcel then // begin // nLen := Length(sTemp); // if nLen > 10000 then // begin // bExtrImg := false; // SetLength(sTemp, 5000); // sTemp := sTemp + Format(' ... (OrgSize=%d)', [InsertPointComma(nLen, 3)]); // end; // end; if sTemp <> '' then sBody := sTemp; end else bExtrImg := false; end else if CB.HasFormat(CF_TEXT) then begin if CB.GetSize < 10000 then begin sTemp := Trim(CB.AsText); // if bIsExcel then // begin // nLen := Length(sTemp); // if nLen > 10000 then // begin // bExtrImg := false; // SetLength(sTemp, 5000); // sTemp := sTemp + Format(' ... (OrgSize=%d)', [InsertPointComma(nLen, 3)]); // end; // end; if sTemp <> '' then sBody := sTemp; end else bExtrImg := false; end else if CB.HasFormat(CF_HDROP) then begin sTemp := ''; hCb := CB.GetAsHandle(CF_HDROP); if hCb <> 0 then begin GlobalLock(hCb); try nLen := DragQueryFile(hCb, DWORD(-1), nil, MAX_PATH); for i := 0 to nLen - 1 do begin if DragQueryFile(hCb, i, arrFName, MAX_PATH) <> 0 then SumString(sTemp, arrFName, '|'); end; finally GlobalUnlock(hCb); end; end; if sTemp <> '' then sBody := sTemp; end; if bExtrImg and CB.HasFormat(CF_BITMAP) then begin hCb := CB.GetAsHandle(CF_BITMAP); if hCb <> 0 then begin GlobalLock(hCb); try try Guard(bmp, Vcl.Graphics.TBitmap.Create); bmp.LoadFromClipboardFormat(CF_BITMAP, hCb, CB.GetAsHandle(CF_PALETTE)); Guard(png, TPngImage.Create); png.Assign(bmp); sTemp := 'C:\ProgramData\HE\Task\'; if ForceDirectories(sTemp) then begin sTemp := sTemp + FormatDateTime('yyyymmddhhnnss', Now) + '.png'; png.SaveToFile(sTemp); if FileExists(sTemp) then begin sImgPath := sTemp; if aO.B['DoClipOcr'] then sOcrBody := ExtrTextFromImage(sImgPath); end; end else begin TTgTrace.T('Fail .. GetCbData() .. creatDir'); exit; end; except on E: Exception do ETgException.TraceException(E, 'Fail .. GetCbData() .. make image'); end; finally GlobalUnlock(hCb); end; end; end; Result := true; except on E: EClipboardException do bFail := true; on E: Exception do ETgException.TraceException(E, 'Fail .. GetCbData()'); end; if bFail then begin if nTry < 5 then begin Inc(nTry); Sleep(500); goto LB_TryReadCB; end; end; end; function TProcessParam.IsParamOK: Boolean; var sPath, sTemp: String; O: ISuperObject; bDel: Boolean; i: Integer; Jobs: array [0..9] of JOB_INFO_1; begin Result := false; if ExistsParam('-hook') then begin var sDllPath: String := CutFileExt(GetRunExePath) + '32.dll'; if FileExists(sDllPath) then Result := InjectModule(StrToInt64Def(GetParamValue('-hook'), 0), sDllPath) <> 0; end else if ExistsParam('-hook2') then begin var sDllPath: String := CutFileExt(GetRunExePath) + '32f.dll'; if FileExists(sDllPath) then Result := InjectModule(StrToInt64Def(GetParamValue('-hook2'), 0), sDllPath) <> 0; end else if ExistsParam('-clearhook') then begin var sDllPath: String := CutFileExt(GetRunExePath) + '32.dll'; var sDllPath2: String := CutFileExt(GetRunExePath) + '32f.dll'; if FileExists(sDllPath) then begin EjectModuleFromPath(sDllPath + '|' + sDllPath2); Sleep(1000); EjectModuleFromPath(sDllPath + '|' + sDllPath2); end; end else if ExistsParam('-pjob') then begin sPath := GetParamValue('-pjob'); i := StrToIntDef(GetParamValue('-c'), -1); if i = -1 then exit; var nJobId: Integer := StrToIntDef(GetParamValue('-j'), -1);; if nJobId = -1 then exit; var hPrt: THandle := 0; var dwNed, dwRet: DWORD; if OpenPrinter(PChar(sPath), hPrt, nil) then begin SetJob(hPrt, nJobId, 0, nil, i); ClosePrinter(hPrt); end; end else begin sPath := GetParamValue('/p'); if sPath = '' then sPath := GetParamValue('-p'); if (sPath = '') or not FileExists(sPath) then sPath := GetRunExePathDir + DAT_PARAM; bDel := true; try if not LoadJsonObjFromFile(O, sPath) then exit; if O.B['RunAs'] then begin bDel := false; // 관리자 권한으로 실행 추가 23_0504 12:41:16 kku O.B['RunAs'] := false; sTemp := 'C:\ProgramData\HE\Task\'; if ForceDirectories(sTemp) and SaveJsonObjToFile(O, sTemp + DAT_PARAM) then begin ExecutePath_runAs(GetRunExePath, Format('/p "%s"', [sTemp + DAT_PARAM])); exit; end; end; finally if bDel then DeleteFile(PChar(sPath)); end; hRcvWnd_ := O.I['RcvWnd']; if O.I['CT'] <> 0 then CUSTOMER_TYPE := O.I['CT']; case O.I['Cmd'] of HPCMD_SELECT_FILE : SelectFileFromOpenDialog(O.S['Filter'], O.I['Ctrl']); HPCMD_SELECT_FOLDER : SelectFolderFromFileOpenDialog(O.I['Ctrl']); HPCMD_EXECUTE_FILE : begin sPath := O.S['Path']; sTemp := O.S['Param']; if CompareText(sPath, 'explorer.exe') = 0 then begin // explorer.exe라면... 재시작을 의미하기로 한다 23_0227 14:58:18 kku // TerminateProcessByName(sPath); ExecutePath_hide('cmd.exe', '/c taskkill /f /im explorer.exe'); Sleep(500); end; if O.B['Hide'] then ExecutePath_hide(sPath, sTemp) else if O.B['Open'] then OpenPath(sPath) else ExecutePath(sPath, sTemp); end; HPCMD_OPEN_SELECT : ExplorerSelectedPath(O.S['Path']); // HPCMD_SHOW_NOTIFICATION HPCMD_INSTALL_OUTLOOK_ADDIN : begin InstallOutlookAddin(O.S['MdDir']); SendData(HPCMD_INSTALL_OUTLOOK_ADDIN, ''); end; HPCMD_UNINSTALL_OUTLOOK_ADDIN : begin sTemp := O.S['MdDir']; ExecutePath_hide('regsvr32.exe', Format('/u /s "%s"', [sTemp + DLL_ADDIN])); ExecutePath_hide('regsvr32.exe', Format('/u /s "%s"', [sTemp + DLL_ADDIN64])); end; HPCMD_LICENSE_AGREE : begin var dlg: TDlgLicense := TDlgLicense.Create(nil, O.I['CT'], O.B['Count'], O.B['NoExit'], O.S['Msg']); dlg.ShowModal; dlg.Free; end; HPCMD_PERSONALINFO_AGREE : begin var dlg: TDlgUserInfoAgree := TDlgUserInfoAgree.Create(nil); dlg.ShowModal; dlg.Free; end; HPCMD_SEND_QnA_MESSAGE : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; var dlg: TDlgQnA := TDlgQnA.Create(nil); dlg.Label1.Caption := O.S['Desc']; dlg.lbOk.Caption := O.S['BtnOk']; dlg.lbCancel.Caption := O.S['BtnCancel']; dlg.ShowModal; dlg.Free; end; HPCMD_INPUT_EXCEPT_REASON : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; var dlg: TDlgQnA := TDlgQnA.Create(nil); dlg.ReqCmd := HPCMD_INPUT_EXCEPT_REASON; dlg.MinStrLen := O.I['MinTL']; dlg.sMinInputMsg := O.S['MinMsg']; dlg.Caption := O.S['Title']; dlg.Label1.Caption := O.S['Desc']; dlg.lbOk.Caption := O.S['BtnOk']; dlg.ShowModal; dlg.Free; end; HPCMD_REQ_DEVICE_EXCEPT : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; var ReqInfo: TReqDevExceptInfo := TTgJson.GetDataAsType(O.O['ReqInfo']); var dlg: TDlgReqDevExcept := TDlgReqDevExcept.Create(nil, ReqInfo); dlg.ShowModal; dlg.Free; end; HPCMD_SEND_AFTER_REPORT : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; var dlg: TDlgAfterReport := TDlgAfterReport.Create(nil); dlg.SetInfo(O.S['UserName'], O.D['AfterRptDT']); dlg.ShowModal; dlg.Free; end; HPCMD_REQ_CLIPBOARD_DATA : begin var sBody, sOcrBody, sImgPath: String; if GetCbData(O, sBody, sOcrBody, sImgPath) then begin var OData: ISuperObject := SO; OData.S['Body'] := sBody; OData.S['OcrBody'] := sOcrBody; OData.S['ImgPath'] := sImgPath; OData.S['WinCap'] := O.S['WinCap']; OData.S['AName'] := O.S['AName']; OData.S['APath'] := O.S['APath']; OData.I['AWnd'] := O.I['AWnd']; SendData(HPCMD_REQ_CLIPBOARD_DATA, OData.AsString); end; end; HPCMD_REQ_NETDIR_SCAN : begin nRole_ := O.I['Cmd']; sOwnerMtx_ := O.S['Mtx']; NetSI_.llTasker := O.I['Tasker']; NetSI_.FileScanOpt := TTgJson.GetDataAsType(O.O['FOpt']); NetSI_.dwCustomType := O.I['CT']; NetSI_.sTgNetDir := O.S['TgNDir']; hRcvWnd_ := NetSI_.FileScanOpt.CttSchOpt.hRcvHwnd; end; HPCMD_REQ_NETDRVADDR : begin var fs: TFileStream; Guard(fs, TFileStream.Create(PChar(O.S['OutPath']), fmCreate)); var sData: UTF8String := NetDriveToRemoteAddr(O.S['NetDrv']); fs.Write(PAnsiChar(sData)^, Length(sData)); exit; end; HPCMD_OPEN_ENCRYPT : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; DrmI_.sUName := O.S['UName']; DrmI_.sDept := O.S['Dept']; DrmI_.sEmpNo := O.S['EN']; DrmI_.sPoName := O.S['PON']; var FList: TStringList; Guard(FList, TStringList.Create); if (O.O['Files'] <> nil) and (O.O['Files'].DataType = stArray) then begin for i := 0 to O.A['Files'].Length - 1 do Flist.Add(O.A['Files'].S[i]); end; var dlg: TDlgEncryptDrm := TDlgEncryptDrm.Create(nil); if Flist.Count > 0 then dlg.AddFiles(FList); dlg.ShowModal; dlg.Free; end; HPCMD_OPEN_DECRYPT : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; DrmI_.sUName := O.S['UName']; DrmI_.sDept := O.S['Dept']; DrmI_.sEmpNo := O.S['EN']; DrmI_.sPoName := O.S['PON']; DrmI_.bApproval := O.B['AVAL']; var FList: TStringList; Guard(FList, TStringList.Create); if (O.O['Files'] <> nil) and (O.O['Files'].DataType = stArray) then begin for i := 0 to O.A['Files'].Length - 1 do Flist.Add(O.A['Files'].S[i]); end; var dlg: TDlgDecryptDrm := TDlgDecryptDrm.Create(nil); if Flist.Count > 0 then dlg.AddFiles(FList); dlg.ShowModal; dlg.Free; end; HPCMD_REQ_DECRYPT : begin APP_TITLE := O.S['WinCap']; Application.Title := APP_TITLE; DrmI_.sUName := O.S['UName']; DrmI_.sDept := O.S['Dept']; DrmI_.sEmpNo := O.S['EN']; DrmI_.sPoName := O.S['PON']; var FList: TStringList; Guard(FList, TStringList.Create); if (O.O['DecFiles'] <> nil) and (O.O['DecFiles'].DataType = stArray) then begin for i := 0 to O.A['DecFiles'].Length - 1 do begin sPath := O.A['DecFiles'].O[i].S['filePath']; if FileExists(sPath) and (O.A['DecFiles'].O[i].S['hash'] = GetFileToSha1Str(sPath)) then Flist.Add(sPath); end; end; var dlg: TDlgDecryptDrm := TDlgDecryptDrm.Create(nil); if Flist.Count > 0 then dlg.AddFiles(FList); var n1, n2, n3: Integer; dlg.DoDecFiles(n1, n2, n3); dlg.SetReadOnly; dlg.FormStyle := fsStayOnTop; dlg.ShowModal; dlg.Free; end; HPCMD_COPY_FILE : begin var sSrc: String := O.S['Src']; var sDest: String := O.S['Dest']; if FileExists(sSrc) then CopyFile(PChar(sSrc), PChar(sDest), false); end; HPCMD_REQ_FILEICON : begin sPath := O.S['P']; var ico: TIcon := GetFileSmallIcon(sPath); var ms: TMemoryStream := TMemoryStream.Create; ico.SaveToStream(ms); var OInfo: ISuperObject := SO; OInfo.I['N'] := O.I['N']; OInfo.S['E'] := O.S['E']; OInfo.S['D'] := EncodeBase64(ms.Memory, ms.Size); SendData(HPCMD_REQ_FILEICON, OInfo.AsJSon); ms.Free; ico.Free; end; HPCMD_CTRL_PRINTER : begin sPath := O.S['P']; i := O.I['C']; var nJobId: Integer := O.I['J']; var hPrt: THandle := 0; var dwNed, dwRet: DWORD; if OpenPrinter(PChar(sPath), hPrt, nil) then SetJob(hPrt, nJobId, 0, nil, i); end; HPCDM_PRINT_INFO_LIST : begin var PrtsInfo: TPrintersInfo; Guard(PrtsInfo, TPrintersInfo.Create); PrtsInfo.RefreshList; PrtsInfo.SaveToFile(O.S['P']); end else begin nRole_ := O.I['Cmd']; sOwnerMtx_ := O.S['Mtx']; end; end; Result := true; end; end; end.