Main Area and Open Discussion > General Software Discussion
Need examples of Win console apps that yield useful data
kyrathaba:
So, for the last few weeks I've been writing a book (via my private DokuWiki and simultaneously in .epub and .prc formats) about some of the things I've learned to do with C# over the last decade. I'm at a place in the book where I show how to capture output from some other process that is initiated by my C# app. I'm looking for a couple of Windows command-line programs that are complex enough to yield data not just-as-easily-derived by the calling application. It's okay if the C# app needs to do some string parsing, etc., with the returned data. I'm just wanting to give a good example of not re-inventing the wheel.
Ath:
For my NANY 2015 entry SedTester I found these resources/examples:
http://stackoverflow.com/questions/285760/how-to-spawn-a-process-and-capture-its-stdout-in-net
http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results
That's what I based my call-out to sed.exe on. That piece of code is quite specific (and poorly documented ;D), so I hope this enough for now.
kyrathaba:
Thanks, Ath! Appreciate it.
KodeZwerg:
I havent checked out the link from Ath, when using Delphi to capture console, we work with pipes.
I show you how i do it that the output do not lag / it display everything like console would display it, and not just the final result.
FYI: In Delphi, a TMemo is a control that can contain Text.
--- Code: Delphi ---procedure CaptureConsoleOutput(const ACommand, AParameters: String; AMemo: TMemo); const CReadBuffer = 2400; var saSecurity: TSecurityAttributes; hRead: THandle; hWrite: THandle; suiStartup: TStartupInfo; piProcess: TProcessInformation; pBuffer: array[0..CReadBuffer] of Char; dRead: DWord; dRunning: DWord; begin saSecurity.nLength := SizeOf(TSecurityAttributes); saSecurity.bInheritHandle := True; saSecurity.lpSecurityDescriptor := nil; if CreatePipe(hRead, hWrite, @saSecurity, 0) then begin FillChar(suiStartup, SizeOf(TStartupInfo), #0); suiStartup.cb := SizeOf(TStartupInfo); suiStartup.hStdInput := hRead; suiStartup.hStdOutput := hWrite; suiStartup.hStdError := hWrite; suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW; suiStartup.wShowWindow := SW_HIDE; if CreateProcess(nil, PChar(ACommand + ' ' + AParameters), @saSecurity, @saSecurity, True, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup, piProcess) then begin repeat dRunning := WaitForSingleObject(piProcess.hProcess, 100); Application.ProcessMessages(); repeat dRead := 0; ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil); pBuffer[dRead] := #0; OemToAnsi(pBuffer, pBuffer); AMemo.Lines.Add(String(pBuffer)); until (dRead < CReadBuffer); until (dRunning <> WAIT_TIMEOUT); CloseHandle(piProcess.hProcess); CloseHandle(piProcess.hThread); end; CloseHandle(hRead); CloseHandle(hWrite); end;end;I hope it helps.
kyrathaba:
Thanks, KodeZwerg.
I've got a pretty decent grasp of the code. What I'm looking for are examples of Windows CLI programs whose output it could be useful to capture from within another program.
Navigation
[0] Message Index
[#] Next page
Go to full version