GUI plus Console?

D

Davej

Can a GUI program also launch a console? That might be useful as an
output for debug messages. Thanks.
 
A

Arne Vajhøj

Can a GUI program also launch a console? That might be useful as an
output for debug messages.

An old code snippet:

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;
private const uint ATTACH_PARENT_PROCESS = 0xffffffff;
[DllImport("kernel32.dll")]
public static extern bool AttachConsole(uint dwProcessId);
[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()
{
/*
if(!AttachConsole(ATTACH_PARENT_PROCESS))
{
AllocConsole();
}
*/
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();
}
}


Arne
 
M

Marcel Müller

Can a GUI program also launch a console? That might be useful as an
output for debug messages. Thanks.

I did not test this with C#.NET, but writing debug output to stderr and
piping the output from the GUI application to a file should work in general.

Start app with logging:
myguiapp.exe 2>>myapplog.txt

Concurrently view the log e.g. with tail:
tail -f myapplog.txt


Marcel
 
R

Radu Cosoveanu

Can a GUI program also launch a console? That might be useful as an
output for debug messages. Thanks.

Hi,

You can try to log in Trace and from the configuration file to set the output into a log file.

Radu.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top