473,240 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

Console app

Is it possible to have an app that is both a console app and a windows app?
If there are no command line switches then it will run as a console app, if
there are command line switches then it will be a windows app. If I make it
a windows app then I can't write to the console. If I make it a console app
then it starts a console if the user runs it from the start menu so from
what I see it's not possible.

Thanks in advance,
Michael
Aug 6 '07 #1
15 2740
Michael C wrote:
Is it possible to have an app that is both a console app and a windows app?
Yes. Just open a windows form from a console application.
If there are no command line switches then it will run as a console app, if
there are command line switches then it will be a windows app.
That's not possible. You can't turn a non-console application into one
after it has started, and you have to start the application before you
can check the command line.
If I make it
a windows app then I can't write to the console. If I make it a console app
then it starts a console if the user runs it from the start menu so from
what I see it's not possible.
You could have a console application start a windows version of the
program instead if it doesn't find anything in the command line.

You could fake the console window by opening a windows form that acts
like a console window.

--
Göran Andersson
_____
http://www.guffa.com
Aug 6 '07 #2
Michael:

When you build the project, you can specifiy whether the project is in
console mode. All this means is that Windows will create a default console
by default. I typically write WinForms applications that I specificy as
Console. This enables me to do things likes Console.WriteLine() in my
application to do simply tracing and debugging. When you turn off Console,
it simply means that Windows will not create a default Console.

As the previous reply states, you cannot choose this at run-time -- the
subsystem (Windows/Console) needs to be decided at compile/link time.

Note that from a WinForms-type application, you can always create your own
Console explicitly.

jpuopolo
"Michael C" <no****@nospam.comwrote in message
news:OF****************@TK2MSFTNGP05.phx.gbl...
Is it possible to have an app that is both a console app and a windows
app? If there are no command line switches then it will run as a console
app, if there are command line switches then it will be a windows app. If
I make it a windows app then I can't write to the console. If I make it a
console app then it starts a console if the user runs it from the start
menu so from what I see it's not possible.

Thanks in advance,
Michael


Aug 6 '07 #3

"Michael C" <no****@nospam.comwrote in message
news:OF****************@TK2MSFTNGP05.phx.gbl...
Is it possible to have an app that is both a console app and a windows
app? If there are no command line switches then it will run as a console
app, if there are command line switches then it will be a windows app. If
I make it a windows app then I can't write to the console. If I make it a
console app then it starts a console if the user runs it from the start
menu so from what I see it's not possible.
Yes. Write it as a windows app but tell the compiler it's a console app.
Then it will have access to the console, and it can refrain from actually
opening any windows if that's what you want.

- The other Michael C.
Aug 6 '07 #4
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
Yes. Write it as a windows app but tell the compiler it's a console app.
Then it will have access to the console, and it can refrain from actually
opening any windows if that's what you want.
Thanks for the reply. The problem with this approach is that when the user
starts the exe from say the start menu then a dos box appears. I'm presuming
it's not possible to do what I want because everything is done by the time
main gets called and I have no control over it before then.

Michael
Aug 6 '07 #5
Michael C wrote:
Is it possible to have an app that is both a console app and a windows app?
If there are no command line switches then it will run as a console app, if
there are command line switches then it will be a windows app. If I make it
a windows app then I can't write to the console. If I make it a console app
then it starts a console if the user runs it from the start menu so from
what I see it's not possible.
It is not easy, but if you are wiling to code for it, then it
can be done.

See the code below (it is not production grade, but it is a
starting point).

Arne

=============================================

using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace E
{
public class MainForm : Form
{
private Label lbl;
public MainForm()
{
InitializeComponent();
}
public void InitializeComponent()
{
lbl = new Label();
SuspendLayout();
lbl.Location = new Point(50, 50);
lbl.Size = new Size(200, 25);
lbl.Text = "This is a GUI app";
Text = "Main form";
Size = new Size(300, 300);
Controls.Add(lbl);
ResumeLayout(false);
}
}
public class MyConsole : IDisposable
{
private const uint STD_INPUT_HANDLE = 0xfffffff6;
private const uint STD_OUTPUT_HANDLE = 0xfffffff5;
private const uint STD_ERROR_HANDLE = 0xfffffff4;
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
[DllImport("kernel32.dll")]
public static extern int GetStdHandle(uint nStdHandle);
[DllImport("kernel32.dll")]
public static extern bool WriteConsole(int hConsoleOutput,
string lpBuffer,
int nNumberOfCharsToWrite,
ref int
lpNumberOfCharsWritten,
int lpReserved);
[DllImport("kernel32.dll")]
public static extern bool ReadConsole(int hConsoleInput,
StringBuilder lpBuffer,
int nNumberOfCharsToRead,
ref int lpNumberOfCharsRead,
int lpReserved);
private int stdin;
private int stdout;
public MyConsole()
{
AllocConsole();
stdin = GetStdHandle(STD_INPUT_HANDLE);
stdout = GetStdHandle(STD_OUTPUT_HANDLE);
}
public void WriteLine(string s)
{
int len = 0;
WriteConsole(stdout, s + "\r\n", s.Length + 2, ref len, 0);
}
public string ReadLine()
{
int len = 0;
StringBuilder sb = new StringBuilder();
ReadConsole(stdin, sb, 256, ref len, 0);
return sb.ToString(0, sb.Length - 2);
}
public void Dispose()
{
FreeConsole();
}
}
public class MainClass
{
[STAThread]
public static void Main(string[] args)
{
if(args[0] == "GUI")
{
Application.Run(new MainForm());
}
else if(args[0] == "Console")
{
using(MyConsole console = new MyConsole())
{
console.WriteLine("This is a console app");
}
}
}
}
}
Aug 7 '07 #6
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
It is not easy, but if you are wiling to code for it, then it
can be done.

See the code below (it is not production grade, but it is a
starting point).
Thanks for the reply. This is a good solution but isn't exactly what I need.
If someone types MyApp.exe Console at the command line then it will create a
new command window instead of using the existing one.

BTW, your code can be simplified by just calling AllocConsole and then using
the standard dotnet console commands.

Michael
Aug 7 '07 #7
Michael C wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
>It is not easy, but if you are wiling to code for it, then it
can be done.

See the code below (it is not production grade, but it is a
starting point).

Thanks for the reply. This is a good solution but isn't exactly what I need.
If someone types MyApp.exe Console at the command line then it will create a
new command window instead of using the existing one.
I thougth you said that the users would be starting the EXE from
the start menu ??

But if they have a console already, then use AttachConsole
instead of AllocConsole.

Arne
Aug 7 '07 #8
Michael C wrote:
BTW, your code can be simplified by just calling AllocConsole and then using
the standard dotnet console commands.
Actually I did not even try that.

I prefer not to write code that work or not work depending
on the implementation of the Console class.

Arne
Aug 7 '07 #9
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
I thougth you said that the users would be starting the EXE from
the start menu ??
They might start from the start menu but they might start from an existing
console.
But if they have a console already, then use AttachConsole
instead of AllocConsole.
Problem is by that time the console has already gone back to the prompt so
the users can type additional commands. I think something is needed to
modify the code that runs before main is called. This is probably possible
somehow but difficult.

Michael
Aug 7 '07 #10
Michael C wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
>It is not easy, but if you are wiling to code for it, then it
can be done.

See the code below (it is not production grade, but it is a
starting point).

Thanks for the reply. This is a good solution but isn't exactly what I need.
If someone types MyApp.exe Console at the command line then it will create a
new command window instead of using the existing one.
Hm.

I just tried.

On my system it does not create a new console window in that scenario.

Arne
Aug 7 '07 #11
Arne Vajhøj wrote:
Michael C wrote:
>"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk. ..
>>It is not easy, but if you are wiling to code for it, then it
can be done.

See the code below (it is not production grade, but it is a
starting point).

Thanks for the reply. This is a good solution but isn't exactly what I
need. If someone types MyApp.exe Console at the command line then it
will create a new command window instead of using the existing one.

Hm.

I just tried.

On my system it does not create a new console window in that scenario.
Nah. That is with /t:exe, but that gives a console with start run.

Arne
Aug 7 '07 #12
Michael C wrote:
Problem is by that time the console has already gone back to the prompt so
the users can type additional commands. I think something is needed to
modify the code that runs before main is called. This is probably possible
somehow but difficult.
You need a new /t: type that neither start a new process
or create a console window.

Arne
Aug 7 '07 #13

"Michael C" <no****@nospam.comwrote in message
news:un**************@TK2MSFTNGP05.phx.gbl...
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:O$**************@TK2MSFTNGP03.phx.gbl...
>Yes. Write it as a windows app but tell the compiler it's a console app.
Then it will have access to the console, and it can refrain from actually
opening any windows if that's what you want.

Thanks for the reply. The problem with this approach is that when the user
starts the exe from say the start menu then a dos box appears. I'm
presuming it's not possible to do what I want because everything is done
by the time main gets called and I have no control over it before then.
The other thing it could do is the Win32 AllocConsole call to get a console.
Aug 7 '07 #14
"Michael A. Covington" <lo**@ai.uga.edu.for.addresswrote in message
news:Of*************@TK2MSFTNGP04.phx.gbl...
>Thanks for the reply. The problem with this approach is that when the
user starts the exe from say the start menu then a dos box appears. I'm
presuming it's not possible to do what I want because everything is done
by the time main gets called and I have no control over it before then.

The other thing it could do is the Win32 AllocConsole call to get a
console.
I tried that and it does work but if you run the app from a console then it
creates a new console to send the text to, when the app closes the new
console disappears. If your app just sends a simple response then you just
get a flicker of this new console on the screen.
>

Aug 7 '07 #15
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
>On my system it does not create a new console window in that scenario.

Nah. That is with /t:exe, but that gives a console with start run.
Thanks for all the replies. Looks like it is not possible without doing some
sort of low level hacks to modify the compile process. I'd assume this is
possible but could be messy and I generally don't like adding to the
complexity of compiling an app.

Michael
Aug 7 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Dave | last post by:
Hi, I have done some research, trying to Clear The Screen in java code. The first option was the obv: system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n"); then i heard about this method:...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
7
by: shawnk | last post by:
Hello Everyone How do you format format numbers right-justified using Console.WriteLine(), i.e I need to line up numbers in vertical columns and the MSDN documentation is pretty poor Here is the...
5
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
5
by: Publicjoe | last post by:
I am working on a little app which uses colour in the console window. I have created a class to extend the console functionality but the ClearScreen method does not work correctly. I am enclosing a...
17
by: MumboJumbo | last post by:
Hi I have a really basic question hopefully some can help me with: Can you write a (i.e. one) C# project that works from the cmd line and gui? I seems if i write a GUI app it can't write to...
5
by: portroe | last post by:
Hi I am using console.Writeline in my simple program. I do not however see anything happening in the output window when I debug, there are also no error messages, Has anybody a tip on what...
3
by: julianmoors | last post by:
Hey, Currently I'm writing a VB.NET/1.1 app and I need to mask the input for the password field. Does anyone know how to do this in VB? I've seen a C# example, but wouldn't know how to convert...
6
by: tony | last post by:
Hello! When you have windows forms you have the same possibility as when you have a Console application to use Console.Writeln to write whatever on the screen. Now to my question: Is it...
1
by: John Wright | last post by:
I am running a console application that connects to an Access database (8 million rows) and converts it to a text file and then cleans and compacts the database. When it runs I get the following...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.