Results 1 to 14 of 14

Thread: windows forms app - output to console, how?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    windows forms app - output to console, how?

    Read it all..
    Just wondering how this could be done.
    I have a windows forms app, and i want it to output to the same console window that it was started from (I know i can call AllocConsole api and create a new window, but i want to output to the same window the program was run from)

    any suggestions?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: windows forms app - output to console, how?

    I don't think you can do this. I don't think the app has any knowledge of where it was called from, it could be called from a command line as easily as double clicking on it. The only way I think you could do this would be to write a console app that then calls your windows app. If something needs to be written, it is sent from your windows app to your console app, which then writes out to the console.

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: windows forms app - output to console, how?

    Maybe you could try redirecting the StandardOutput of the current process, then try writing a string to it. If the string appears in the same console then bingo.
    I don't live here any more.

  4. #4
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: windows forms app - output to console, how?

    right click project > properties
    select the application type to Console Application
    the application will launch the console window as well as your own application both at the same time and you can read and write to that console window

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: windows forms app - output to console, how?

    Quote Originally Posted by Techno
    right click project > properties
    select the application type to Console Application
    the application will launch the console window as well as your own application both at the same time and you can read and write to that console window
    yea but i dont want an extra console window open when they havent launched it from console aah I'm being picky i guess
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: windows forms app - output to console, how?

    ok redescribing it....
    i want the app to be in console mode if it's given a certain argument at startup,
    and i want it to be like a winforms app if it's passed no arguments.

    making it a console app project would create a console window even all the time
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: windows forms app - output to console, how?

    so.. the window starts in a console, and then a new window opens.. ( a windows form ) and then you type something in that window and it says that in the console? if so.. I think I know a way to do it.

  8. #8
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: windows forms app - output to console, how?

    your picky. LOL
    I guess one way would be to hide the console app by getting a handle on it or something? So when it launches with your winform app, you can hide it or something. As suggested perhaps you can redirect the output and input from your winform to a new console app but I still dont think that would quite work.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  9. #9

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: windows forms app - output to console, how?

    Quote Originally Posted by Techno
    your picky. LOL


    I guess one way would be to hide the console app by getting a handle on it or something? So when it launches with your winform app, you can hide it or something. As suggested perhaps you can redirect the output and input from your winform to a new console app but I still dont think that would quite work.
    hmm well to a new console app.... i think i could do that, even though i haven't tried yet I will in a few hours though


    haah I am picky what can I do
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  10. #10

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: windows forms app - output to console, how?

    Quote Originally Posted by Fromethius
    so.. the window starts in a console, and then a new window opens.. ( a windows form ) and then you type something in that window and it says that in the console? if so.. I think I know a way to do it.
    yea so like this, if I have myapp.exe:

    from windows, run myapp.exe... gui comes up. No console
    from windows, run a shortcut that calls myapp.exe -nogui. No GUI: it should open a command prompt and my app will output to that

    AND!!!

    -from command prompt, run myappp.exe... a windows form opens and that's it
    -from command prompt, run myapp.exe -nogui. Then no GUI would open up, and my app will be writing to the same console window it was opened from



    I'm being extremely picky, I know. But that's the behaviour I'm looking for and I believe i've see that (not with .NET apps)
    any suggestions?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: windows forms app - output to console, how?

    I'm not entirely convinced that this is possible under Windows. Not without resorting to the API that is.
    I don't live here any more.

  12. #12

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: windows forms app - output to console, how?

    Quote Originally Posted by wossname
    I'm not entirely convinced that this is possible under Windows. Not without resorting to the API that is.
    I dont mind API
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  13. #13
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: windows forms app - output to console, how?

    If you don't mind the console popping up for a nanosecond when you run your winforms, you could do something like this (just a proof of concept):

    Code:
        class Program
        {
            [DllImport("kernel32.dll")]
            public static extern Boolean FreeConsole();
            static void Main(string[] args)
            {
                if (args.Length > 0 && args[0] == "-nogui")
                {
                    Console.WriteLine("A");
                    Console.ReadLine();
    
                }
                else
                {
                    FreeConsole();
                    System.Windows.Forms.Application.Run(new Form1());
                }
            }
        }
    I set this up as a command line app.

  14. #14

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: windows forms app - output to console, how?

    aaa haha thanks for the idea! i didnt think of that
    I was using AllocConsole and FreeConsole to open/close a new one, but why did i not think of closing the existing one

    I'll try it tomorrow morning
    thank you!
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

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