Solved

DOS/Windows interaction

Posted on 1998-01-27
10
324 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

18 Experts available now in Live!

Get 1:1 Help Now