473,411 Members | 2,009 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,411 software developers and data experts.

Problem with console window

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
11 4827
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
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
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
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
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tim Axtelle | last post by:
I am new to Python and am trying to create a standalone exe from a python script using py2exe 0.5.0 and Python 2.3 without success. I am able to generate the appropriate .exe file but it is not...
6
by: Shaun Fleming | last post by:
I've been trying to make this simple script compatible across various browsers. It works for IE 6.0 and NS 7 but doesnt work with Opera (I have version 7.11). This is what is supposed to happen:...
7
by: Merlin | last post by:
Hi there, I have a serious problem with opening a new window with the help of JavaScript. The problem only occures with Firefox. Once you click on the item which fires up the open function, the...
4
by: nick | last post by:
Hi all: In the winform i want to run commandline with return value(string), then display this string in winform textbox. anyone has idea about that? Thanks Nick
0
by: Rajiv Das | last post by:
I am trying to create a Dual mode tool (runs in both console and GUI mode) similar to ILDASM. This is my progra...
0
by: Mythran | last post by:
I can draw onto the console window where I want using a mixture of API calls and the System.Drawing namespace (.Net 1.1). I am trying to install hooks for the window to catch a resize or another...
10
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
3
by: TC | last post by:
I'm trying to debug a console application, but I can't see the console output. I've seen many references which say that console output is supposed to appear on the Output window when the...
5
by: =?Utf-8?B?SmFtZXMgV29uZw==?= | last post by:
Dear all, I'd like to know if there is any method to minimize command mode window when a console program is running. In my case, there are several console programs which run periodically in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.