Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread Delphi and PHP
Fri, Aug 31 2012 7:10 AMPermanent Link

Uli Becker

Does anyone know how I can use php with a server written with Delphi
(Indy or Tim's webserver)?

I know that I can run php.exe from the command line and save the result
as a file, but I guess there is a better way.

Thanks Uli
Fri, Aug 31 2012 12:25 PMPermanent Link

Jose Eduardo Helminsky

HPro Informatica

Uli

I have used a following routine to capture the out of any application into a
string and then send it back to client browser.

cOut := ExecAndWait('c:\webserver\scripts\php.exe parameters',True)

function ExecAndWait(cCmd: String; IsOut: Boolean = False; IsShow: Boolean =
False): String;
var zAppName: array[0..512] of char;
   StartupInfo: TStartupInfo;
   ProcessInfo: TProcessInformation;
   ReadPipe: THandle;
   WritePipe: THandle;
   Security : TSecurityAttributes;
   nInt: DWord;
   nRead: DWord;
   xApp: DWord;
   xBuf: PAnsiChar;
begin
    Result := '';
    if IsOut then begin
       with Security do begin
          nlength := SizeOf(TSecurityAttributes) ;
          binherithandle := true;
          lpsecuritydescriptor := nil;
       end;
       if not Createpipe (ReadPipe, WritePipe, @Security, 0) then begin
          raise Exception.Create('Não foi possível iniciar a captura do
processo');
       end;
    end;
    xBuf := AllocMem(2400 + 1);

    StrPCopy(zAppName,cCmd);
    FillChar(StartupInfo,Sizeof(StartupInfo),#0);
    StartupInfo.cb := Sizeof(StartupInfo);
    if IsOut then begin
       StartupInfo.hStdOutput := WritePipe;
       StartupInfo.hStdInput := ReadPipe;
       StartupInfo.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;
    end else begin
       StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
    end;
    if IsShow then begin
       StartupInfo.wShowWindow := SW_SHOW;
    end else begin
       StartupInfo.wShowWindow := SW_HIDE;
    end;

    if IsOut then begin
       if not
CreateProcess(nil,zAppName,@Security,@Security,True,NORMAL_PRIORITY_CLASS,nil,
nil,StartupInfo,ProcessInfo) then begin
          raise Exception.Create('Erro durante a execução do comando
'+cCmd);
       end;
       repeat
          xApp := WaitForSingleObject(ProcessInfo.hProcess,100);
          Application.ProcessMessages;
       until (xApp <> WAIT_TIMEOUT);
       repeat
          nRead := 0;
          ReadFile(ReadPipe,xBuf[0],2400,nRead,nil);
          xBuf[nRead]:= #0;
          OemToAnsi(xBuf,xBuf);
          Result := Result + string(xBuf);
       until (nRead < 2400);
       FreeMem(xBuf);
       CloseHandle(ProcessInfo.hProcess);
       CloseHandle(ProcessInfo.hThread);
       CloseHandle(ReadPipe) ;
       CloseHandle(WritePipe) ;
    end else begin
       if not
CreateProcess(nil,zAppName,nil,nil,false,NORMAL_PRIORITY_CLASS,nil,
nil,StartupInfo,ProcessInfo) then begin
          raise Exception.Create('Erro durante a execução do comando
'+cCmd);
       end;
       Application.ProcessMessages;
       WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
       GetExitCodeProcess(ProcessInfo.hProcess,nInt);
       Application.ProcessMessages;
       Result := IntToStr(nInt);
    end;
end;

I hope it helps you.

Eduardo

Fri, Aug 31 2012 1:17 PMPermanent Link

Uli Becker

Eduardo,

> I have used a following routine to capture the out of any application into a
> string and then send it back to client browser.
>
> cOut := ExecAndWait('c:\webserver\scripts\php.exe parameters',True)

That works without any problems

muito obrigado!

Uli
Image