yeputons

Untitled

Apr 3rd, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.57 KB | None | 0 0
  1. function StartIServer(const CmdStr, StartDirectory:string;
  2.                       flags:cardinal;
  3.                       var cont_in, cont_out, iserver:THandle):cardinal;
  4. var
  5.   SecAttr:security_attributes;
  6.   nulhdl:THandle;
  7.   TimeUsed, MaxMemoryUsed:int64;
  8.   ReturnCode:DWORD;
  9.  
  10.   iserv_in, iserv_out:THandle;
  11.   tmp:Thandle;
  12. begin
  13. result := EXEC_FAIL;
  14.  
  15. SecAttr.nlength:=sizeof (SecAttr);
  16. SecAttr.lpSecurityDescriptor:=nil;
  17. SecAttr.bInheritHandle:=true;
  18. nulhdl:=createfile ('nul', generic_write, file_share_read or file_share_write,
  19.                    @SecAttr, create_always, 0, 0);
  20. if (IsErrorHandle (nulhdl)) then begin
  21.   exit;
  22. end;
  23.  
  24. if not CreatePipe(iserv_in, cont_out, nil, 0) then begin
  25.   CloseHandle(nulhdl);
  26.   exit;
  27. end;
  28.  
  29. if not CreatePipe(cont_in, iserv_out, nil, 0) then begin
  30.   CloseHandle(iserv_in);
  31.   CloseHandle(cont_out);
  32.   CloseHandle(nulhdl);
  33.   exit;
  34. end;
  35.  
  36. if not DuplicateHandle(GetCurrentProcess, iserv_in, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
  37.   CloseHandle(iserv_in);
  38.   CloseHandle(iserv_out);
  39.   CloseHandle(cont_in);
  40.   CloseHandle(cont_out);
  41.   CloseHandle(nulhdl);
  42.   exit;
  43. end else iserv_in := tmp;
  44. if not DuplicateHandle(GetCurrentProcess, iserv_out, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
  45.   CloseHandle(iserv_in);
  46.   CloseHandle(iserv_out);
  47.   CloseHandle(cont_in);
  48.   CloseHandle(cont_out);
  49.   CloseHandle(nulhdl);
  50.   exit;
  51. end else iserv_out := tmp;
  52.  
  53. Result:=ExternalExecuteRedirect (CmdStr, StartDirectory, flags or EXEC_FLAG_UTILITY or EXEC_FLAG_NOWAIT or EXEC_FLAG_CLOSE_HANDLES,
  54.                        0, 0, iserv_in, iserv_out, {temporary}nulhdl,
  55.                        TimeUsed, MaxMemoryUsed, ReturnCode, @iserver);
  56. iserver_process := iserver;
  57.  
  58. if not DuplicateHandle(GetCurrentProcess, cont_in, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
  59.   TerminateProcess(iserver, 243);
  60.   CloseHandle(cont_in);
  61.   CloseHandle(cont_out);
  62.   Result := EXEC_FAIL;
  63.   exit;
  64. end else cont_in := tmp;
  65. if not DuplicateHandle(GetCurrentProcess, cont_out, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
  66.   TerminateProcess(iserver, 243);
  67.   CloseHandle(cont_in);
  68.   CloseHandle(cont_out);
  69.   Result := EXEC_FAIL;
  70.   exit;
  71. end else cont_out := tmp;
  72.  
  73. if (Result = EXEC_OK) xor (not IsErrorHandle(iserver_process)) then begin
  74.   logstring('StartIServer fail: result=%d, but iserver_process=%d', [Result, iserver_process]);
  75.   Result := EXEC_FAIL;
  76. end;
  77.  
  78. end;
Add Comment
Please, Sign In to add comment