Problem with console window

O

objectref

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
 
O

objectref

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
);
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
O

objectref

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 said:
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]
- (e-mail address removed)

objectref said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

objectref said:
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 said:
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]
- (e-mail address removed)

objectref said:
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
 
O

objectref

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
 
O

objectref

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 said:
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]
- (e-mail address removed)

objectref said:
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 said:
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]
- (e-mail address removed)

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
 
N

Nicholas Paldino [.NET/C# MVP]

Can you post your code?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

objectref said:
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 said:
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]
- (e-mail address removed)

objectref said:
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


in message 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]
- (e-mail address removed)

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
 
O

objectref

Nicholas Paldino said:
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();

}

}

}
 
T

Tom Porterfield

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.
 
O

objectref

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
 
O

objectref

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
);
}
}
}
 

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