473,320 Members | 1,879 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,320 software developers and data experts.

ReadConsoleOutput - ReadConsoleOutputCharacter APIs

Hello,
As i think, ther is no way to use ReadConsoleOutput and
ReadConsoleOutPutCharacter with VB.Net or C#.

Someone have some code for me? Please....

Stefano
Nov 20 '05 #1
12 6754
Ok, after about a month i surrender....
But i have created a working VC++ 7.0 console wich do this work for me...
VC++ is more difficult, but really more powerful.. ;)
Thanks to all..
Stefano

"Stefano Camaiani" <st*****@subsonic.it> ha scritto nel messaggio
news:ek**************@TK2MSFTNGP09.phx.gbl...
Hello,
As i think, ther is no way to use ReadConsoleOutput and
ReadConsoleOutPutCharacter with VB.Net or C#.

Someone have some code for me? Please....

Stefano

Nov 20 '05 #2
Hi Stefano,

VC++ does have more oomph for system stuff. I did look on Google but there
is very little information about ReadConsoleOutput, certainly nothing in
classic VB, let alone VB.NET.

|| Ok, after about a month i surrender....
Sorry we couldn't help.

Best Regards,
Fergus

Nov 20 '05 #3
In article <#u**************@TK2MSFTNGP10.phx.gbl>, Stefano Camaiani wrote:
Ok, after about a month i surrender....
But i have created a working VC++ 7.0 console wich do this work for me...
VC++ is more difficult, but really more powerful.. ;)
Thanks to all..
Stefano

"Stefano Camaiani" <st*****@subsonic.it> ha scritto nel messaggio
news:ek**************@TK2MSFTNGP09.phx.gbl...
Hello,
As i think, ther is no way to use ReadConsoleOutput and
ReadConsoleOutPutCharacter with VB.Net or C#.

Someone have some code for me? Please....

Stefano



Option Explicit On
Option Strict On

Imports System.Runtime.InteropServices

Public Module ModMain

Private Enum STD_HANDLES
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
END ENUM

<StructLayout(LayoutKind.Explicit, CharSet:=CharSet.Auto)> _
Private Structure CHAR_TYPE
<FieldOffset(0)> _
Public UnicodeChar As Short

<FieldOffset(0)> _
Public AsciiChar As Byte
End Structure

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure CHAR_INFO
Public [Char] As CHAR_TYPE
Public Attributes As Short
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure COORD
Public X As Short
Public Y As Short

Public Sub New(ByVal X As Short, ByVal Y As Short)
Me.X = X
Me.Y = Y
End Sub
End Structure

<StructLayout(LayoutKind.Sequential)> _
Private Structure SMALL_RECT
Public Left As Short
Public Top As Short
Public Right As Short
Public Bottom As Short

Public Sub New(ByVal Left As Short, ByVal Top As Short, ByVal
Right As Short, ByVal Bottom As Short)
Me.Left = Left
Me.Top = Top
Me.Right = Right
Me.Bottom = Bottom
End Sub
End Structure

Private Declare Function GetStdHandle Lib "kernel32" ( _
ByVal nStdHandle As STD_HANDLES) As IntPtr

Private Declare Auto Function ReadConsoleOutput Lib "kernel32" ( _
ByVal hConsoleOutput As IntPtr, _
ByRef lpBuffer As CHAR_INFO, _
ByVal dwBufferSize As COORD, _
ByVal dwBufferCoord As COORD, _
ByRef lpReadRegion As SMALL_RECT) As Boolean

Public Sub Main()
Console.WriteLine("*-*-*-*")
Console.WriteLine("*-*-*-*")
Console.WriteLine("*-*-*-*")
Console.WriteLine("*-*-*-*")

Dim hConsoleOutput As IntPtr =
GetStdHandle(STD_HANDLES.STD_OUTPUT_HANDLE)
Dim Buffer(27) As CHAR_INFO
Dim BufferSize As New COORD(7, 4)
Dim BufferCoord As New COORD(0, 0)
Dim ReadRegion As New SMALL_RECT(0, 0, 6, 3)

Console.WriteLine(ReadConsoleOutput(hConsoleOutput , Buffer(0),
BufferSize, BufferCoord, ReadRegion))

For i As Integer = 0 To 27
Console.WriteLine(Chr(Buffer(i).Char.UnicodeChar))
Next
End Sub
End Module
HTH,
Tom Shelton
Nov 20 '05 #4
Hi Tom,

Ha, ha, you Hero!! ;-))

Let's hope Stefano pops back in!

Regards,
Fergus
Nov 20 '05 #5
In article <u7*************@TK2MSFTNGP10.phx.gbl>, Fergus Cooney wrote:
Hi Tom,

Ha, ha, you Hero!! ;-))

I don't like hearing something can't be done is all :)
Let's hope Stefano pops back in!


Yeah, let's hope...

Tom Shelton
Nov 20 '05 #6
Thanks Fergus,
Is not a problem, but is really strange, not?
Yes, i'm able to use ReadConsoleOutput and ReadConsoleOutputCharacter
with Vc++ and Vb6.0.
This should be a limitation of the .Net technology, but we are
goodlucked because this is a little limitation...

Regards,
Stefano from Italy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #7


Ehi THANKS!!!!!!
If this will work then you are really a HERO!!!

I will try it soon as possible...
But i'm not happy, because when i thinked of Vb.Net and C# was not able
to do it, then i started to learn C++.....and this was the only way to
force me to learn c++... ;)

Then i must say: Visual Basic.net and C# are very awesomes....

A little questio for the forum:

Like Vb 60 and Vb.net, also c# is not able to use libraries without
declaring first....
Then at this time C# has the same power of Vb.Net or a little more?

Sorry for my bad english, i'm Italian....

Regards and thanks to all here! ;)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #8
Sorry, but i have a new little questions... :)

If this code will work then i will be very happy, but i should be very
much more happy if i will be able to use this ReadConsoleOutput function
within my GUI application....(Then without build a stand alone console)

Per example: if i start a new process from my GUI application to run a
command line tool then i get a working console, but how to get his
STD_OUTPUT_HANDLE???

I don't think of myProcess.StandardOutput should work....

A new question for my heroes! ;)
Thanks,
Regards,
Best Whishes,
Stefano
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #9
Hi Stefano,

|| Ehi THANKS!!!!!!

:-)) It's nice to see you happy.

|| If this will work then you are really a HERO!!!

Although you're replying to my branch of the thread, I'm assuming that you mean Tom here. So I'll take the liberty of rephrasing your statement.


If this will work then Tom - you are really a HERO!!!



;-)

===============================
|| Like Vb 60 and Vb.net, also c# is not able to use libraries
|| without declaring first....
|| Then at this time C# has the same power of Vb.Net or a little more?

C# has some syntactic powers that VB hasn't - some ideas are therefore easier or cleaner to implement, but then VB has some that C# doesn't have.

As to actual power - for the most part it is the .NET Framework which provides this and so
there is very little difference between C# and VB (though I still think that C++ has an edge). The ideal of the design is such that any language added to the .NET family has near enough equal access to all that's available. It's then more a choice of which language do you prefer to do a task, rather than which one can do it.

Lol, Sorry for my non-existant Italian. I'm British!! ;-)

Regards,
Fergus
Nov 20 '05 #10
In article <O9**************@TK2MSFTNGP09.phx.gbl>, Stefano Camaiani wrote:
Sorry, but i have a new little questions... :)

If this code will work then i will be very happy, but i should be very
much more happy if i will be able to use this ReadConsoleOutput function
within my GUI application....(Then without build a stand alone console)

Per example: if i start a new process from my GUI application to run a
command line tool then i get a working console, but how to get his
STD_OUTPUT_HANDLE???


You can use System.Diagnostics.Process tot start the application, and
then read from the StandardOutput property to grab the output...

Dim p As New Process
p.StartInfo.File = "my.exe"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()

......
strBuffer = p.StandardOutput.ReadLine()
.....

There are more complete examples in the documentation - just look up the
StandardOutput property of the System.Diagnostics.Process class.

Tom Shelton
Nov 20 '05 #11
Well, really thanks Tom, your code work 100% !!!

I tried to Get STD_OUTPUT_HANDLE from my GUI application, but with no
success.
This is what i done:

-I tried to create a new thread and launch a process from there, but the
Handle returned is ever the one of my gui application.

-Then i triend to create a new thread and launch a Shell from there, but
the Handle returned is ever the one of my gui application.

In Vb 6.0 is possible to create a Console and get his Handle by calling
the API AllocConsole, this is a little part of code:

Api Declaration:
Private Declare Function AllocConsole Lib "kernel32" () As Long

And the code o my button1:
AllocConsole
Shell "cmd /k dir c:\"
StdOut = GetStdHandle(STD_OUTPUT_HANDLE)

The follow of the code is similar the one you gived me...

I hope this should help.....

And i hope of you should help me ;)

Regards.
Stefano
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #12
In article <Oa**************@TK2MSFTNGP12.phx.gbl>, Stefano Camaiani wrote:
Well, really thanks Tom, your code work 100% !!!

I tried to Get STD_OUTPUT_HANDLE from my GUI application, but with no
success.
This is what i done:

-I tried to create a new thread and launch a process from there, but the
Handle returned is ever the one of my gui application.

-Then i triend to create a new thread and launch a Shell from there, but
the Handle returned is ever the one of my gui application.

In Vb 6.0 is possible to create a Console and get his Handle by calling
the API AllocConsole, this is a little part of code:

Api Declaration:
Private Declare Function AllocConsole Lib "kernel32" () As Long

And the code o my button1:
AllocConsole
Shell "cmd /k dir c:\"
StdOut = GetStdHandle(STD_OUTPUT_HANDLE)

The follow of the code is similar the one you gived me...

I hope this should help.....

And i hope of you should help me ;)

Regards.
Stefano
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Is this what your after:

Option Explicit On
Option Strict On

Imports System.Diagnostics.Process

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Anchor =
CType((System.Windows.Forms.AnchorStyles.Bottom Or
System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.Button1.Location = New System.Drawing.Point(284, 260)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'ListBox1
'
Me.ListBox1.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right),
System.Windows.Forms.AnchorStyles)
Me.ListBox1.IntegralHeight = False
Me.ListBox1.Location = New System.Drawing.Point(0, 0)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(364, 256)
Me.ListBox1.TabIndex = 1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(360, 285)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim p As New Process

ListBox1.Items.Clear()

p.StartInfo.FileName = "cmd"
p.StartInfo.Arguments = "/k dir c:\"
p.StartInfo.CreateNoWindow = True
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()

Dim output As String = p.StandardOutput.ReadLine()
Do Until output Is Nothing
ListBox1.Items.Add(output)
output = p.StandardOutput.ReadLine()
Loop
End Sub
End Class

HTH,
Tom Shelton
Nov 20 '05 #13

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

Similar topics

2
by: Tanuki | last post by:
Hi All: I am doing some development in VC++ using some llibraries downloaded from the web. The library comes in 3 files <mylib.dll>, <mylib.lib>, <mylib.exp> and I also have <mylib.h>. As...
0
by: Eric A. Johnson | last post by:
Hello All, I'm having a problem with the below code. I am trying to copy an area of screen space into an X by Y array of type CHAR_INFO before I overwrite it with my "window". If I comment out...
1
by: cindy liu | last post by:
Hi, I'd like to create xml string based on my xsd file. Is there any .Net APIs to do this? Like in BizTalk 2004, you can generate xml instance based on xsd file. Thanks in advance!!! Cindy
3
by: Raquel | last post by:
A very basic question to all the esteemed listers. I am going through UDB manual. At the end of each chapter, "DB2 APIs" are listed. For example a multi-page description of the DB2 API "db2Restore"...
2
by: Maverick | last post by:
I'm implementing the functions of WriteConsoleOutput and ReadConsleOutput output. The "WriteConsoleOutput" can be work properly ....but the output of "ReadConsoleOutput" is wrong anyway .... can...
3
by: TC | last post by:
Hey Folks, I am using the following 4 Win32 APIs with a C# AddIn: FindWindow SetWindowLong SetForegroundWindow EnableWindow Within the AddIn, there are some winforms. I use these APIs to...
2
by: Doug Perkes | last post by:
There was a post a couple of months ago entitled "ReadConsoleOutput - ReadConsoleOutputCharacter APIs". I have some questions in relation to that post. I have an existing console application...
1
by: nagrik | last post by:
Hello Group, I am writing a http client and reading a web page from the server. The page can be compressed in any of the format namely; gzip or deflate or compress. My client reads the page...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.