ReadConsoleOutput - ReadConsoleOutputCharacter APIs

  • Thread starter Thread starter Stefano Camaiani
  • Start date Start date
S

Stefano Camaiani

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

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
 
Hi Tom,

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

Let's hope Stefano pops back in!

Regards,
Fergus
 
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
 
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! ;)
 
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
 
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
 
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
 
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
 
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


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
 
Back
Top