Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DOS/Windows interaction

Posted on 1998-01-27
10
Medium Priority
?
342 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

885 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