PDA

Click to See Complete Forum and Search --> : windows forms app - output to console, how?


MrPolite
Oct 20th, 2006, 10:20 PM
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?

Negative0
Oct 21st, 2006, 08:21 AM
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.

wossname
Oct 21st, 2006, 09:14 AM
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.

Techno
Oct 21st, 2006, 10:53 AM
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

MrPolite
Oct 21st, 2006, 11:23 AM
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 :D aah I'm being picky i guess

MrPolite
Oct 21st, 2006, 11:25 AM
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:(

Fromethius
Oct 21st, 2006, 11:33 AM
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.

Techno
Oct 21st, 2006, 11:40 AM
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.

MrPolite
Oct 23rd, 2006, 02:00 AM
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 :D I will in a few hours though


haah I am picky :D what can I do

MrPolite
Oct 23rd, 2006, 02:04 AM
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 :D and I believe i've see that (not with .NET apps)
any suggestions?

wossname
Oct 23rd, 2006, 06:38 AM
I'm not entirely convinced that this is possible under Windows. Not without resorting to the API that is.

MrPolite
Oct 23rd, 2006, 10:42 AM
I'm not entirely convinced that this is possible under Windows. Not without resorting to the API that is.
I dont mind API :D

Negative0
Oct 23rd, 2006, 08:27 PM
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):


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.

MrPolite
Oct 25th, 2006, 02:28 AM
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 :D

I'll try it tomorrow morning
thank you!