Solved

DOS/Windows interaction

Posted on 1998-01-27
10
323 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now