By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Problem with console window

P: n/a
Hi to all,

i have a window app and i want to display some info in a console window.
I figured out (after a very long search...) how am i supposed to do it and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never displayed
there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref
Nov 16 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
i changed a bit the code to this:

AllocConsole();
IntPtr con = GetStdHandle(-11);
SetConsoleTitle("xexexe");
IntPtr intptr = new IntPtr(0);
uint dwNumberOfCharsWritten = 0;
string wbuffer = "anthonyb";
WriteConsole
(
con,
wbuffer,
(uint) wbuffer.Length,
ref dwNumberOfCharsWritten,
intptr // reserved
);
throw new Win32Exception(Marshal.GetLastWin32Error());

and now i can see what the error is: "The specified module could not be
found"
If it means the console window handle, i can see that the con value always
has same value...

Any help on this ???

thanks a lot for any help

p/invoke declarations:
---------------------
[DllImport("kernel32", SetLastError=true, ExactSpelling=true)]
static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool SetConsoleTitle(string lpConsoleTitle);
[DllImport("Kernel32.DLL", EntryPoint="WriteConsoleW",
CallingConvention=CallingConvention.StdCall)]
public static extern bool WriteConsole
(
IntPtr hConsoleOutput, // handle to screen buffer
string lpBuffer, // write buffer
uint nNumberOfCharsToWrite, // number of characters to write
ref uint lpNumberOfCharsWritten, // number of characters written
IntPtr lpReserved // reserved
);
Nov 16 '05 #2

P: n/a
objectref,

Once you make the call to AllocConsole, you should be able to just call
the static methods on the Console class in the System namespace, and write
what you want. There is no need to go through the P/Invoke layer to write
to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi to all,

i have a window app and i want to display some info in a console window.
I figured out (after a very long search...) how am i supposed to do it
and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never displayed
there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref

Nov 16 '05 #3

P: n/a
Hi Nicholaw and thanks for your help.
Are you sure that is working this way ?
I mean, this is a window app that i open a console window and all the search
i did in the last 2 hours, gave to me sort-of the code that i posted,
with p/invoke and WriteConsole stuff...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Og**************@TK2MSFTNGP11.phx.gbl...
objectref,

Once you make the call to AllocConsole, you should be able to just call
the static methods on the Console class in the System namespace, and write
what you want. There is no need to go through the P/Invoke layer to write
to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi to all,

i have a window app and i want to display some info in a console window.
I figured out (after a very long search...) how am i supposed to do it
and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never displayed
there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref


Nov 16 '05 #4

P: n/a
objectref,

I did it in about two minutes. I just created a console with
AllocConsole (called through the P/Invoke layer), and then I just called the
methods on the Console class, and it worked.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
Hi Nicholaw and thanks for your help.
Are you sure that is working this way ?
I mean, this is a window app that i open a console window and all the
search
i did in the last 2 hours, gave to me sort-of the code that i posted,
with p/invoke and WriteConsole stuff...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Og**************@TK2MSFTNGP11.phx.gbl...
objectref,

Once you make the call to AllocConsole, you should be able to just
call the static methods on the Console class in the System namespace, and
write what you want. There is no need to go through the P/Invoke layer
to write to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi to all,

i have a window app and i want to display some info in a console window.
I figured out (after a very long search...) how am i supposed to do it
and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never
displayed there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref



Nov 16 '05 #5

P: n/a
I pressed the "Send" button in a hurry...

Obviously you meant something like this:

AllocConsole();
Console.Write("abcdefg");
so i tried that but still nothing is displayed in the conolse window...
objectref


Once you make the call to AllocConsole, you should be able to just call
the static methods on the Console class in the System namespace, and write
what you want. There is no need to go through the P/Invoke layer to write
to the console.

Hope this helps.

Nov 16 '05 #6

P: n/a
Strange...i have it in front of me and it does not work...
You mean simply call AllocConsole() and then do
a Console.WriteLine("something") ??

Still, the text is not displayed in the console window...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O4**************@TK2MSFTNGP15.phx.gbl...
objectref,

I did it in about two minutes. I just created a console with
AllocConsole (called through the P/Invoke layer), and then I just called
the methods on the Console class, and it worked.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
Hi Nicholaw and thanks for your help.
Are you sure that is working this way ?
I mean, this is a window app that i open a console window and all the
search
i did in the last 2 hours, gave to me sort-of the code that i posted,
with p/invoke and WriteConsole stuff...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Og**************@TK2MSFTNGP11.phx.gbl...
objectref,

Once you make the call to AllocConsole, you should be able to just
call the static methods on the Console class in the System namespace,
and write what you want. There is no need to go through the P/Invoke
layer to write to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi to all,

i have a window app and i want to display some info in a console
window.
I figured out (after a very long search...) how am i supposed to do it
and
i try using the following code.

The problem is that ok is set to true, con is set to a number (all fine
until here)
the console window appears but the string "anthonyb" is never
displayed there...

Can anyone help me and point out to me what am i doing wrong ??
bool ok = AllocConsole();
IntPtr con = GetStdHandle(-11);
uint written;
WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
Thanks a lot for any help,

objectref



Nov 16 '05 #7

P: n/a
Can you post your code?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:Oy**************@TK2MSFTNGP11.phx.gbl...
Strange...i have it in front of me and it does not work...
You mean simply call AllocConsole() and then do
a Console.WriteLine("something") ??

Still, the text is not displayed in the console window...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:O4**************@TK2MSFTNGP15.phx.gbl...
objectref,

I did it in about two minutes. I just created a console with
AllocConsole (called through the P/Invoke layer), and then I just called
the methods on the Console class, and it worked.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:uW**************@TK2MSFTNGP10.phx.gbl...
Hi Nicholaw and thanks for your help.
Are you sure that is working this way ?
I mean, this is a window app that i open a console window and all the
search
i did in the last 2 hours, gave to me sort-of the code that i posted,
with p/invoke and WriteConsole stuff...

objectref
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:Og**************@TK2MSFTNGP11.phx.gbl...
objectref,

Once you make the call to AllocConsole, you should be able to just
call the static methods on the Console class in the System namespace,
and write what you want. There is no need to go through the P/Invoke
layer to write to the console.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"objectref" <ob*******@mediatrel.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> Hi to all,
>
> i have a window app and i want to display some info in a console
> window.
> I figured out (after a very long search...) how am i supposed to do
> it and
> i try using the following code.
>
> The problem is that ok is set to true, con is set to a number (all
> fine until here)
> the console window appears but the string "anthonyb" is never
> displayed there...
>
> Can anyone help me and point out to me what am i doing wrong ??
>
>
> bool ok = AllocConsole();
> IntPtr con = GetStdHandle(-11);
> uint written;
> WriteConsole(con, "anthonyb", 8, out written, IntPtr.Zero);
>
>
> Thanks a lot for any help,
>
> objectref
>



Nov 16 '05 #8

P: n/a

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eU**************@tk2msftngp13.phx.gbl...
Can you post your code?


At first, thanks a lot for your effor to help me...

here it comes: (and it gives me an exception of type "The specified module
could not be found"

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

using System.IO;

namespace WindowsApplication12

{

public class Form1 : System.Windows.Forms.Form

{

[DllImport("kernel32", SetLastError=true, ExactSpelling=true)]

static extern bool AllocConsole();

[DllImport("kernel32.dll")]

static extern IntPtr GetStdHandle(int nStdHandle);

[DllImport("kernel32.dll")]

static extern bool SetConsoleTitle(string lpConsoleTitle);

[DllImport("Kernel32.DLL", EntryPoint="WriteConsoleW",
CallingConvention=CallingConvention.StdCall)]

public static extern bool WriteConsole

(

IntPtr hConsoleOutput, // handle to screen buffer

string lpBuffer, // write buffer

uint nNumberOfCharsToWrite, // number of characters to write

ref uint lpNumberOfCharsWritten, // number of characters written

IntPtr lpReserved // reserved

);

private const int STD_OUTPUT_HANDLE = -11;

private System.Windows.Forms.Button button1;

private System.ComponentModel.Container components = null;

public Form1()

{

InitializeComponent();

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(64, 64);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 266);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e) {

bool ok = AllocConsole();

IntPtr ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTitle("xexexe");

IntPtr intptr = new IntPtr(0);

uint dwNumberOfCharsWritten = 0;

string wbuffer = "anthonyb";

WriteConsole

(

ConsoleHandle, // handle to screen buffer

wbuffer, // write buffer

(uint) wbuffer.Length, // number of characters to write

ref dwNumberOfCharsWritten, // number of characters written

intptr // reserved

);

throw new Win32Exception();

}

}

}


Nov 16 '05 #9

P: n/a
On Thu, 16 Dec 2004 16:46:52 +0200, objectref wrote:
Hi Nicholaw and thanks for your help.
Are you sure that is working this way ?
I mean, this is a window app that i open a console window and all the search
i did in the last 2 hours, gave to me sort-of the code that i posted,
with p/invoke and WriteConsole stuff...


Here is what I did that works:

[System.Runtime.InteropServices.DllImport("kernel32 ", SetLastError=true,
ExactSpelling=true)]
static extern bool AllocConsole();

[System.Runtime.InteropServices.DllImport("kernel32 .dll")]
static extern IntPtr GetStdHandle(int nStdHandle);

private void button1_Click(object sender, System.EventArgs e)
{
bool rc = AllocConsole();
IntPtr handle = GetStdHandle(-11);
if (rc)
{
Console.WriteLine(textBox1.Text);
}
else
{
MessageBox.Show("FAILED");
}
}

Keep in mind that when run in the VS.NET debugger the output will go to the
debugger console. But when run outside of VS.NET debugger the contents of
textBox1 are being written to the console created by AllocConsole.
--
Tom Porterfield
Nov 16 '05 #10

P: n/a
Hhhmmm....i see your point and you are right,
when running from inside VS it does not send anything to the console window.

But, i finally figured out a solution that works both inside and outside VS,
using CreateFile. It is usefull for debugging to see what is going on at
the console window you just created. When you're done, you switch
to the solution you provided.

I will post it tomorrow morning after i get back to work.

Thanks for your help!

objectref

Keep in mind that when run in the VS.NET debugger the output will go to
the
debugger console. But when run outside of VS.NET debugger the contents of
textBox1 are being written to the console created by AllocConsole.
--
Tom Porterfield

Nov 16 '05 #11

P: n/a
Well,

the code is like this and working both from inside the vs.net and outside.
The only remaining problem is that if you close the Console Window,
then the whole app is closed...
here it is:
---------------------------
using System;
using System.Runtime.InteropServices;
namespace runtime
{
public class ConsoleWindow
{
[DllImport("kernel32", SetLastError=true, ExactSpelling=true)]
static extern bool AllocConsole();
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr CreateFile(
string filename,
uint fileaccess,
uint fileshare,
IntPtr securityattributes,
int creationdisposition,
int flags, IntPtr template);
[DllImport("kernel32.dll")]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool SetConsoleTitle(string lpConsoleTitle);
[DllImport("kernel32.dll")]
static extern bool WriteConsole(
IntPtr hConsoleOutput,
string lpBuffer,
uint nNumberOfCharsToWrite,
out uint lpNumberOfCharsWritten,
IntPtr lpReserved);
[DllImport("kernel32.dll")]
static extern bool SetStdHandle(uint nStdHandle, IntPtr hHandle);
[DllImport("kernel32.dll")]
static extern bool SetConsoleOutputCP(uint wCodePageID);
private const int STD_OUTPUT_HANDLE = -11;
//private IntPtr conOut;
public ConsoleWindow()
{
bool ok = AllocConsole();
//conOut = CreateFile( "CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0,
IntPtr.Zero);
}
public void WriteToWindow(string text) {
//bool ok = AllocConsole();
IntPtr conOut = CreateFile( "CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0,
IntPtr.Zero);
uint dwNumberOfCharsWritten = 0;
WriteConsole
(
conOut, // handle to screen buffer
text, // write buffer
(uint) text.Length, // number of characters to write
out dwNumberOfCharsWritten, // number of characters written
IntPtr.Zero // reserved
);
}
}
}
Nov 16 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.