Question

Changing the Screen Buffer Size of a spawned console application.

Asked by: ThievingSix

Mostly this interrupts the readability of the console window output. I piped the console output to a memo but noticed that the text is looking like this:

"CHKDSK is verifying files (stage 1 of
3)..."

Now when I run it in a console window in which I have changed the screen buffer its all one line. Now chkdsk is only a test, this is going to be used to log errors from a java program outputting in a console window. What I want to do is change the screen buffer size.

Any ideas?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-01-31 at 18:34:02ID24102097
Topic

Delphi Programming

Participating Experts
2
Points
500
Comments
5

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. pipes and buffering
    Is there a way to turn off buffering from a process that is read in as a pipe. (Other than useing $| =1 or FILE->autoflush(1) ) ? I use a tail to monitor some files that are being appended to, and the data seems to be buffered....I am not getting the most recent data, and ...
  2. Spawn w/ output redirection
    I am trying to spawn a program and redirect any output (stdout & stderr) to a file (on Windows). The main program also has to have the option to wait for the spawned program or run on without waiting. Currently, I am using _spawnl to execute the spawned program. But as s...
  3. ReadConsoleInput Interrupt
    I am in desperate need of finding a way to interrupt a ReadConsoleInput() function. This is in a console application. I am using Dev-C++ as a .c program. Ideally I would use the alarm() function, but it does not seem to be available in Dev. I have run across SetTimer, and...
  4. Pipe
    Why this won't work? #include <stdio.h> char c[3][15] ={"/bin/ls", "usr/bin/wc", "/usr/bin/wc"}; int main() { int i, j; int thisin; int pid[2]; char temp[10]; while ( 1 ) { fflush(stdin); fgets(te...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Geert_GruwezPosted on 2009-02-03 at 00:08:19ID: 23535023

 

by: rllibbyPosted on 2009-02-03 at 14:03:06ID: 23542852

Hard to say without seeing your code, but in order to preserve the output line breaks, your going to need to buffer the pipe data.

For example

- in a single read you get: "CHKDSK is verifying files (stage 1 of"
- You then either (a) append this data directly to the memo or (b) use a temp stringlist, set the text with the data, and call AddStrings to the memo.
- you read again, and get: "3)..."#10#13
- you repeat step 2.

You end up with 3 lines that really should be one. Again, not sure if this is your case, but I CAN tell you that regardless of console dimensions, the CRLF gets read when reading from the pipe.

To correct, you should be buffering this data until (a) you get a LF (#10) or (b) the program is done. If (a), then copy what you have up to and including the line feed, then trim this text and THEN add to the memo. Then delete the buffer up to and including the line feed, and wait for more data. When the progam is done, if buffer is not empty, then add the final text to the memo (for cases where you don't get a final LF).

Russell

 

by: ThievingSixPosted on 2009-02-04 at 14:23:51ID: 23554396

This is the code I am using.

It seems that if I do something like this:

procedure TDosBoxThread.UpdateMemo;
begin
  AMemo.Text := AMemo.Text + FOutData;
  //AMemo.Lines.Add(FOutData);
end;

I get it as all of one line: "CHKDSK is verifying files (stage 1 of 3)...". The problem then would be the percent listings(e.g. "0 percent completed.") do not translate into multiple lines as they do not have a line feed after them which is why I choose the commented approach.

unit uDosBoxThread;
 
interface
 
uses
  Windows, SysUtils, Classes, StdCtrls;
 
type
  TDosBoxThread = class(TThread)
  private
    FOutData : String;
    FProcessInformation : TProcessInformation;
    procedure UpdateMemo;
    procedure RunDosInMemo(DosApp: String);
  protected
    procedure Execute; override;
  public
    AMemo : TMemo;
    DosFileName : String;
  end;
 
  TDosBox = class(TObject)
  private
    FDosBoxThread : TDosBoxThread;
    FTerminateOnFinish : Boolean;
  protected
    procedure OnTerminate(Sender: TObject);
    function ThreadedDosInMemo(DosFileName: String; AMemo: TMemo): TDosBoxThread;
  public
    property DosBoxThread : TDosBoxThread read FDosBoxThread write FDosBoxThread;
    constructor Create(DosFileName: PAnsiChar; AMemo: TMemo; TerminateOnFinish: Boolean = True);
    destructor Destroy; override;
  end;
 
implementation
 
uses uFunctions;
 
constructor TDosBox.Create(DosFileName: PAnsiChar; AMemo: TMemo; TerminateOnFinish: Boolean = True);
begin
  inherited Create;
  FTerminateOnFinish := TerminateOnFinish;
  FDosBoxThread := ThreadedDosInMemo(DosFileName,AMemo);
end;
 
destructor TDosBox.Destroy;
begin
  FreeAndNil(FDosBoxThread);
  inherited Destroy;
end;
 
function TDosBox.ThreadedDosInMemo(DosFileName: String; AMemo: TMemo): TDosBoxThread;
begin
  Result := TDosBoxThread.Create(True);
  Result.AMemo := AMemo;
  Result.DosFileName := DosFileName;
  Result.FreeOnTerminate := True;
  Result.OnTerminate := OnTerminate;
  Result.Resume;
end;
 
procedure TDosBox.OnTerminate(Sender: TObject);
begin
  Try
    If FTerminateOnFinish Then
      begin
      CloseAppOrTerminate(FDosBoxThread.FProcessInformation.hProcess,FDosBoxThread.FProcessInformation.dwProcessId);
    end;
  Finally
    CloseHandle(FDosBoxThread.FProcessInformation.hProcess);
    CloseHandle(FDosBoxThread.FProcessInformation.hThread);
  end;
end;
 
procedure TDosBoxThread.RunDosInMemo(DosApp: String);
const
  ReadBuffer = 4096;
var
  SecurityAttributes : TSecurityAttributes;
  ReadPipe, WritePipe : DWORD;
  StartupInfo : TStartupInfo;
  ProcessInformation : TProcessInformation;
  Buffer : PChar;
  BytesRead : DWORD;
  BytesToRead : DWORD;
  BytesWritten : DWORD;
begin
  With SecurityAttributes Do
    begin
    nLength := SizeOf(TSecurityAttributes);
    bInheritHandle := True;
    lpSecurityDescriptor := nil;
  end;
  If CreatePipe(ReadPipe,WritePipe,@SecurityAttributes,0) Then
    begin
    Try
      Buffer := AllocMem(ReadBuffer + 1);
      If Buffer = nil Then LastError;
      Try
        FillChar(StartupInfo,Sizeof(StartupInfo),0);
        StartupInfo.cb := SizeOf(StartupInfo);
        StartupInfo.hStdOutput := WritePipe;
        StartupInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE);
        StartupInfo.hStdError := WritePipe;
        StartupInfo.dwFlags := STARTF_USESTDHANDLES OR STARTF_USESHOWWINDOW;
        StartupInfo.wShowWindow := SW_HIDE;
        If CreateProcess(nil,PChar(DosApp),@SecurityAttributes,@SecurityAttributes,True,NORMAL_PRIORITY_CLASS,nil,nil,StartupInfo,ProcessInformation) Then
          begin
          CloseHandle(WritePipe);
          FProcessInformation := ProcessInformation;
          Repeat
            BytesRead := 0;
            BytesToRead := 0;
            If Not(PeekNamedPipe(ReadPipe,nil,0,nil,@BytesToRead,nil)) Then
              begin
              Break;
            end;
            If BytesToRead > 0 Then
              begin
              If Not(ReadFile(ReadPipe,Buffer^,BytesToRead,BytesRead,nil)) Then LastError;
              OemToChar(Buffer,Buffer);
              SetLength(FOutData,BytesRead);
              Move(Buffer^,FOutData[1],BytesRead);
              If Pos('Press any key to continue . . .',FOutData) > 0 Then
                begin
                Buffer[0] := #13;
                Buffer[1] := #0;
                CharToOem(Buffer,Buffer);
                WriteFile(WritePipe,Buffer^,2,BytesWritten,nil);
                FOutData := 'Program has ended!';
              end;
              Synchronize(UpdateMemo);
            end;
          Until ((GetExitCodeProcess(ProcessInformation.hProcess) <> STILL_ACTIVE) And (BytesToRead = 0));
        end
        Else
          begin
          LastError;
        end;
      Finally
        FreeMem(Buffer);
      end;
    Finally
      CloseHandle(ReadPipe);
    end;
  end
  Else
    begin
    LastError;
  end;
end;
 
procedure TDosBoxThread.UpdateMemo;
begin
  AMemo.Lines.Add(FOutData);
end;
 
procedure TDosBoxThread.Execute;
begin
  RunDosInMemo(DosFileName);
end;
 
end.
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:

Select allOpen in new window

 

by: rllibbyPosted on 2009-02-05 at 10:42:03ID: 23562541

The pct's don't end in a line feed, but they do end in a carriage return. The wrapper class code below can be used to correctly line break the data. I wasn't able to test it using your code (missing defines, etc), but I did test it using my threaded TPipeConsole component.

Notes:

 To handle both CR and LF, the object should be constructed as:
 var = TFormattedOutput.Create([#10, #13]);

 Use BeginUpdate(Lines: TStrings), eg: BeginUpdate(AMemo.Lines) before using the Add().

 Use Add(Data: String) to append the data. Line break handling will be done using the chars you specify in the create.

 When your thread is done, use EndUpdate() to flush any final remaining data.

-----
Russell

type
  TAnsiCharSet      =  Set of AnsiChar;

type
  TFormattedOutput  =  class(TObject)
  private
     // Private declaration
     FChars:        TAnsiCharSet;
     FLines:        TStrings;
     FBuffer:       String;
  protected
     // Protected declaration
     procedure      Clear;
     function       ScanChars(Source: PChar): PChar;
  public
     // Public declaration
     constructor    Create(TermChars: TAnsiCharSet);
     procedure      Add(Data: String);
     procedure      BeginUpdate(Lines: TStrings);
     procedure      EndUpdate;
  end;

implementation

procedure TFormattedOutput.Add(Data: String);
var  lpszBuffer:    PChar;
     lpszScan:      PChar;
     szLine:        String;
     dwSize:        Integer;
begin

  // Check list
  if Assigned(FLines) then
  begin
     // Append data to current buffer
     FBuffer:=FBuffer + Data;
     // Check length of text
     if (Length(FBuffer) > 0) then
     begin
        // Set size to remove
        dwSize:=0;
        // Setup pointer to string
        lpszBuffer:=Pointer(FBuffer);
        // Lock the lines
        FLines.BeginUpdate;
        // Resource protection
        try
           // Seed the first scan
           lpszScan:=ScanChars(lpszBuffer);
           // While we have a line feed
           while Assigned(lpszScan) do
           begin
              // Push past the char
              Inc(lpszScan);
              // While term chars
              while (lpszScan^ > #0) and (lpszScan^ in FChars) do Inc(lpszScan);
              // Update size of data to remove
              Inc(dwSize, lpszScan - lpszBuffer);
              // Set the line to process
              SetString(szLine, lpszBuffer, lpszScan - lpszBuffer);
              // Trim trailing whitespace and add line
              FLines.Add(TrimRight(szLine));
              // Update current parse point
              lpszBuffer:=lpszScan;
              // Seed the next scan
              lpszScan:=ScanChars(lpszBuffer);
           end;
           // Check size, remove from start of buffer
           if (dwSize > 0) then Delete(FBuffer, 1, dwSize);
        finally
           // Unlock the lines
           FLines.EndUpdate;
        end;
     end;
  end;

end;

procedure TFormattedOutput.BeginUpdate(Lines: TStrings);
begin

  // Clear current
  Clear;

  //  Save lines
  FLines:=Lines;

end;

procedure TFormattedOutput.EndUpdate;
begin

  // Resource protection
  try
     // Check list and remaining buffer
     if (Assigned(FLines) and (Length(FBuffer) > 0)) then
     begin
        // Add final line of data
        FLines.Add(FBuffer);
     end;
  finally
     // Clear current
     Clear;
  end;

end;

function TFormattedOutput.ScanChars(Source: PChar): PChar;
begin

  // Set default result
  result:=nil;

  // Check source
  if Assigned(Source) then
  begin
     // While not end of string
     while (Source^ > #0) do
     begin
        // Check for chars
        if (Source^ in FChars) then
        begin
           // Located char
           result:=Source;
           // Done processing
           break;
        end;
        // Push next char
        Inc(Source);
     end;
  end;

end;

procedure TFormattedOutput.Clear;
begin

  // Clear buffered data
  SetLength(FBuffer, 0);

  // Clear current list
  FLines:=nil;

end;

constructor TFormattedOutput.Create(TermChars: TAnsiCharSet);
begin

  // Perform inherited
  inherited Create;

  // Set initial defaults
  FChars:=TermChars;

  // Clear remaining settings
  Clear;

end;

 

by: ThievingSixPosted on 2009-02-05 at 15:31:44ID: 31541392

Perfect, Thanks.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...