Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying a DOS box in VB Forms

Status
Not open for further replies.

afryer

Programmer
Mar 9, 2004
207
GB
Hi All,

Does anyone know a control that mimics the DOS box (displayed by entering CMD). I am looking, ideally for control over this so that I can send commands and parse the output from these commands (even using something as simple as myDosBox.Text).

Any advice would be welcome.

Andrew
 
What is the actual goal here?

Do you want a form that has such a "box" as a control covering part of the form? The regular Textbox in multiline mode should work in multiline mode. Just monitor keys for the Enter and when you see it take the last "line" from the .Text property. "Print" to it by appending text to the .Text property.

It is also possible to write a VB5/6 program to be a console application with no forms at all. You have to make sure the resulting EXE is for "Subsystem Console" and not the usual "Subsystem Windows" but there are several ways to achieve that. The resulting program can be run in a command window or will come up in one when double-clicked in Explorer.

See and many other articles on the web.
 
What you're looking to do is pretty tough. I believe strongm put some code up several months ago on this, and there were some interesting pitfalls and wrinkles. I haven't a clue how to find it, though; perhaps someone can help.

Bob
 
Maybe I was looking at this inside-out.

Are you looking for something in a control that your program uses to talk to a program it runs externally? Something like "shelling" the external program but being able to write to the program input and read the program output?

This isn't really hard.

One limitation you'll run into though is that many Windows console programs do not read and write the Std I/O streams. Instead they do Console Device I/O, so simple redirection to anonymous pipes will not establish the 2-way communication you may want.

An example is the FTP client supplied with Windows XP (FTP.EXE).
 
Hi,

Basically what I am looking for is a standard Windows form with an embedded DOS box in it. I am realising rapidly that it is not easy to do and I have played around with removing the forms to the extent that I do have a console project running. This is not quite what I need however because I need to be able to enter DOS commands into the embedded object (for example enter the dir command and get the list of directories).

I can use the shell command and also I have a module that I used previously to spawn a cmd session, run a application and capture the output, but the problem with this one is that the new application I am trying to use does not seem to have standard output for some reason and I can't capture it either using my module or by trying to pipe it to a file in DOS.

I am trying different ways of doing it now, but thanks for everyones help.

Incidentally in .NET it seems a whole lot easier to do, but I am using VB6 unfortunately.

Andrew
 
afryer,

This is off the top of my head but;
Setup a text box on the form into which you type your DOS commands with a doit button next to it. Below that have a multiline text box for results/ output.

So you enter your 'DOS' command into the first textbox - press doit - your code then Shells the command to cmd.ex or command .com and redirects output to a file - your code then waits until the file containing the output shows up - when the file is found its content is displayed in the multiline text box. Should work for DIR at least.

Use white text on a black background and it may even look convincing.

hugh
 
afryer,

Something like this with a few bells and whistles may do it for you.

Option Explicit
DefInt A-Z

Private Sub Command1_Click()

Dim f%, a$

If Len(Dir$("mycom.my")) Then Kill "mycom.my"

Shell "CMD.EXE /C " & Text1.Text & " > mycom.my"

Do Until Len(Dir$("mycom.my"))
DoEvents
Loop

f = FreeFile
Open "mycom.my" For Input As f
a$ = Input$(LOF(f), f)
Close f

Text2 = a$

End Sub

regards Hugh
 
thread222-339817 Here's what I was looking for, or something like it. strongm has quite a few threads on this.

HTH

Bob
 
As I said about FTP.EXE, many console programs do I/O against the Console Device and don't use Std I/O streams at all. There are both high and low level I/O APIs that can be used to conduct conversations via one or several screen buffers.

I'm not aware of any technique to "hook" these API calls for a given process. Even if you could it would probably require that you implement most of the available functionality in order to get everything to work right.

I have an ActiveX DLL I wrote that is used to give a CScript (typically written in VBScript) access to a custom screen buffer that is divided into various "panels" to provide a sort of GUI interface within a console window (or full screen). The result is a lot like a VBDOS application. No way this I/O is going to be intercepted via pipes - it isn't routed via any stream file such as stdin/stdout at all.

I can't think of any trick to embed as console window as a VB control within a VB form. The best thing you have is to allocate a console and let the user interact with that (separate) window.

strongm's comment in the thread referenced above discusses the topic I mentioned earlier: creating a VB program that interacts with a console via Std I/O streams. Such programs can be redirected via anonymous pipes. I'm not sure how that gets you anywhere toward your goal though.

I can't imagine how doing "this" in .Net buys you anything at all either. True, it is easy to write a VB.Net console program, but it is pretty trivial in VB anyway:

modMain.bas
Code:
'Requires a reference to Microsoft Scripting Runtime.
Sub Main()
    Dim FSO As New Scripting.FileSystemObject
    Dim sin As Scripting.TextStream
    Dim sout As Scripting.TextStream
    Dim strWord As String
    
    Set sin = FSO.GetStandardStream(StdIn)
    Set sout = FSO.GetStandardStream(StdOut)
    sout.WriteLine "Hello!"
    sout.WriteLine "What's the word?"
    strWord = sin.ReadLine()
    sout.WriteLine "So, the word is " & strWord
    Set sout = Nothing
    Set sin = Nothing
End Sub

You compile the program and then edit the EXE using any of several techniques. I like to use a short WScript:

LinkConsole.vbs
Code:
Option Explicit
'LinkConsole.vbs
'
'This is a WSH script used to make it easier to edit
'a compiled VB6 EXE using LINK.EXE to create a console
'mode program.
'
'Drag the EXE's icon onto the icon for this file, or
'execute it from a command prompt as in:
'
'        LinkConsole.vbs <EXEpath&file>
'
'Be sure to set up strLINK to match your VB6 installation.

Dim strLINK, strEXE, WSHShell

strLINK = _
  """C:\Program Files\Microsoft Visual Studio\VB98\LINK.EXE"""
strEXE = """" & WScript.Arguments(0) & """"
Set WSHShell = CreateObject("WScript.Shell")

WSHShell.Run _
  strLINK & " /EDIT /SUBSYSTEM:CONSOLE " & strEXE

Set WSHShell = Nothing
WScript.Echo "Complete!"

Voila! A simple VB program that can be run from a console and interact in character mode. No API calls required at all.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top