[RESOLVED] Communicate two executables through the console
Hello, I am using a code that calls a console executable with command lines, reads its data and tells me when it ends, fragments of the code that I use so that you have an idea
Code:
' Call this sub to execute and capture a console app.
' Ex: Call ExecAndCapture("ping localhost", Text1)
Public Sub ExecAndCapture(ByVal sCommandLine As String, _
cTextBox As TextBox, _
Optional ByVal sStartInFolder As _
String = vbNullString)
Const BUFSIZE As Long = 1024 * 10
Dim hPipeRead As Long
Dim hPipeWrite As Long
Dim sa As SECURITY_ATTRIBUTES
Dim si As STARTUPINFO
Dim pi As PROCESS_INFORMATION
Dim baOutput(BUFSIZE) As Byte
Dim sOutput As String
Dim lBytesRead As Long
With sa
.nLength = Len(sa)
.bInheritHandle = 1 ' get inheritable pipe
' handles
End With 'SA
If CreatePipe(hPipeRead, hPipeWrite, sa, 0) = 0 Then
Exit Sub
End If
With si
.cb = Len(si)
.dwFlags = STARTF_USESHOWWINDOW Or _
STARTF_USESTDHANDLES
.wShowWindow = SW_HIDE ' hide the window
.hStdOutput = hPipeWrite
.hStdError = hPipeWrite
End With 'SI
If CreateProcess(vbNullString, sCommandLine, ByVal 0&, _
ByVal 0&, 1, 0&, ByVal 0&, sStartInFolder, si, pi) _
Then
Call CloseHandle(hPipeWrite)
Call CloseHandle(pi.hThread)
hPipeWrite = 0
Do
DoEvents
If ReadFile(hPipeRead, baOutput(0), BUFSIZE, _
lBytesRead, ByVal 0&) = 0 Then
Exit Do
End If
sOutput = Left$(StrConv(baOutput(), vbUnicode), _
lBytesRead)
cTextBox.SelText = sOutput
Loop
Call CloseHandle(pi.hProcess)
End If
' To make sure...
Call CloseHandle(hPipeRead)
Call CloseHandle(hPipeWrite)
End Sub
Now, my question is the following, I would like to make another program that makes vb6 that can send that information, that is, write the console and that can be read with the previous code, I want to communicate two executables but only "using this method"
I tried these two codes but they didn't work, I'm on window x64
https://www.tek-tips.com/faqs.cfm?fid=5647
https://docs.microsoft.com/es-ES/tro...i-applications
Re: Communicate two executables through the console
Blatant self-promotion: https://github.com/PeterSwinkels/Console-Shell - I believe this program does almost exactly what you need. If not, let me know and I will see what I can come up with. Okay?
Re: Communicate two executables through the console
Quote:
Originally Posted by
Peter Swinkels
hello, the code you provide is something similar to what I already have, what I need is the other part, let's say create the console executable.
In the links that I published above it would seem to be the solution but for some reason they do not work for me
Code:
sWriteBuffer = "Hola mundo"
hStdOut = stdout()
WriteFile hStdOut, sWriteBuffer, Len(sWriteBuffer) + 1, lBytesWritten
In windows 10x64bits hStdOut = 0
In windows 7x32bits hStdOut = 7 but lBytesWritten=0
Re: Communicate two executables through the console
I am not sure what you mean, but try this: https://github.com/PeterSwinkels/Console. Why not pm me with an explanation of what you need in Spanish (your native language, correct?). I could read it using Google Translate.
Re: Communicate two executables through the console
Try this function
Code:
Public Sub ConsolePrint(sText As String)
Const InIde As Boolean = False '-- make this a function
Const StdOut As Long = 1
Static oStdOut As Object
If InIde Then
Debug.Print sText;
Else
If oStdOut Is Nothing Then
With CreateObject("Scripting.FileSystemObject")
Set oStdOut = .GetStandardStream(StdOut)
End With
End If
oStdOut.Write sText
End If
End Sub
This will work only if you project has
Code:
[VBCompiler]
LinkSwitches=/SUBSYSTEM:CONSOLE
. . . manually added to the .vbp file.
Btw, GetStandardStream returns a TextStream object which has a lot of methods/properties which can be used to read input too so you might want to call these directly and skip this wrapper function altogether.
Noticably the TextStream object is missing a Flush function and your problem with console I/O seems to be connected with buffering in some way.
cheers,
</wqw>
Re: Communicate two executables through the console
Here's a drop-in Console Class that I have been using for many years.
From the web site;
Quote:
The whole enchilada. If you want to write a real console application, using Classic VB, this is the ticket. Create a new project, set it to start with Sub Main, drop the MConsole.bas file into your application, and you're almost ready to rock. This sample provides complete support for writing and testing console applications within the IDE.
Quote:
Of course, redirection and pipes are fully supported as well. Here's a snippet showing how to read standard input, and send it directly to the clipboard. (This is the heart of a neat little utility I wrote that allows you to capture the output of any console application and place it on the clipboard.) This snippet also demonstrates output of debugging information, output to standard error, and the issuance of an exitcode upon completion:
Site: Karl E. Peterson's Classic VB Code: Console
Joe
Re: Communicate two executables through the console
Quote:
Originally Posted by
Joe Caverly
I was about to comment that it's impossible for a win32 subsystem executable to completely emulate console subsystem executable because cmd.exe will *not* wait for the win32 executable to finish execution unlike console subsystem executable which is waited upon for complete termination before continuing current command line or batch of execution and then I found out the "Required Modification of Executable" paragraph and realized what a total clutch this whole module of Karls has become actually!
Don't use it!
Karl himself would have *not* used it provided that he knew about the undocumented LinkSwitches option. This single options can mark every executable into a console subsystem straigh out of VBIDE linker already (and much more) and requires tons of *less* code with just a couple of API calls to operate on the now OS provided console and virtually no APIs when using the VBScript built-in TextStreams for everything console related.
Again, there is nothing this module brings that is not already available from the VBScript built-in TextStreams.
cheers,
</wqw>
Re: Communicate two executables through the console
Quote:
Originally Posted by
wqweto
Try this function
Code:
Public Sub ConsolePrint(sText As String)
Const InIde As Boolean = False '-- make this a function
Const StdOut As Long = 1
Static oStdOut As Object
If InIde Then
Debug.Print sText;
Else
If oStdOut Is Nothing Then
With CreateObject("Scripting.FileSystemObject")
Set oStdOut = .GetStandardStream(StdOut)
End With
End If
oStdOut.Write sText
End If
End Sub
This will work only if you project has
Code:
[VBCompiler]
LinkSwitches=/SUBSYSTEM:CONSOLE
. . . manually added to the .vbp file.
Btw,
GetStandardStream returns a
TextStream object which has a lot of methods/properties which can be used to read input too so you might want to call these directly and skip this wrapper function altogether.
Noticably the TextStream object is missing a Flush function and your problem with console I/O seems to be connected with buffering in some way.
cheers,
</wqw>
Yes!!, That works perfect! Thank you very much, excellent trick, now trying the previous links adding those lines to the .vbp they also work, now that I read better again I realize that dilettante had suggested modifying the exe so that it works, but I like it more that it can be done do from the .vbp file.
thank you very much weqeto and peter swinkels
Re: Communicate two executables through the console
Quote:
Originally Posted by
Peter Swinkels
Strange. You can't control your console program(https://github.com/PeterSwinkels/Console-Shell) with your console shell (https://github.com/PeterSwinkels/Console-Shell).may be vb6 console program have some bugs?
Re: [RESOLVED] Communicate two executables through the console
@xxdoc13: I am not sure what you mean by controlling my program with itself. It is possible my description of it is lacking. What it is supposed to do is allow you to launch console based programs and interact with them from inside a GUI. If it does that there are no bugs, but I will revise the description to avoid future misunderstandings.
Re: [RESOLVED] Communicate two executables through the console
@xxdoc13: I updated Console Shell's description at Github and while the program worked as I had intended after testing it I did find there was room for improvement. If you have any suggestions I might be able to implement them. A new version should be available soon.
An updated version has been uploaded now.