Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using stdout in VB 6 via the FSO

377 views
Skip to first unread message

peter cabus

unread,
Feb 27, 2000, 3:00:00 AM2/27/00
to
Hi all,

According to the documentation on the Microsoft WEB site (MSDN) it should be
possible to use textstreams via the FileSystem object to read from and write
to StdIn and StdOut. When I try to do this I always get an error saying
that the handle is invalid.

Code I used:

Dim objFileSystem As New FileSystemObject
Dim objTextStream As TextStream
Dim evStdOut As StandardStreamTypes

With objFileSystem
evStdOut = StdOut
Set objTextStream = .GetStandardStream(evStdOut)
objTextStream.Write "test"
End With

Any ideas?

Michael Harris

unread,
Feb 27, 2000, 3:00:00 AM2/27/00
to
There was a discussion sparked by this back when that Scripting Clinic article first appeared...  Your VB program needs to execute as a console application.  The perception was that there was an "undocumented VB compile switch" that let you create VB based console applications, but apparently that wasn't true (at least on one ever disclosed what the magic switch was ;-)...
 
There is a way to attach a console window to a VB application as documented by this KB article:
 
Q171654 - HOWTO: Attach a Console Window to Your Visual Basic Program
http://support.microsoft.com/support/kb/articles/Q171/6/54.asp
 
The KB example code uses kernel32 API calls to allocate a console, get a handle to stdout, and to write to the console.  You *may* be able to allocate the console as illustrated in the example and then use the FSO.GetStandardStream in place of GetStdHandle() to get an FSO TextStream object. 
 
I qualified this with *may* because no one's ever posted that they have done this successfully.

--
Michael Harris
MVP - Windows Script
 
 
"peter cabus" <peter...@pandora.be> wrote in message news:u9NcfUSg$GA.191@cppssbbsa04...
Hi Jef,

Have a look at:
http://msdn.microsoft.com/workshop/languages/clinic/scripting061499.asp

Extract from the article:

"Visual Basic user bonus:

The stdin/stdout/stderr support is actually implemented in the
FileSystemObject, so you can now access stdin/stdout/stderr from any Visual
Basic program. As a Visual Basic programmer, I've been waiting for ages for
this functionality. "






Jeff Mastry <jma...@sc.rr.com> wrote in message
news:Uu6u4.26364$gW5.1...@typhoon.southeast.rr.com...
> Interesting - I've never seen GetStandardStream documented anywhere
before.
> Thanks.
>
> Anyway, you're getting this error because your program doesn't have a
> console - and therefore no StdIn, StdOut or StdErr. You can duplicate this
> problem with VBScript by running this script with and without cscript.exe:
>
> ===
> Const StdIn = 0
> Const StdOut = 1
> Const StdErr = 2
>
> Dim fso, out
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set out = fso.GetStandardStream( StdOut )
> out.Write "Test"
> ===
>
> without cscript.exe you will get the exact same error: The handle is
> invalid, but with cscript.exe it works as expected. I may be wrong, but my
> guess is you need to allocate a console before you can do this in VB. I
> searched MSDN but didn't find an article that mentions this technique for
> VB. Could you post the address?
>
> Jeff
>
> "peter cabus" <peter...@pandora.be> wrote in message
> news:Odpk5mPg$GA.237@cppssbbsa04...

Jeff Mastry

unread,
Feb 28, 2000, 3:00:00 AM2/28/00
to
Works for me! I took the code from the Q article, added a reference to the
scripting runtime, and modified Form_Load() like this:

Private Sub Form_Load()
If AllocConsole() Then
hConsole = GetStdHandle(STD_OUTPUT_HANDLE)
If hConsole = 0 Then MsgBox "Couldn't allocate STDOUT"

Dim fso As New FileSystemObject
Dim out As TextStream
Set out = fso.GetStandardStream(1)
out.Write "This is a test!" & vbCrLf
out.Close

Else
MsgBox "Couldn't allocate console"
End If
End Sub

Works great!

Jeff

"Michael Harris" <Please...@To.NewsGroup> wrote in message
news:excDPyXg$GA....@cppssbbsa02.microsoft.com...

0 new messages