DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Need examples of Win console apps that yield useful data  (Read 9,694 times)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Need examples of Win console apps that yield useful data
« on: September 03, 2018, 10:25 PM »
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.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Need examples of Win console apps that yield useful data
« Reply #1 on: September 04, 2018, 07:09 PM »
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.
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Need examples of Win console apps that yield useful data
« Reply #2 on: September 04, 2018, 09:33 PM »
Thanks, Ath! Appreciate it.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • Participated in NANY Challenge Four Times
    • Coding Snacks
Re: Need examples of Win console apps that yield useful data
« Reply #3 on: September 05, 2018, 05:22 AM »
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

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Need examples of Win console apps that yield useful data
« Reply #4 on: September 05, 2018, 11:26 AM »
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.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • Participated in NANY Challenge Four Times
    • Coding Snacks
Re: Need examples of Win console apps that yield useful data
« Reply #5 on: September 05, 2018, 09:25 PM »
You mean like "net.exe" or "robocopy.exe" or "upx.exe" ?

x16wda

  • what am I doing in this handbasket?
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
    • Participated in NANY Challenge Once
Re: Need examples of Win console apps that yield useful data
« Reply #6 on: September 06, 2018, 01:05 AM »
I have some rexx code that uses tslistusers.exe (from here) and some wmic.exe commands to produce a nice web page showing who's logged into the rds farm desktops and what remoteapps they're running. I do a good bit of scripting and use a lot of little utilities (eg, unxutils) and grab and parse output if I need it.
vi vi vi - editor of the beast

Mark0

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 654
Re: Need examples of Win console apps that yield useful data
« Reply #7 on: September 09, 2018, 06:13 PM »
I humbly propose my TrID tool. :)
Your example could be a GUI interface for it.

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: Need examples of Win console apps that yield useful data
« Reply #8 on: September 11, 2018, 10:00 PM »
Thanks x16wda and Mark0
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Original address: https://www.donationcoder.com/forum/index.php?topic=46069.0