topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 10:19 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Need examples of Win console apps that yield useful data
« on: September 03, 2018, 05: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.

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,612
    • View Profile
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #1 on: September 04, 2018, 02:09 PM »
For my NANY 2015 entry SedTester I found these resources/examples:
http://stackoverflow...re-its-stdout-in-net
http://stackoverflow...-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

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #2 on: September 04, 2018, 04:33 PM »
Thanks, Ath! Appreciate it.

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • View Profile
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #3 on: September 05, 2018, 12: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 [Select]
  1. procedure CaptureConsoleOutput(const ACommand, AParameters: String; AMemo: TMemo);
  2.  const
  3.    CReadBuffer = 2400;
  4.  var
  5.    saSecurity: TSecurityAttributes;
  6.    hRead: THandle;
  7.    hWrite: THandle;
  8.    suiStartup: TStartupInfo;
  9.    piProcess: TProcessInformation;
  10.    pBuffer: array[0..CReadBuffer] of Char;
  11.    dRead: DWord;
  12.    dRunning: DWord;
  13.  begin
  14.    saSecurity.nLength := SizeOf(TSecurityAttributes);
  15.    saSecurity.bInheritHandle := True;  
  16.    saSecurity.lpSecurityDescriptor := nil;
  17.  
  18.    if CreatePipe(hRead, hWrite, @saSecurity, 0) then
  19.    begin    
  20.      FillChar(suiStartup, SizeOf(TStartupInfo), #0);
  21.      suiStartup.cb := SizeOf(TStartupInfo);
  22.      suiStartup.hStdInput := hRead;
  23.      suiStartup.hStdOutput := hWrite;
  24.      suiStartup.hStdError := hWrite;
  25.      suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;    
  26.      suiStartup.wShowWindow := SW_HIDE;
  27.  
  28.      if CreateProcess(nil, PChar(ACommand + ' ' + AParameters), @saSecurity,
  29.        @saSecurity, True, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup, piProcess)
  30.        then
  31.      begin
  32.        repeat
  33.          dRunning  := WaitForSingleObject(piProcess.hProcess, 100);        
  34.          Application.ProcessMessages();
  35.          repeat
  36.            dRead := 0;
  37.            ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil);          
  38.            pBuffer[dRead] := #0;
  39.  
  40.            OemToAnsi(pBuffer, pBuffer);
  41.            AMemo.Lines.Add(String(pBuffer));
  42.          until (dRead < CReadBuffer);      
  43.        until (dRunning <> WAIT_TIMEOUT);
  44.        CloseHandle(piProcess.hProcess);
  45.        CloseHandle(piProcess.hThread);    
  46.      end;
  47.  
  48.      CloseHandle(hRead);
  49.      CloseHandle(hWrite);
  50.    end;
  51. end;
I hope it helps.

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #4 on: September 05, 2018, 06: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.

KodeZwerg

  • Honorary Member
  • Joined in 2018
  • **
  • Posts: 718
    • View Profile
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #5 on: September 05, 2018, 04:25 PM »
You mean like "net.exe" or "robocopy.exe" or "upx.exe" ?

x16wda

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 888
  • what am I doing in this handbasket?
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #6 on: September 05, 2018, 08:05 PM »
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: 652
    • View Profile
    • Mark's home
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #7 on: September 09, 2018, 01: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
    • View Profile
    • Donate to Member
Re: Need examples of Win console apps that yield useful data
« Reply #8 on: September 11, 2018, 05:00 PM »
Thanks x16wda and Mark0