Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function StartIServer(const CmdStr, StartDirectory:string;
- flags:cardinal;
- var cont_in, cont_out, iserver:THandle):cardinal;
- var
- SecAttr:security_attributes;
- nulhdl:THandle;
- TimeUsed, MaxMemoryUsed:int64;
- ReturnCode:DWORD;
- iserv_in, iserv_out:THandle;
- tmp:Thandle;
- begin
- result := EXEC_FAIL;
- SecAttr.nlength:=sizeof (SecAttr);
- SecAttr.lpSecurityDescriptor:=nil;
- SecAttr.bInheritHandle:=true;
- nulhdl:=createfile ('nul', generic_write, file_share_read or file_share_write,
- @SecAttr, create_always, 0, 0);
- if (IsErrorHandle (nulhdl)) then begin
- exit;
- end;
- if not CreatePipe(iserv_in, cont_out, nil, 0) then begin
- CloseHandle(nulhdl);
- exit;
- end;
- if not CreatePipe(cont_in, iserv_out, nil, 0) then begin
- CloseHandle(iserv_in);
- CloseHandle(cont_out);
- CloseHandle(nulhdl);
- exit;
- end;
- if not DuplicateHandle(GetCurrentProcess, iserv_in, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
- CloseHandle(iserv_in);
- CloseHandle(iserv_out);
- CloseHandle(cont_in);
- CloseHandle(cont_out);
- CloseHandle(nulhdl);
- exit;
- end else iserv_in := tmp;
- if not DuplicateHandle(GetCurrentProcess, iserv_out, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
- CloseHandle(iserv_in);
- CloseHandle(iserv_out);
- CloseHandle(cont_in);
- CloseHandle(cont_out);
- CloseHandle(nulhdl);
- exit;
- end else iserv_out := tmp;
- Result:=ExternalExecuteRedirect (CmdStr, StartDirectory, flags or EXEC_FLAG_UTILITY or EXEC_FLAG_NOWAIT or EXEC_FLAG_CLOSE_HANDLES,
- 0, 0, iserv_in, iserv_out, {temporary}nulhdl,
- TimeUsed, MaxMemoryUsed, ReturnCode, @iserver);
- iserver_process := iserver;
- if not DuplicateHandle(GetCurrentProcess, cont_in, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
- TerminateProcess(iserver, 243);
- CloseHandle(cont_in);
- CloseHandle(cont_out);
- Result := EXEC_FAIL;
- exit;
- end else cont_in := tmp;
- if not DuplicateHandle(GetCurrentProcess, cont_out, GetCurrentProcess, @tmp, 0, True, DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE) then begin
- TerminateProcess(iserver, 243);
- CloseHandle(cont_in);
- CloseHandle(cont_out);
- Result := EXEC_FAIL;
- exit;
- end else cont_out := tmp;
- if (Result = EXEC_OK) xor (not IsErrorHandle(iserver_process)) then begin
- logstring('StartIServer fail: result=%d, but iserver_process=%d', [Result, iserver_process]);
- Result := EXEC_FAIL;
- end;
- end;
Add Comment
Please, Sign In to add comment