473,785 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2792
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.WriteLi ne() 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******** ********@TK2MSF TNGP05.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******** ********@TK2MSF TNGP05.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.ed u.for.addresswr ote in message
news:O$******** ******@TK2MSFTN GP03.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()
{
InitializeCompo nent();
}
public void InitializeCompo nent()
{
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(lb l);
ResumeLayout(fa lse);
}
}
public class MyConsole : IDisposable
{
private const uint STD_INPUT_HANDL E = 0xfffffff6;
private const uint STD_OUTPUT_HAND LE = 0xfffffff5;
private const uint STD_ERROR_HANDL E = 0xfffffff4;
[DllImport("kern el32.dll")]
public static extern bool AllocConsole();
[DllImport("kern el32.dll")]
public static extern bool FreeConsole();
[DllImport("kern el32.dll")]
public static extern int GetStdHandle(ui nt nStdHandle);
[DllImport("kern el32.dll")]
public static extern bool WriteConsole(in t hConsoleOutput,
string lpBuffer,
int nNumberOfCharsT oWrite,
ref int
lpNumberOfChars Written,
int lpReserved);
[DllImport("kern el32.dll")]
public static extern bool ReadConsole(int hConsoleInput,
StringBuilder lpBuffer,
int nNumberOfCharsT oRead,
ref int lpNumberOfChars Read,
int lpReserved);
private int stdin;
private int stdout;
public MyConsole()
{
AllocConsole();
stdin = GetStdHandle(ST D_INPUT_HANDLE) ;
stdout = GetStdHandle(ST D_OUTPUT_HANDLE );
}
public void WriteLine(strin g s)
{
int len = 0;
WriteConsole(st dout, s + "\r\n", s.Length + 2, ref len, 0);
}
public string ReadLine()
{
int len = 0;
StringBuilder sb = new StringBuilder() ;
ReadConsole(std in, 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.WriteLi ne("This is a console app");
}
}
}
}
}
Aug 7 '07 #6
"Arne Vajhøj" <ar**@vajhoej.d kwrote in message
news:46******** *************** @news.sunsite.d k...
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.d kwrote in message
news:46******** *************** @news.sunsite.d k...
>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.d kwrote in message
news:46******** *************** @news.sunsite.d k...
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

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

Similar topics

19
105841
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: System.out.print((char)27 + "[2J");
1
5387
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 application (VB.NET) will start a process, redirect its stdout and capture that process' output, displaying it in a window. I've written a component for this, and a test application for the component. It allows me to specify a command to execute,...
7
6359
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 problem double percent_00_01 = 1D / 10000D double percent_00_10 = 1D / 1000D double percent_01_00 = 1D / 100D double percent_10_00 = 10D / 100D
5
11258
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 to avoid the overhead of writing to the console if it is not visible, eg I am running inside a WinForm application. thanks
5
6344
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 complete project to show what happens. If anybody has an idea of how to fix this, please let me know. Yes I am aware that this is all in .Net 2. Thanks in advance.
17
4235
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 console using System.Console.WriteLine if thge project has its "Output Type" to "Windows Application". However I can write to stdio if i set output type to "Console Application". When I do this I unfortunately get a "console box" as well
5
11515
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 I may be doing wrong, thanks
3
11374
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 it myself. Here's the URL: http://www.codeproject.com/dotnet/ConsolePasswordInput.asp
6
5714
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 possible to use Console.Writeln when you have a Webservice. I don't think it's possible but just to be sure I ask you?
1
2044
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 error: The CLR has been unable to transition from COM context 0x1a2008 to COM context 0x1a2178 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10152
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.