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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output to stdout 1

Status
Not open for further replies.

BFreshour

Programmer
Mar 20, 2002
84
0
0
Does anyone know how to output to stdout using VB? And no, I don't want to spawn a new console (i.e. use AllocConsole method of the WScript object). I want to use the originating command console that I ran my executable from. And no, I don't want to write the program in C++.
Also, I'm not using .Net, I'm using VB6.

----
This isn't really my question but a question I got from a friend of mine trying to perform this. I thought I might ask here.
 
It depends on what you are trying to do. Are you trying to send information to a program running in the console window? If so, the SENDKEYS function will do most of what you want.
 
Take a look at cgi4vb a freeware library that has a lot more than you need.

But one thing it does that you are asking about, is to write to stdout. It does this because as a CGI which is its main purpose you need to write to stdout to send a web page back to a browser from the web server.

The whole thing is pretty small, and you will definitely find an example that you can hack into your desired shape.

David Wertman
Web Champ
 
strongm ? Are you betting the info is not there or that the (original) poster cannot take from the reference and ' ... can hack into your desired shape ... '?

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
The cgi4bin code is designed to work from a webpage. In other words, it assumes that the program has STDIN and STDOUT, and relies on the fact that, as a CGI program, STDIN and STDOUT exist. Essentially, the same trick can be pulled by adding a reference to the Windows Script Control Object Model library, which allows you to get references to STDIN, STDOUT and STDERR as TextStream objects, and then read and write to them via the FileSystemObject - and it suffers the same problem; only really works in a CGI/scripting environment. This is because a VB program in the normal Windows environment doesn't have STDIN or STDOUT (or STDERR, for that matter) since the VB startup code deliberately eliminates them. Now, you can partially get around this by doing an AllocConsole (which BFreshour specifically states he does not want to do), but even then you are somewhat limited in that you can only write to STDOUT of the newly allocated console - when what you really want to be doing is writing to the STDIN of A.N. Other application that is using that console (and you really want to be reading the STDOUT of that application, rather than reading the STDIN of the console).

So you have three problems:

1) VB program doesn't actually have STDIN, STDOUT or STDERR.
2) if you address this through an AllocConsole, you get a STDIN, STDOUT and STDERR for a console, not for any other application that might also use that console (for argument's sake let's say command.com/cmd.exe)
3) Even if you do manage to get those streams for the application, then the file permissions on them are wrong - the STDIN of the application is read-only, when you actually want to write it, and STDOUT of the application is write-only when you want to read it

Now, the ONLY lines of code relevant to all this in cg14win are:

hStdIn = GetStdHandle(STD_INPUT_HANDLE)
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)

which are then used by the ReadFile and WriteFile APIs.

I'd suggest that this neither provides the necessary info originally asked for, nor is it a sufficient basis for being hacked into the necessary shape.

 
Bottom line...

What in VB is the equivalient to Java's System.out.println("exciting")?

I've created a exe from this example - more info then I need.


I need the simplest example...I have code that grabs the input params when calling the exe. How come there's not a simple what the exe spits out, print it out
(some kind of SetCommandLine()).

Function GetCommandLine(Optional MaxArgs) As String()
Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
Dim ArgArray() As String
If IsMissing(MaxArgs) Then MaxArgs = 10
ReDim ArgArray(MaxArgs)
NumArgs = 0: InArg = False
CmdLine = Command()
CmdLnLen = Len(CmdLine)
'Debug.Print (CmdLine)
For I = 1 To CmdLnLen
C = Mid(CmdLine, I, 1)
Debug.Print ("Character = " & C)
If (C <> &quot; &quot; And C <> vbTab) Then
If Not InArg Then
If NumArgs = MaxArgs Then Exit For
NumArgs = NumArgs + 1
'Debug.Print (&quot;NumArgs =&quot; & NumArgs)
InArg = True
End If
ArgArray(NumArgs) = ArgArray(NumArgs) & C
Else
InArg = False
End If
Next I
ReDim Preserve ArgArray(NumArgs)
MsgBox (ArgArray(0))
GetCommandLine = ArgArray

Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
Bottom line: VB doesn't have an equivalent.

Java's PrintLn sends output to the STDOUT stream and, if you have been following this thread, you'll be aware that VB doesn't have any of the standard streams.

The Microsoft Scripting Runtime library, however (as mentioned above) does have a built-in ability to write to use the standard streams - and when used from commandline VBScript this works fine (because you have a console) - but simply adding a reference to the library does not give VB a console. So u=you have to do it yourself

The code below is the simplest possible example of putting all this together. You'll need a form with a command button, and you'll need to add a reference to the Microsoft Scripting Runtime library:
[tt]
Option Explicit
Private Declare Function AllocConsole Lib &quot;kernel32&quot; () As Long
Private Declare Function FreeConsole Lib &quot;kernel32&quot; () As Long

Private Sub Command1_Click()
PrintLn &quot;Hello&quot;
End Sub

Private Sub Form_Load()
AllocConsole
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
FreeConsole
End Sub

Public Sub PrintLn(strString As String)
With New FileSystemObject
.GetStandardStream(StdOut).WriteLine strString
End With
End Sub
 
Thanks! I'll try it out, don't need a form or button, just need to call a exe and output string(s) to the SAME console window, no spawning of new console windows :)

Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
The form and button was just for the sake of illustrating the point.

As to your actual requirement, I'm afraid you are likely to be doomed to disappointment. The simple solution I've given above won't work in your scenario; when a VB is launched from a console the startup code severs all it's links with that console, since VB apps are always GUI apps. You have to do an AllocConsole to get a console Window, or start playing with AttachConsole which, I should point out, is only available on XP and W2003, and in turn requires that you identify the process ID that owns the current console. As you can see, it rapidly begins to get more complicated.

If you do a keyword search in this forum, you should find several of my posts that deal with these problems (including a technique that turns VB applications into genuine console applications)

Also it is worth pointing out that you can create genuine console applications with VB.NET
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top