Solved

DOS/Windows interaction

Posted on 1998-01-27
10
332 Views
Last Modified: 2013-12-03
I need a nice code sample for writing and reading from a DOS window. What I'm trying to do is open a dos console program, send characters to it, and read its output as it's printed. Preferred language is Visual Basic, but C++ is okay too. Please feel free to use Win32 API calls. Thanks.
--
Matt Nicholson
mnichols@cs.fit.edu
0
Comment
Question by:mnichols
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 4

Expert Comment

by:tflai
ID: 1410432
1. Incorporate the following code to your project.
2. Call RedirectIOToConsole() at initialization.
3. Use "fprintf(stdout, ...)", fprintf(stdin, ...)", "<<", or ">>" to do your input/output to that DOS window.
-------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "GuiCon.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifndef _USE_OLD_IOSTREAMS
//using namespace std;
#endif

// Maximum number of lines the output console should have.
static const WORD MAX_CONSOLE_LINES = 500;

#define _GUICON_DEBUG
#ifdef _GUICON_DEBUG

void RedirectIOToConsole()
  {
    int                         hConHandle;
    long                        hStdHandle;
    CONSOLE_SCREEN_BUFFER_INFO  conInfo;
    FILE                        *fp;

    // Allocate a console for this application.
    AllocConsole();

    // Set the screen buffer to be big enough to let us scroll text.
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &conInfo);
    conInfo.dwSize.Y = MAX_CONSOLE_LINES;
    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), conInfo.dwSize);

    // Redirect unbuffered STDOUT to the console.
    hStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
    hConHandle = _open_osfhandle(hStdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "w" );
    *stdout = *fp;
    setvbuf( stdout, NULL, _IONBF, 0 );

    // Redirect unbuffered STDIN to the console.
    hStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
    hConHandle = _open_osfhandle(hStdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "r" );
    *stdin = *fp;
    setvbuf( stdin, NULL, _IONBF, 0 );

    // Redirect unbuffered STDERR to the console.
    hStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
    hConHandle = _open_osfhandle(hStdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "w" );
    *stderr = *fp;
    setvbuf( stderr, NULL, _IONBF, 0 );

    // Make cout, wout, cin, wcin, wcerr, cerr, wclog, and clog
    // point to console as well.
//    ios::sync_with_stdio();
  }

#endif

// End of File
0
 
LVL 1

Author Comment

by:mnichols
ID: 1410433
Okay, thanks for the code. Now, can anyone help me translate this into code usable in Visual Basic? Unfortunately, I'm writing a Visual Basic program that requires this functionality. The only reason why I accepted C++ was that usually I can translate it over to VB if it just consists of Win32 API calls.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1410434
I'm not too familiar with VB, but the Win32 API's should be the same as defined in "WINCON.H" header file.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 4

Expert Comment

by:tflai
ID: 1410435
Just in case you wonder what's my "GuiCon.h", here it is:
---------------------------------------------------
#ifndef _GUICON_H
#define _GUICON_H

#include <windows.h>
#include <wincon.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

#include <iostream.h>
#include <fstream.h>

void RedirectIOToConsole();

#endif

// End of File
0
 
LVL 1

Author Comment

by:mnichols
ID: 1410436
See, I understand your C++ code quite well. However, Visual Basic doesn't have a standard out or in. it's not like you define an input and output source to print to (be it a console, the printer, a file, etc). so like you get the standard input and output handles, but it's not like you can attach it to good ole cout and cin.
0
 
LVL 2

Accepted Solution

by:
shogi earned 150 total points
ID: 1410437
You can't translate that directly in Visual Basic, like setvbuf() is a runtime C librairie, and good luck to write that in VB.

  The best is to compile that piece of code in a DLL and use it directly in VB, like any other DLL call.
0
 
LVL 4

Expert Comment

by:tflai
ID: 1410438
All that code and no points.  tsk, tsk...  :-(
0
 
LVL 1

Expert Comment

by:TheMadManiac
ID: 1410439
if it makes you feel any better tflai, i think you gave the right answer, especially since C++ was ok too
0
 
LVL 1

Author Comment

by:mnichols
ID: 1410440
just wanted to close it on up. sorry, tflai thanks for the code, but i'd only accept it if it were usable to me. unfortunately i can't just insert the raw c++ code into a VB program. i was just hoping i could translate, but i can't.-- Matt
0
 

Expert Comment

by:Gregor_the_Great
ID: 7665025
I have been looking for something like this, myself.
I found a workable solution, in VB, at:

http://www.internettrash.com/users/fdb/cons.htm

Check it out!
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question