Results 1 to 11 of 11

Thread: [RESOLVED] Communicate two executables through the console

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    439

    Resolved [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
    Last edited by LeandroA; Mar 28th, 2021 at 06:23 PM.
    leandroascierto.com Visual Basic 6 projects

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    439

    Re: Communicate two executables through the console

    Quote Originally Posted by Peter Swinkels View Post
    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?
    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
    leandroascierto.com Visual Basic 6 projects

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,152

    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>

  6. #6
    Addicted Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    133

    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;
    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.
    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
    Last edited by Joe Caverly; Mar 29th, 2021 at 07:15 AM.

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,152

    Re: Communicate two executables through the console

    Quote Originally Posted by Joe Caverly View Post
    Here's a drop-in Console Class that I have been using for many years.

    From the web site;


    Site: Karl E. Peterson's Classic VB Code: Console

    Joe
    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>

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    439

    Re: Communicate two executables through the console

    Quote Originally Posted by wqweto View Post
    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
    leandroascierto.com Visual Basic 6 projects

  9. #9
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    678

    Re: Communicate two executables through the console

    Quote Originally Posted by Peter Swinkels View Post
    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?
    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?
    Last edited by xxdoc123; Mar 29th, 2021 at 08:00 PM.

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.

  11. #11
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    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.
    Last edited by Peter Swinkels; Apr 1st, 2021 at 08:06 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width