ReadConsoleOutputCharacer API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefano Camaiani

    ReadConsoleOutputCharacer API

    Hello, it's strange but in the web there are not (or i found not)
    explanation on howto use ReadConsoleOutp utCharacter from C# or Vb.Net....
    Very much articles, but not one word of this particular API and also
    ReadConsoleOutp ut too....

    May be of this API is disabled on those new Languages?

    Regards.
    Stefano.


  • Jon Skeet [C# MVP]

    #2
    Re: ReadConsoleOutp utCharacer API

    Stefano Camaiani <stefano@subson ic.it> wrote:[color=blue]
    > Hello, it's strange but in the web there are not (or i found not)
    > explanation on howto use ReadConsoleOutp utCharacter from C# or Vb.Net....
    > Very much articles, but not one word of this particular API and also
    > ReadConsoleOutp ut too....
    >
    > May be of this API is disabled on those new Languages?[/color]

    It's a Win32 API, not part of the .NET framework. It's not that it's so
    much "disabled" - it's just not part of the framework. You could use
    P/Invoke, of course, but I think there are other .NET console APIs out
    there which may well be easier to use.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Stefano Camaiani

      #3
      Re: ReadConsoleOutp utCharacer API

      Thanks Jon, i will reply to you here too...
      I understand you, but why all other API related are supported then?
      First: Sorry for my bad english, i'm Italian...
      Here some APIs Console related wich i'm able tu use with Vb.Net and C#:
      SetConsoleTextA ttribute

      GetStdHandle

      SetConsoleCurso rInfo

      GetConsoleMode

      SetConsoleMode

      SetConsoleTitle A

      GetConsoleTitle A

      GetConsoleScree nBufferInfo

      SetConsoleCurso rPosition

      SetConsoleTextA ttribute

      FillConsoleOutp utCharacter

      FillConsoleOutp utAttribute

      SetConsoleCurso rPosition

      SetConsoleCurso rInfo

      ReadConsole

      Then why if those APIs are not part of framework they works well?

      Regards.

      Stefano...


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> ha scritto nel messaggio
      news:MPG.19ebac ccda6340a89897f 6@msnews.micros oft.com...[color=blue]
      > Stefano Camaiani <stefano@subson ic.it> wrote:[color=green]
      > > Hello, it's strange but in the web there are not (or i found not)
      > > explanation on howto use ReadConsoleOutp utCharacter from C# or[/color][/color]
      Vb.Net....[color=blue][color=green]
      > > Very much articles, but not one word of this particular API and also
      > > ReadConsoleOutp ut too....
      > >
      > > May be of this API is disabled on those new Languages?[/color]
      >
      > It's a Win32 API, not part of the .NET framework. It's not that it's so
      > much "disabled" - it's just not part of the framework. You could use
      > P/Invoke, of course, but I think there are other .NET console APIs out
      > there which may well be easier to use.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: ReadConsoleOutp utCharacer API

        Stefano Camaiani <stefano@subson ic.it> wrote:[color=blue]
        > Thanks Jon, i will reply to you here too...
        > I understand you, but why all other API related are supported then?[/color]

        Um, they're not, as far as I can see...
        [color=blue]
        > First: Sorry for my bad english, i'm Italian...
        > Here some APIs Console related wich i'm able tu use with Vb.Net and C#:
        > SetConsoleTextA ttribute[/color]

        Which class is that in? It doesn't appear in the MSDN index when I have
        the Visual C# filter on.

        <snip>
        [color=blue]
        > Then why if those APIs are not part of framework they works well?[/color]

        Could you give a small sample program that shows them being used?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Stefano Camaiani

          #5
          Re: ReadConsoleOutp utCharacer API

          Well, then i'm really unhappy of Microsoft has NOT released a Visual
          Basic version _NOT_ Framework... :(

          I love Visual Basic, but i really need this API to continue my Job.....
          Why? Because the standard Console functions of Visual Basic.Net and c#
          are not able to handle my problem...

          Then i will need to continue to use VB6 forever?

          Please, if you know some other useful APIs to get Console Output Buffer,
          before a shelled application end, and before some data buffer has full
          filled the StandardOutput PIPE buffer, then please let me know.....


          Regards.

          Stefano.


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

          Comment

          • Michael A. Covington

            #6
            Re: ReadConsoleOutp utCharacer API


            "Stefano Camaiani" <stefano@subson ic.it> wrote in message
            news:OnuvMlEjDH A.556@TK2MSFTNG P11.phx.gbl...[color=blue]
            > Well, then i'm really unhappy of Microsoft has NOT released a Visual
            > Basic version _NOT_ Framework... :(
            >
            > I love Visual Basic, but i really need this API to continue my Job.....
            > Why? Because the standard Console functions of Visual Basic.Net and c#
            > are not able to handle my problem...
            >
            > Then i will need to continue to use VB6 forever?[/color]

            No; you can call Win32 routines such as this one by using PInvoke.


            Comment

            • Stefano Camaiani

              #7
              Re: ReadConsoleOutp utCharacer API

              Well, thanks again, but while the use of PInvoke should be really easy for
              you, but i'm not able to create the code alone and a one i already created
              does not work, then i suppose of those APIs are not supported by the new
              programming languages...:(
              There are some useful codes for Vb.Net and for C# about console:
              Vb.Net = http://www.allapi.net/classlib/class.php?id=4
              C# =



              "Michael A. Covington" <look@www.covin gtoninnovations .com.for.addres s> ha
              scritto nel messaggio news:eqcYrJHjDH A.3212@tk2msftn gp13.phx.gbl...[color=blue]
              >
              > "Stefano Camaiani" <stefano@subson ic.it> wrote in message
              > news:OnuvMlEjDH A.556@TK2MSFTNG P11.phx.gbl...[color=green]
              > > Well, then i'm really unhappy of Microsoft has NOT released a Visual
              > > Basic version _NOT_ Framework... :(
              > >
              > > I love Visual Basic, but i really need this API to continue my Job.....
              > > Why? Because the standard Console functions of Visual Basic.Net and c#
              > > are not able to handle my problem...
              > >
              > > Then i will need to continue to use VB6 forever?[/color]
              >
              > No; you can call Win32 routines such as this one by using PInvoke.
              >
              >[/color]


              Comment

              • Mattias Sjögren

                #8
                Re: ReadConsoleOutp utCharacer API

                Stefano,
                [color=blue]
                >and a one i already created does not work[/color]

                Why don't you post your code here then, so we can help you find the
                problem?



                Mattias

                --
                Mattias Sjögren [MVP] mattias @ mvps.org

                Please reply only to the newsgroup.

                Comment

                • Stefano Camaiani

                  #9
                  Re: ReadConsoleOutp utCharacer API


                  Well, first: Thanks... Second: Sorry for my bad english...

                  Ok, i worked on 2 existing class i found in the Web.
                  Vb.Net = http://www.allapi.net/classlib/class.php?id=4
                  C# =

                  76d1f08-2d79-47bd-825b-0489938aae0f

                  I tried to implement ReadConsoleOutp utCharacter and also
                  ReadConsoleOutp ut on it without success....

                  This is my code for Vb.Net:
                  Api declarations:

                  <DllImport("KER NEL32.DLL",
                  EntryPoint:="Re adConsoleOutput CharacterA", CharSet:=CharSe t.Ansi,
                  SetLastError:=T rue, ExactSpelling:= True,
                  CallingConventi on:=CallingConv ention.StdCall) > _
                  Private Shared Function ReadConsoleOutp utCharacter(ByV al
                  hConsoleOutput As Integer, ByVal lpCharacter As String, ByVal nLenght As
                  Integer, ByVal dwCursorPositio n As COORD, ByVal lpNumberOfChars Read As
                  Integer) As Integer
                  End Function

                  <DllImport("KER NEL32.DLL", EntryPoint:="Re adConsoleOutput A",
                  CharSet:=CharSe t.Ansi, SetLastError:=F alse, ExactSpelling:= True,
                  CallingConventi on:=CallingConv ention.StdCall) > _
                  Private Shared Function ReadConsoleOutp ut(ByVal hConsoleOutput As
                  Integer, ByVal lpCharacter As CHAR_INFO, ByVal dwBufferSize As COORD,
                  ByVal dwBufferCoord As COORD, ByVal lpReadRegion As SMALL_RECT) As
                  Integer
                  End Function

                  and my code is:
                  Public Shared Property OutputS() As String
                  Get
                  Dim NumberOfCharRea d As Integer
                  Dim ConsoleInfo As CONSOLE_SCREEN_ BUFFER_INFO
                  Dim CaptureStart As Integer
                  Dim NrOfVisibleChar s As Integer
                  With ConsoleInfo.srW indow
                  NrOfVisibleChar s = (ConsoleAttribu tes.WindowHeigh t *
                  ConsoleAttribut es.WindowWidth) 'Calculate area to capture
                  End With
                  'MsgBox(NrOfVis ibleChars)
                  Dim crd As COORD
                  crd.x = 1
                  crd.y = 1
                  Dim sb3 As New String(" ", 256)
                  GetConsoleScree nBufferInfo(Get StdHandle(STD_O UTPUT_HANDLE),
                  MyScreenBufferI nfo)
                  CaptureStart = ConsoleInfo.srW indow.Left Or
                  (ConsoleInfo.sr Window.Top * &H10000)
                  'CaptureStart = MyScreenBufferI nfo.srWindow.To p * &H10000
                  'CaptureStart = 1048576
                  'CaptureStart = 1

                  MsgBox(MyScreen BufferInfo.wAtt ributes.ToStrin g)
                  ReadConsoleOutp utCharacter(Get StdHandle(STD_O UTPUT_HANDLE),
                  sb3, NrOfVisibleChar s, crd, NumberOfCharRea d)
                  'MsgBox(NumberO fCharRead)
                  Return sb3
                  End Get
                  Set(ByVal Value As String)
                  End Set
                  End Property

                  Public Shared Property GetConsoleOutpu tS() As String
                  Get
                  Dim CharacterInfo As CHAR_INFO
                  Dim crd1 As COORD
                  crd1.x = 10
                  crd1.y = 10
                  Dim crd2 As COORD
                  crd2.x = 20
                  crd2.y = 20
                  Dim Coordinates As SMALL_RECT
                  Coordinates.Top = 1
                  Coordinates.Bot tom = 10
                  Coordinates.Lef t = 1
                  Coordinates.Rig ht = 10
                  'MsgBox(NrOfVis ibleChars)
                  ReadConsoleOutp ut(GetStdHandle (STD_OUTPUT_HAN DLE),
                  CharacterInfo, crd2, crd1, Coordinates)
                  MsgBox(Len(Char acterInfo))
                  Return CharacterInfo.C har1.ToString
                  End Get
                  Set(ByVal Value As String)
                  End Set
                  End Property
                  *************** *************** *************** *************** ************
                  *************** *************** *************** *************** ************
                  *************** *************** ***
                  This is my Api Declaration on c#
                  [DllImport("kern el32.dll", EntryPoint="Rea dConsoleOutputC haracterA",
                  SetLastError=tr ue,
                  CharSet=CharSet .Auto, CallingConventi on=CallingConve ntion.StdCall)]
                  private static extern bool ReadConsoleOutp utCharacter(int
                  hConsoleInput,
                  StringBuilder buf, int nNumberOfCharsT oRead, COORD dwReadCoord, ref
                  int lpNumberOfChars Read);


                  and this is my code:
                  public static string ReadChar2()
                  {
                  // Temporarily disable character echo (ENABLE_ECHO_IN PUT) and line
                  input
                  // (ENABLE_LINE_IN PUT) during this operation
                  SetConsoleMode( hConsoleInput,
                  (int) (InputModeFlags .ENABLE_PROCESS ED_INPUT |
                  InputModeFlags. ENABLE_WINDOW_I NPUT |
                  InputModeFlags. ENABLE_MOUSE_IN PUT));

                  int lpNumberOfChars Read = 0;
                  //StringBuilder buf = new StringBuilder(1 );
                  StringBuilder buf = new StringBuilder(1 00);

                  COORD c = new COORD();
                  c.x = (short)1;
                  c.y = (short)1;
                  bool success = ReadConsoleOutp utCharacter(hCo nsoleInput, buf, 5, c,
                  ref lpNumberOfChars Read);

                  // Reenable character echo and line input
                  SetConsoleMode( hConsoleInput,
                  (int) (InputModeFlags .ENABLE_PROCESS ED_INPUT |
                  InputModeFlags. ENABLE_ECHO_INP UT |
                  InputModeFlags. ENABLE_LINE_INP UT |
                  InputModeFlags. ENABLE_WINDOW_I NPUT |
                  InputModeFlags. ENABLE_MOUSE_IN PUT));

                  if (success)
                  return buf.ToString();
                  else
                  throw new ApplicationExce ption("Attempt to call ReadConsole API
                  failed.");
                  }
                  '************** *************** *************** *************** ************
                  *************** *************** *************** *************** ************
                  *************** *************** ***

                  If you would like i have also the VB 6.0 code wich work great with only
                  few lines of code:


                  Dim ConsoleInfo As CONSOLE_SCREEN_ BUFFER_INFO
                  Dim ConsoleText As String
                  Dim NrOfVisibleChar s As Long
                  Dim NrOfCharsRead As Long
                  Dim CaptureStart As Long

                  ' Execute DOS command, the command will now use the previously
                  allocated console window

                  'Any shell here.....

                  StdOut = GetStdHandle(ST D_OUTPUT_HANDLE ) ' Get a handle to Std_out
                  'MsgBox (StdOut)
                  GetConsoleScree nBufferInfo StdOut, ConsoleInfo ' Get info about the
                  console window, such as width and height
                  With ConsoleInfo.srW indow
                  NrOfVisibleChar s = (.Bottom - .Top) * (.Right - .Left) ' Calculate
                  area to capture
                  End With

                  ConsoleText = String(NrOfVisi bleChars, vbNullChar) ' Reserve space for
                  captured text

                  ' This is a little trick that has to be done in order to be able to pass
                  the X and Y coordinates to ReadConsoleOutp utCharacter
                  ' They should really be passsed as a COORD structure, but VB won't allow
                  user defined types to be passed by value...
                  CaptureStart = ConsoleInfo.srW indow.Left Or (ConsoleInfo.sr Window.Top *
                  &H10000)
                  MsgBox CaptureStart
                  ReadConsoleOutp utCharacter StdOut, ConsoleText, NrOfVisibleChar s,
                  CaptureStart, NrOfCharsRead ' Get all visible text in the console
                  window

                  ConsoleText = Left(ConsoleTex t, NrOfCharsRead) ' Trim results

                  Text1.Text = ConsoleText

                  '************** *************** *************** *************** ************
                  *************** *************** *************** *************** ************
                  *************** *************** ***

                  Here is it.....
                  Why i need those APIs? Because i only must read an Output of a command
                  line tools....but i should not use the standard redirections because
                  they show me the output buffer only when the command line tool has
                  exited or if the PIPE buffer is full....too slow for me...i need to get
                  the output immediately...

                  I will very thanks you very very very much if you should give me a
                  way....
                  Note: Visual C++.net is able to use both ReadConsoleOutp ut and
                  ReadConsoleOutp utCharacter APIs.....(windo ws.h)...If you would like i
                  should post it too.....

                  Please don't lose too much time on searching for my problem. because
                  ithink of VB.Net and C# should not use those APIs.....





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

                  Comment

                  • Stefano Camaiani

                    #10
                    Re: ReadConsoleOutp utCharacer API

                    Well,
                    i surrender....
                    But i have created a Visual C++ 7.0 console wich do this work for me....
                    Thanks to all...



                    "Stefano Camaiani" <stefano@subson ic.it> ha scritto nel messaggio
                    news:emLHbMCjDH A.3340@tk2msftn gp13.phx.gbl...[color=blue]
                    > Hello, it's strange but in the web there are not (or i found not)
                    > explanation on howto use ReadConsoleOutp utCharacter from C# or Vb.Net....
                    > Very much articles, but not one word of this particular API and also
                    > ReadConsoleOutp ut too....
                    >
                    > May be of this API is disabled on those new Languages?
                    >
                    > Regards.
                    > Stefano.
                    >
                    >[/color]


                    Comment

                    • Mattias Sjögren

                      #11
                      Re: ReadConsoleOutp utCharacer API

                      Stefano,
                      [color=blue]
                      > <DllImport("KER NEL32.DLL",
                      >EntryPoint:="R eadConsoleOutpu tCharacterA", CharSet:=CharSe t.Ansi,
                      >SetLastError:= True, ExactSpelling:= True,
                      >CallingConvent ion:=CallingCon vention.StdCall )> _
                      > Private Shared Function ReadConsoleOutp utCharacter(ByV al
                      >hConsoleOutp ut As Integer, ByVal lpCharacter As String, ByVal nLenght As
                      >Integer, ByVal dwCursorPositio n As COORD, ByVal lpNumberOfChars Read As
                      >Integer) As Integer
                      > End Function[/color]

                      Should be

                      <DllImport("KER NEL32.DLL", CharSet:=CharSe t.Auto, SetLastError:=T rue)>
                      _
                      Private Shared Function ReadConsoleOutp utCharacter(ByV al
                      hConsoleOutput As IntPtr, ByVal lpCharacter As StringBuilder, ByVal
                      nLenght As Integer, ByVal dwCursorPositio n As COORD, ByRef
                      lpNumberOfChars Read As Integer) As Boolean
                      End Function

                      [color=blue]
                      > <DllImport("KER NEL32.DLL", EntryPoint:="Re adConsoleOutput A",
                      >CharSet:=CharS et.Ansi, SetLastError:=F alse, ExactSpelling:= True,
                      >CallingConvent ion:=CallingCon vention.StdCall )> _
                      > Private Shared Function ReadConsoleOutp ut(ByVal hConsoleOutput As
                      >Integer, ByVal lpCharacter As CHAR_INFO, ByVal dwBufferSize As COORD,
                      >ByVal dwBufferCoord As COORD, ByVal lpReadRegion As SMALL_RECT) As
                      >Integer
                      > End Function[/color]

                      <DllImport("KER NEL32.DLL", CharSet:=CharSe t.Auto)> _
                      Private Shared Function ReadConsoleOutp ut(ByVal hConsoleOutput As
                      IntPtr, <[In], Out> ByVal lpCharacter() As CHAR_INFO, ByVal
                      dwBufferSize As COORD, ByVal dwBufferCoord As COORD, ByRef
                      lpReadRegion As SMALL_RECT) As Boolean
                      End Function



                      Mattias

                      --
                      Mattias Sjögren [MVP] mattias @ mvps.org

                      Please reply only to the newsgroup.

                      Comment

                      Working...