jak pobrac dane z programu dosowego?

0

To co w temacie, czyli jesli mam np. dosowy program, ktory po uruchomieniu wyswietla w konsoli/na ekranie kilka linii tekstu, to jak je pobrac programem w delphi. W plikach wsadowych/dosie dziala polecenie program.exe > plik.txt, ale gdy stworzylem taki plik wsadowy i uruchamialem delphim ( shellapi lub winexec ) to w plik.txt nic sie nie wyswietalalo ( gdy uruchamialem plik wsadowy "recznie" to plik.txt zapelnia sie zawartoscia ekranu, zapelnionym przez program.exe

0

Odpal
command.com /c program.exe > plik
lub
cmd.exe /c program.exe > nplik

W zależności od tego, który masz system (nt i pochodne - cmd)

// strumienie i fajki są obsługiwane przez shelle, nie są właściwościa systemu.

0

Kod ten chyba pare razy krążył po forum.

function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile, 
  ErrMsg: string): Boolean; 
const 
  ROUTINE_ID = '[function: CreateDOSProcessRedirected ]'; 
var 
  OldCursor: TCursor; 
  pCommandLine: array[0..MAX_PATH] of Char; 
  pInputFile, pOutPutFile: array[0..MAX_PATH] of Char; 
  StartupInfo: TStartupInfo; 
  ProcessInfo: TProcessInformation; 
  SecAtrrs: TSecurityAttributes; 
  hAppProcess, hAppThread, hInputFile, hOutputFile: THandle; 
begin 
  Result := False; 

  if not FileExists(InputFile) then 
    raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 + 'Input file * %s *' + #10 + 'does not exist' + #10 + #10 + ErrMsg, [InputFile]); 

  OldCursor := Screen.Cursor; 
  Screen.Cursor := crHourglass; 

  StrPCopy(pCommandLine, CommandLine); 
  StrPCopy(pInputFile, InputFile); 
  StrPCopy(pOutPutFile, OutputFile); 

  try 

    FillChar(SecAtrrs, SizeOf(SecAtrrs), #0); 
    SecAtrrs.nLength := SizeOf(SecAtrrs); 
    SecAtrrs.lpSecurityDescriptor := nil; 
    SecAtrrs.bInheritHandle := True; 

    hInputFile := CreateFile(pInputFile,GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, @SecAtrrs, OPEN_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0);            
                      
    if hInputFile = INVALID_HANDLE_VALUE then 
      raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 + 'WinApi function CreateFile returned an invalid handle value' + #10 + 'for the input file * %s *' + #10 + #10 + ErrMsg, [InputFile]); 

    { create the appropriate handle for the output file } 
    hOutputFile := CreateFile(pOutPutFile,  { pointer to name of the file }  GENERIC_READ or GENERIC_WRITE, { access (read-write) mode } FILE_SHARE_READ or FILE_SHARE_WRITE, @SecAtrrs, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, 0);  { handle to file with attributes to copy } 

    if hOutputFile = INVALID_HANDLE_VALUE then 
      raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 + 'WinApi function CreateFile returned an invalid handle value' + #10 + 'for the output file * %s *' + #10 + #10 + ErrMsg, [OutputFile]); 

    FillChar(StartupInfo, SizeOf(StartupInfo), #0); 
    StartupInfo.cb := SizeOf(StartupInfo); 
    StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES; 
    StartupInfo.wShowWindow := SW_HIDE; 
    StartupInfo.hStdOutput := hOutputFile; 
    StartupInfo.hStdInput := hInputFile; 

    Result := CreateProcess(nil, pCommandLine, nil,nil, True, CREATE_NEW_CONSOLE or REALTIME_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);           
    if Result then 
    begin 
      WaitForSingleObject(ProcessInfo.hProcess, INFINITE); 
      hAppProcess := ProcessInfo.hProcess; 
      hAppThread  := ProcessInfo.hThread; 
    end 
    else 
      raise Exception.Create(ROUTINE_ID + #10 + #10 + 'Function failure' + #10 + #10 + ErrMsg); 

  finally 
    if hOutputFile <> 0 then CloseHandle(hOutputFile); 
    if hInputFile <> 0 then CloseHandle(hInputFile); 
    if hAppThread <> 0 then CloseHandle(hAppThread); 
    if hAppProcess <> 0 then CloseHandle(hAppProcess); 
    { restore the old cursor } 
    Screen.Cursor := OldCursor; 
  end; 
end; 

http://www.torry.net

// Patrz, po wywaleniu spacji i sformatowaniu jest czytelniejszy i juz nie rozwala strony :] [mf]

0

Flabra, twoja metoda nie dziala gdy wywolam command.com z opcja SW_HIDE, a nie chce aby wyskakiwala konsola dosa.

Co do tej funkcji, to co mam podac jako plik wejsciowy skoro program ktory chce uruchomic sam sobie dobiera plik wejsciowy?

Teraz nadal nikt nie zna odpowiedzi?

1 użytkowników online, w tym zalogowanych: 0, gości: 1