Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Returning string to stdout from a vb.net gui app?

Returning string to stdout from a vb.net gui app?

Scheduled Pinned Locked Moved Visual Basic
csharptutorialquestion
12 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dave Kreskowiak

    First, what verison of VB.NET are you using? Let's see the code that created your console and the code that is trying to write to it.

    Dave Kreskowiak Microsoft MVP - Visual Basic

    S Offline
    S Offline
    Sam Marrocco
    wrote on last edited by
    #3

    vb.net 2005. As for "code that created the console", I'm not sure of what you are asking: For testing, I'm creating a standard windows form application (hence as I mentioned it being gui-based and not console-based). Call it MyApp for discussion sake. Somewhere in the code (in this case within the Form_Load event) I have placed a console.writeline("hello world") statement. I was expecting this to display to the console when the program is run from a console, such as: c:\>MyApp and display hello world accordingly. However, nothing is ever displayed on the command line after the program quits. I'm guessing at this point that console.writeline doesn't really write to the console if the app is gui/form based, even if it is called from the command line....?

    D 1 Reply Last reply
    0
    • D Dave Kreskowiak

      First, what verison of VB.NET are you using? Let's see the code that created your console and the code that is trying to write to it.

      Dave Kreskowiak Microsoft MVP - Visual Basic

      S Offline
      S Offline
      Sam Marrocco
      wrote on last edited by
      #4

      Upon rereading your post, a light went on in my head. You might be assuming that *I* am creating the console in which my code is running. The console is a standard DOS shell (cmd.exe) that is run by the user. *My Application*, while gui-based, is being run from that command shell as a commandline. I'm trying to get my app to print text to the shell from which it was called. In C, the closest thing would be a printf(). Hope that helps....

      D 1 Reply Last reply
      0
      • S Sam Marrocco

        vb.net 2005. As for "code that created the console", I'm not sure of what you are asking: For testing, I'm creating a standard windows form application (hence as I mentioned it being gui-based and not console-based). Call it MyApp for discussion sake. Somewhere in the code (in this case within the Form_Load event) I have placed a console.writeline("hello world") statement. I was expecting this to display to the console when the program is run from a console, such as: c:\>MyApp and display hello world accordingly. However, nothing is ever displayed on the command line after the program quits. I'm guessing at this point that console.writeline doesn't really write to the console if the app is gui/form based, even if it is called from the command line....?

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #5

        smarr wrote:

        I was expecting this to display to the console when the program is run from a console

        Doesn't work that way. The console is not part of the process you launched and has absolutely nothing to do with the process at all, at any point in time. You can have multiple consoles open at once, so that app has no way of knowing which one to write to!

        Dave Kreskowiak Microsoft MVP - Visual Basic

        S 1 Reply Last reply
        0
        • D Dave Kreskowiak

          smarr wrote:

          I was expecting this to display to the console when the program is run from a console

          Doesn't work that way. The console is not part of the process you launched and has absolutely nothing to do with the process at all, at any point in time. You can have multiple consoles open at once, so that app has no way of knowing which one to write to!

          Dave Kreskowiak Microsoft MVP - Visual Basic

          S Offline
          S Offline
          Sam Marrocco
          wrote on last edited by
          #6

          I'll wait to respond until I've sure you've read the second response I made....I don't think I was being very clear previously. I'm pretty sure that my vb task should be able to respond to the parent shell window...just not sure how ;)

          D 1 Reply Last reply
          0
          • S Sam Marrocco

            Upon rereading your post, a light went on in my head. You might be assuming that *I* am creating the console in which my code is running. The console is a standard DOS shell (cmd.exe) that is run by the user. *My Application*, while gui-based, is being run from that command shell as a commandline. I'm trying to get my app to print text to the shell from which it was called. In C, the closest thing would be a printf(). Hope that helps....

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #7

            If you don't create the Console window, or at least grab a handle to an existing console window, you can't write to it.

            smarr wrote:

            I'm trying to get my app to print text to the shell from which it was called. In C, the closest thing would be a printf().

            Since you created a Windows Forms app, there is no such thing as a console that is attached to it automatically. Your app has to create one, or find an existing one. This was easy in C because your C apps were console-based, non-GUI apps and ran in the context of the DOS-emulator running in that console window. The consoles Standard In/Out/Error streams are at the disposable of the console app that is launched. This concept doesn't apply to Windows Forms apps since it has no use for those streams. You can create your own Console window using:

            Declare Auto Function AllocConsole Lib "kernel32" () As IntPtr
                .
                .
                .
            AllocConsole()
            Console.WriteLine("This is a test...")
            

            Dave Kreskowiak Microsoft MVP - Visual Basic

            S 1 Reply Last reply
            0
            • S Sam Marrocco

              I'll wait to respond until I've sure you've read the second response I made....I don't think I was being very clear previously. I'm pretty sure that my vb task should be able to respond to the parent shell window...just not sure how ;)

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #8

              smarr wrote:

              I'm pretty sure that my vb task should be able to respond to the parent shell window

              No, it can't. Not unless you specifically go and get a handle for any existing console window. It's just easier to create your own...

              Dave Kreskowiak Microsoft MVP - Visual Basic

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                If you don't create the Console window, or at least grab a handle to an existing console window, you can't write to it.

                smarr wrote:

                I'm trying to get my app to print text to the shell from which it was called. In C, the closest thing would be a printf().

                Since you created a Windows Forms app, there is no such thing as a console that is attached to it automatically. Your app has to create one, or find an existing one. This was easy in C because your C apps were console-based, non-GUI apps and ran in the context of the DOS-emulator running in that console window. The consoles Standard In/Out/Error streams are at the disposable of the console app that is launched. This concept doesn't apply to Windows Forms apps since it has no use for those streams. You can create your own Console window using:

                Declare Auto Function AllocConsole Lib "kernel32" () As IntPtr
                    .
                    .
                    .
                AllocConsole()
                Console.WriteLine("This is a test...")
                

                Dave Kreskowiak Microsoft MVP - Visual Basic

                S Offline
                S Offline
                Sam Marrocco
                wrote on last edited by
                #9

                Hate to keep repeating it, but I cannot create the console: It needs to be an existing shell in which the user executes the gui app, then the string is returned to the (same) shell. It's like the event handlers injected into a gui app disable the "console-ness" of it.

                D 1 Reply Last reply
                0
                • S Sam Marrocco

                  Hate to keep repeating it, but I cannot create the console: It needs to be an existing shell in which the user executes the gui app, then the string is returned to the (same) shell. It's like the event handlers injected into a gui app disable the "console-ness" of it.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #10

                  OK. Well, the console you launch your app from is not attached in any way shape or form to a Windows Forms app. In other words, the parent process of the Windows Forms app is NOT the console process that launched it. There is no way for a Windows Forms app to know which console window launched it. It's exactly the same as if you launched it by double clicking the icon or typed its command line into Start/Run. So, the only way to get the console window attached to your app upon launch is if you write your app as a Console Application, then create and launch your Form using Application.Run(). You'll have to create public properties in your form so you can get any return values out of it. Something like this:

                  Imports System
                  Imports System.Windows.Forms
                   
                  Module Module1
                  Sub Main()
                  Dim myForm As New Form1
                  Console.WriteLine("Application startup...")
                   
                  ' Application.Run is a blocking call...
                  System.Windows.Forms.Application.Run(myForm)
                   
                  ' so, this statement won't execute until the form is closed.
                  Console.WriteLine(myForm.ReturnValue)
                  End Sub
                  End Module
                   
                  Public Class Form1
                  Inherits Form
                   
                  Shared Sub Main()
                  End Sub
                   
                  Private _returnValue As String
                   
                  Public ReadOnly Property ReturnValue() As String
                  Get
                  Return _returnValue
                  End Get
                  End Property
                   
                  Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
                  Console.WriteLine("You can write from the console from inside your form.")
                  _returnValue = "This is the return value stored in the form..."
                  End Sub
                  End Class

                  Dave Kreskowiak Microsoft MVP - Visual Basic

                  S 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    OK. Well, the console you launch your app from is not attached in any way shape or form to a Windows Forms app. In other words, the parent process of the Windows Forms app is NOT the console process that launched it. There is no way for a Windows Forms app to know which console window launched it. It's exactly the same as if you launched it by double clicking the icon or typed its command line into Start/Run. So, the only way to get the console window attached to your app upon launch is if you write your app as a Console Application, then create and launch your Form using Application.Run(). You'll have to create public properties in your form so you can get any return values out of it. Something like this:

                    Imports System
                    Imports System.Windows.Forms
                     
                    Module Module1
                    Sub Main()
                    Dim myForm As New Form1
                    Console.WriteLine("Application startup...")
                     
                    ' Application.Run is a blocking call...
                    System.Windows.Forms.Application.Run(myForm)
                     
                    ' so, this statement won't execute until the form is closed.
                    Console.WriteLine(myForm.ReturnValue)
                    End Sub
                    End Module
                     
                    Public Class Form1
                    Inherits Form
                     
                    Shared Sub Main()
                    End Sub
                     
                    Private _returnValue As String
                     
                    Public ReadOnly Property ReturnValue() As String
                    Get
                    Return _returnValue
                    End Get
                    End Property
                     
                    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
                    Console.WriteLine("You can write from the console from inside your form.")
                    _returnValue = "This is the return value stored in the form..."
                    End Sub
                    End Class

                    Dave Kreskowiak Microsoft MVP - Visual Basic

                    S Offline
                    S Offline
                    Sam Marrocco
                    wrote on last edited by
                    #11

                    I now have an alternative.....Dave, this may be similiar to what you are suggesting, although it does seem to work despite some of the issues you raised. Someone on another list suggested changing the apptype to Console App *after* the app has already been created. I didn't want to change the app drastically and get involved with the whole main/app.run stuff. Plus, I have several utility programs that can benefit from this. Apparently by creating the app normally (as a windows application) and using console.writeline to write to the IDE console for debugging can be the starting point. Afterwards, the app type can be changed (in the IDE Properties for the project) to Console Application. At this point, all the existing infrastructure still works, but console.writeline will now output to something other than the IDE console window. If the app is run from the IDE, it will automatically create a shell window and output to that. However, if the app is run from an existing shell, output will be sent to that window (as expected since this is the shell window that spawned the app) and the app runs as a gui-based application. The best of both worlds. The only bad side is that if the app is run from an icon, it will open a shell for it's output. This is acceptable for me since the app will always be run from an existing shell app. Thanks for the help Dave!

                    D 1 Reply Last reply
                    0
                    • S Sam Marrocco

                      I now have an alternative.....Dave, this may be similiar to what you are suggesting, although it does seem to work despite some of the issues you raised. Someone on another list suggested changing the apptype to Console App *after* the app has already been created. I didn't want to change the app drastically and get involved with the whole main/app.run stuff. Plus, I have several utility programs that can benefit from this. Apparently by creating the app normally (as a windows application) and using console.writeline to write to the IDE console for debugging can be the starting point. Afterwards, the app type can be changed (in the IDE Properties for the project) to Console Application. At this point, all the existing infrastructure still works, but console.writeline will now output to something other than the IDE console window. If the app is run from the IDE, it will automatically create a shell window and output to that. However, if the app is run from an existing shell, output will be sent to that window (as expected since this is the shell window that spawned the app) and the app runs as a gui-based application. The best of both worlds. The only bad side is that if the app is run from an icon, it will open a shell for it's output. This is acceptable for me since the app will always be run from an existing shell app. Thanks for the help Dave!

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #12

                      You can do that. All you're doing is changing the startup code environment. How you're launching the form code (automatically in your case) is very similar to what I put up in the last post. The results look exactly the same, right down to the console window popping up if you double-click the icon. Now you know how to do it yourself instead of relying on a procedure that might not work in the next version of VS.NET.

                      Dave Kreskowiak Microsoft MVP - Visual Basic

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups