0

I want to play with MySQL process and get what ever it write to console so I write this code in FreePascal:

I want to control MySQl and read & write whatever.

    Process := TProcess.Create(nil);
      with Process do
      begin
        Executable := 'C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysql.exe';
        with Parameters do
        begin
          Options := [poUsePipes];
          Add('-u');
          Add('root');
          Add('-p');
        end;
        Execute;
    sleep(1000);
    WriteLn(Process.Output.NumBytesAvailable); // will be 0 but it write "Enter password"
  WriteLn(Process.Stderr.NumBytesAvailable);    // will be 0 but it write "Enter password"  
      end;  

TProcess is a component that control executing other programs,I even test Delphi codes but all result are the same.

But the problem is that this will freeze because there is no output but console window write :

Enter password:

How can I get this in my application and all others?

As I said I want to work with MySQL executable and read from and write in it, So I dont want to use its library or any other DB component.

EDIT: Here is a Delphi version of my test from here with the same result :

function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var
  SA: TSecurityAttributes;
  SI: TStartupInfo;
  PI: TProcessInformation;
  StdOutPipeRead, StdOutPipeWrite: THandle;
  WasOK: Boolean;
  Buffer: array[0..255] of AnsiChar;
  BytesRead: Cardinal;
  WorkDir: string;
  Handle: Boolean;
begin
  Result := '';
  with SA do begin
    nLength := SizeOf(SA);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
  try
    with SI do
    begin
      FillChar(SI, SizeOf(SI), 0);
      cb := SizeOf(SI);
      dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
      hStdOutput := StdOutPipeWrite;
      hStdError := StdOutPipeWrite;
    end;
    WorkDir := Work;
    Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine),
                            nil, nil, True, 0, nil,
                            PChar(WorkDir), SI, PI);
    CloseHandle(StdOutPipeWrite);
    if Handle then
      try
        repeat
          WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
          if BytesRead > 0 then
          begin
            Buffer[BytesRead] := #0;
            Result := Result + Buffer;
          end;
        until not WasOK or (BytesRead = 0);
        WaitForSingleObject(PI.hProcess, INFINITE);
      finally
        CloseHandle(PI.hThread);
        CloseHandle(PI.hProcess);
      end;
  finally
    CloseHandle(StdOutPipeRead);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.text:=GetDosOutput('"C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin\mysql.exe" -u root -p');
end;

It will freeze in WasOK line . Also I tested many other codes for this subject and neither worked.

Community
  • 1
  • 1
user3656830
  • 63
  • 1
  • 7
  • A quick solution can be by Adding password in the script by security wise it is not recommended as now your password will be exposed... After Add('-p');Add('password'); – Naruto Dec 05 '15 at 18:27
  • No this is not the point.I want to get that message and all things after it.Just giving the password is not the answer.Think it as virtual console for MySQL. – user3656830 Dec 05 '15 at 19:31
  • 1
    Call CreateProcess. Create two pipes for input and output. Connect the appropriate end of each pipe to the new process standard handles. Read and write from and to the other ends of the pipes. – David Heffernan Dec 05 '15 at 20:48
  • You think I didnt that? Sure I did and sure it will freeze as I said because there is nothing in output.tested in FPC and Delphi. and put FPC code because TProcess class is more stable. – user3656830 Dec 05 '15 at 21:35

0 Answers0