Solved

DOS/Windows interaction

Posted on 1998-01-27
10
325 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

778 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