WriteConsoleOutput

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Doug Perkes

    WriteConsoleOutput

    Does anyone have an example of declaring and using the WriteConsoleOut put
    Win32 API function? I'm having a little bit of trouble understanding how to
    translate all the structures to VB.NET. Specifically, I am trying to send
    plain text, the enter key, the F3 key, arrow keys, etc to a console window I
    have attached to.

    Thanks,

    Doug


  • Mattias Sjögren

    #2
    Re: WriteConsoleOut put

    Doug,
    [color=blue]
    >Does anyone have an example of declaring and using the WriteConsoleOut put
    >Win32 API function?[/color]

    I don't have any example of how to use it, but I'd declare it like
    this

    Declare Auto Function WriteConsoleOut put Lib "kernel32.d ll" (ByVal
    hConsoleOutput As IntPtr, ByVal lpBuffer(,) As CHAR_INFO, ByVal
    dwBufferSize As COORD, ByVal dwBufferCoord As COORD, ByRef
    lpWriteRegion As SMALL_RECT) As Boolean



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Doug Perkes

      #3
      WriteConsoleInp ut [was WriteConsoleOut put]

      Sorry, I meant to type WriteConsoleInp ut. ;-(

      Could someone help me write the structs and declarations for
      WriteConsoleInp ut. I am very confused by the INPUT_RECORD struct and how to
      create it in VB?

      Thanks,

      Doug

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:OUOWss1uDH A.560@TK2MSFTNG P11.phx.gbl...[color=blue]
      > Doug,
      >[color=green]
      > >Does anyone have an example of declaring and using the WriteConsoleOut put
      > >Win32 API function?[/color]
      >
      > I don't have any example of how to use it, but I'd declare it like
      > this
      >
      > Declare Auto Function WriteConsoleOut put Lib "kernel32.d ll" (ByVal
      > hConsoleOutput As IntPtr, ByVal lpBuffer(,) As CHAR_INFO, ByVal
      > dwBufferSize As COORD, ByVal dwBufferCoord As COORD, ByRef
      > lpWriteRegion As SMALL_RECT) As Boolean
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      > Please reply only to the newsgroup.[/color]


      Comment

      • Tom Shelton

        #4
        Re: WriteConsoleInp ut [was WriteConsoleOut put]

        In article <#KgOWz1uDHA.21 80@TK2MSFTNGP09 .phx.gbl>, Doug Perkes wrote:[color=blue]
        > Sorry, I meant to type WriteConsoleInp ut. ;-(
        >
        > Could someone help me write the structs and declarations for
        > WriteConsoleInp ut. I am very confused by the INPUT_RECORD struct and how to
        > create it in VB?
        >
        > Thanks,
        >
        > Doug[/color]

        Probably the unions... I would do something like this...

        <StructLayout(L ayoutKind.Expli cit)> _
        StructLayout CharUnion
        <FieldOffset(0) > _
        Public UnicodeChar As Short
        <FieldOffset(0) > _
        Public AsciiChar As Byte
        End Structure


        <StructLayout(L ayoutKind.Seque ntial)> _
        Structure KEY_EVENT_RECOR D
        Public bKeyDown As Boolean
        Public wReapeatCount As Short
        Public wVirtualKeyCode As Short
        Public wVirtualScanCod e As Short
        Public uChar As CharUnion

        ' I'd probaby make a enum of the flags
        ' here, but an integer still works :)
        Public dwControlKeySta te As Integer
        End Structure

        <StructLayout(L ayoutKind.Expli cit)> _
        Structure EventUnion
        <FieldOffset(0) > _
        Public KeyEvent As KEY_EVENT_RECOR D
        <FieldOffset(0) > _
        Public MouseEvent As MOUSE_EVENT_REC ORD
        ....
        End Sturcture

        <StructLayout(L ayoutKind.Seque ntial)>
        Structure INPUT_RECORD
        Public EventType As Short
        Public Event As EventUnion
        End Structure

        Not a complete set of definitions, but something to get you going. I
        actually have most of these defined in C# somewhere... How much C# do
        you understand? If I find them, I could post that code.
        --
        Tom Shelton
        MVP [Visual Basic]

        Comment

        • Doug Perkes

          #5
          Re: WriteConsoleInp ut [was WriteConsoleOut put]

          Tom,

          C# would be perfect! I'm need to do this project in C# anyway...but the only
          examples I found were VB so I was using VB to see if I could get it to work.

          If you could post the C# code, I'd be very appreciative. Should I make a
          post to the C# group so it would be relevant to the group?

          Thanks,

          Doug


          "Tom Shelton" <tom@mtogden.co m> wrote in message
          news:O%23f47L2u DHA.3256@TK2MSF TNGP11.phx.gbl. ..[color=blue]
          > In article <#KgOWz1uDHA.21 80@TK2MSFTNGP09 .phx.gbl>, Doug Perkes wrote:[color=green]
          > > Sorry, I meant to type WriteConsoleInp ut. ;-(
          > >
          > > Could someone help me write the structs and declarations for
          > > WriteConsoleInp ut. I am very confused by the INPUT_RECORD struct and how[/color][/color]
          to[color=blue][color=green]
          > > create it in VB?
          > >
          > > Thanks,
          > >
          > > Doug[/color]
          >
          > Probably the unions... I would do something like this...
          >
          > <StructLayout(L ayoutKind.Expli cit)> _
          > StructLayout CharUnion
          > <FieldOffset(0) > _
          > Public UnicodeChar As Short
          > <FieldOffset(0) > _
          > Public AsciiChar As Byte
          > End Structure
          >
          >
          > <StructLayout(L ayoutKind.Seque ntial)> _
          > Structure KEY_EVENT_RECOR D
          > Public bKeyDown As Boolean
          > Public wReapeatCount As Short
          > Public wVirtualKeyCode As Short
          > Public wVirtualScanCod e As Short
          > Public uChar As CharUnion
          >
          > ' I'd probaby make a enum of the flags
          > ' here, but an integer still works :)
          > Public dwControlKeySta te As Integer
          > End Structure
          >
          > <StructLayout(L ayoutKind.Expli cit)> _
          > Structure EventUnion
          > <FieldOffset(0) > _
          > Public KeyEvent As KEY_EVENT_RECOR D
          > <FieldOffset(0) > _
          > Public MouseEvent As MOUSE_EVENT_REC ORD
          > ....
          > End Sturcture
          >
          > <StructLayout(L ayoutKind.Seque ntial)>
          > Structure INPUT_RECORD
          > Public EventType As Short
          > Public Event As EventUnion
          > End Structure
          >
          > Not a complete set of definitions, but something to get you going. I
          > actually have most of these defined in C# somewhere... How much C# do
          > you understand? If I find them, I could post that code.
          > --
          > Tom Shelton
          > MVP [Visual Basic][/color]


          Comment

          • Tom Shelton

            #6
            Re: WriteConsoleInp ut [was WriteConsoleOut put]

            In article <#rgGDr2uDHA.64 0@TK2MSFTNGP11. phx.gbl>, Doug Perkes wrote:[color=blue]
            > Tom,
            >
            > C# would be perfect! I'm need to do this project in C# anyway...but the only
            > examples I found were VB so I was using VB to see if I could get it to work.
            >
            > If you could post the C# code, I'd be very appreciative. Should I make a
            > post to the C# group so it would be relevant to the group?
            >
            > Thanks,
            >
            > Doug
            >
            >
            > "Tom Shelton" <tom@mtogden.co m> wrote in message
            > news:O%23f47L2u DHA.3256@TK2MSF TNGP11.phx.gbl. ..[color=green]
            >> In article <#KgOWz1uDHA.21 80@TK2MSFTNGP09 .phx.gbl>, Doug Perkes wrote:[color=darkred]
            >> > Sorry, I meant to type WriteConsoleInp ut. ;-(
            >> >
            >> > Could someone help me write the structs and declarations for
            >> > WriteConsoleInp ut. I am very confused by the INPUT_RECORD struct and how[/color][/color]
            > to[color=green][color=darkred]
            >> > create it in VB?
            >> >
            >> > Thanks,
            >> >
            >> > Doug[/color]
            >>
            >> Probably the unions... I would do something like this...
            >>
            >> <StructLayout(L ayoutKind.Expli cit)> _
            >> StructLayout CharUnion
            >> <FieldOffset(0) > _
            >> Public UnicodeChar As Short
            >> <FieldOffset(0) > _
            >> Public AsciiChar As Byte
            >> End Structure
            >>
            >>
            >> <StructLayout(L ayoutKind.Seque ntial)> _
            >> Structure KEY_EVENT_RECOR D
            >> Public bKeyDown As Boolean
            >> Public wReapeatCount As Short
            >> Public wVirtualKeyCode As Short
            >> Public wVirtualScanCod e As Short
            >> Public uChar As CharUnion
            >>
            >> ' I'd probaby make a enum of the flags
            >> ' here, but an integer still works :)
            >> Public dwControlKeySta te As Integer
            >> End Structure
            >>
            >> <StructLayout(L ayoutKind.Expli cit)> _
            >> Structure EventUnion
            >> <FieldOffset(0) > _
            >> Public KeyEvent As KEY_EVENT_RECOR D
            >> <FieldOffset(0) > _
            >> Public MouseEvent As MOUSE_EVENT_REC ORD
            >> ....
            >> End Sturcture
            >>
            >> <StructLayout(L ayoutKind.Seque ntial)>
            >> Structure INPUT_RECORD
            >> Public EventType As Short
            >> Public Event As EventUnion
            >> End Structure
            >>
            >> Not a complete set of definitions, but something to get you going. I
            >> actually have most of these defined in C# somewhere... How much C# do
            >> you understand? If I find them, I could post that code.
            >> --
            >> Tom Shelton
            >> MVP [Visual Basic][/color]
            >
            >[/color]


            I was writting a replacement for the Console class that handled multiple
            screen buffers, colors, ctrl events, etc - but the project kind of got
            side lined, so I'll have to look around and see if I still have it. If
            I do, I'll probably just post a link to the source anyway. So, I think
            it's ok to keep the discussion here. Anyway, I'll let you know one way
            or the other latter today.

            --
            Tom Shelton
            MVP [Visual Basic]

            Comment

            • Doug Perkes

              #7
              Re: WriteConsoleInp ut [was WriteConsoleOut put]

              Tom,

              Thank you! You have been more than helpful.

              -- Doug

              "Tom Shelton" <tom@mtogden.co m> wrote in message
              news:#NFVkN6uDH A.1764@TK2MSFTN GP10.phx.gbl...[color=blue]
              >
              >
              > "Tom Shelton" <tom@mtogden.co m> wrote in message
              > news:uKZklS3uDH A.2444@TK2MSFTN GP12.phx.gbl...[color=green]
              > > In article <#rgGDr2uDHA.64 0@TK2MSFTNGP11. phx.gbl>, Doug Perkes wrote:[color=darkred]
              > > > Tom,
              > > >
              > > > C# would be perfect! I'm need to do this project in C# anyway...but[/color][/color]
              > the only[color=green][color=darkred]
              > > > examples I found were VB so I was using VB to see if I could get it[/color][/color]
              > to work.[color=green][color=darkred]
              > > >
              > > > If you could post the C# code, I'd be very appreciative. Should I[/color][/color]
              > make a[color=green][color=darkred]
              > > > post to the C# group so it would be relevant to the group?
              > > >
              > > > Thanks,
              > > >
              > > > Doug[/color][/color]
              >
              > Doug,
              >
              > Well here you go. It is a whole lot of Console API functions. These
              > were from an early revision - I was really just defining them and
              > testing them. I hadn't even wrapped them in a class yet, but I can't
              > find a latter revision. Still, I'm sure that most of them are correct
              > or nearly so. If you have troubles with any individual call, then let
              > me know and I can help you out.
              >
              > I also included a class that was from the test project. It is a
              > modified version of one of the MSDN Console function examples ,
              > translated into C# and using several of the API calls.
              >
              > Anyway,
              > HTH
              > --
              > Tom Shelton
              > [MVP - Visual Basic]
              >
              >
              >[/color]


              Comment

              • Doug Perkes

                #8
                Re: WriteConsoleInp ut [was WriteConsoleOut put]

                Tom,

                Please forgive my ignorance again. I just can't seem to figure this out.

                I am trying to create a method called SendKeys that will write a string to
                the console input.

                Here are the questions I have:
                1. Do I need to send both a key down and a key up event?
                2. What do I put for the ControlKeyState value if no control key is pressed
                3. How do I get the value of the wVirtualKeyCode ?
                4. How do I get the value of the wVirtualScanCod e?

                Thanks again for all your help,

                Doug

                Here's what I have so far:
                private static void SendKeys(string text)
                {
                int length = text.Length;
                INPUT_RECORD[] buffer = new INPUT_RECORD[length];
                int eventsWritten = -1;
                System.Text.ASC IIEncoding encoder = new ASCIIEncoding() ;
                Byte[] bytes = encoder.GetByte s(text);

                for (int i=0;i<length;i+ +)
                {
                KEY_EVENT_RECOR D rec = new KEY_EVENT_RECOR D();
                EVENT_UNION union = new EVENT_UNION();
                union.KeyEvent = rec;
                //Do I need to send both a key down and a key up event?
                rec.bKeyDown = true;
                //What do I put for dwControlKeySta te if no control key is pressed
                //rec.dwControlKe yState = ControlKeyState .????;
                rec.uChar.Ascii Char = bytes[i];
                rec.wRepeatCoun t = 1;
                //how do I get the value of the wVirtualKeyCode
                //rec.wVirtualKey Code = ????;
                //how do I get the value of the wVirtualScanCod e
                //rec.wVirtualSca nCode = ????;
                buffer[i].EventType = InputEventType. KeyEvent;
                buffer[i].Event = union;
                }
                ConsoleApi.Writ eConsoleInput(
                hConsoleInput,
                buffer[0],
                length,
                eventsWritten);

                }


                "Tom Shelton" <tom@mtogden.co m> wrote in message
                news:#NFVkN6uDH A.1764@TK2MSFTN GP10.phx.gbl...[color=blue]
                >
                >
                > "Tom Shelton" <tom@mtogden.co m> wrote in message
                > news:uKZklS3uDH A.2444@TK2MSFTN GP12.phx.gbl...[color=green]
                > > In article <#rgGDr2uDHA.64 0@TK2MSFTNGP11. phx.gbl>, Doug Perkes wrote:[color=darkred]
                > > > Tom,
                > > >
                > > > C# would be perfect! I'm need to do this project in C# anyway...but[/color][/color]
                > the only[color=green][color=darkred]
                > > > examples I found were VB so I was using VB to see if I could get it[/color][/color]
                > to work.[color=green][color=darkred]
                > > >
                > > > If you could post the C# code, I'd be very appreciative. Should I[/color][/color]
                > make a[color=green][color=darkred]
                > > > post to the C# group so it would be relevant to the group?
                > > >
                > > > Thanks,
                > > >
                > > > Doug[/color][/color]
                >
                > Doug,
                >
                > Well here you go. It is a whole lot of Console API functions. These
                > were from an early revision - I was really just defining them and
                > testing them. I hadn't even wrapped them in a class yet, but I can't
                > find a latter revision. Still, I'm sure that most of them are correct
                > or nearly so. If you have troubles with any individual call, then let
                > me know and I can help you out.
                >
                > I also included a class that was from the test project. It is a
                > modified version of one of the MSDN Console function examples ,
                > translated into C# and using several of the API calls.
                >
                > Anyway,
                > HTH
                > --
                > Tom Shelton
                > [MVP - Visual Basic]
                >
                >
                >[/color]


                Comment

                • Tom Shelton

                  #9
                  Re: WriteConsoleInp ut [was WriteConsoleOut put]

                  In article <uK8uPhmvDHA.27 12@tk2msftngp13 .phx.gbl>, Doug Perkes wrote:[color=blue]
                  > Tom,
                  >
                  > Please forgive my ignorance again. I just can't seem to figure this out.
                  >
                  > I am trying to create a method called SendKeys that will write a string to
                  > the console input.
                  >
                  > Here are the questions I have:
                  > 1. Do I need to send both a key down and a key up event?
                  > 2. What do I put for the ControlKeyState value if no control key is pressed
                  > 3. How do I get the value of the wVirtualKeyCode ?
                  > 4. How do I get the value of the wVirtualScanCod e?
                  >
                  > Thanks again for all your help,
                  >
                  > Doug
                  >
                  > Here's what I have so far:
                  > private static void SendKeys(string text)
                  > {
                  > int length = text.Length;
                  > INPUT_RECORD[] buffer = new INPUT_RECORD[length];
                  > int eventsWritten = -1;
                  > System.Text.ASC IIEncoding encoder = new ASCIIEncoding() ;
                  > Byte[] bytes = encoder.GetByte s(text);
                  >
                  > for (int i=0;i<length;i+ +)
                  > {
                  > KEY_EVENT_RECOR D rec = new KEY_EVENT_RECOR D();
                  > EVENT_UNION union = new EVENT_UNION();
                  > union.KeyEvent = rec;
                  > //Do I need to send both a key down and a key up event?
                  > rec.bKeyDown = true;
                  > //What do I put for dwControlKeySta te if no control key is pressed
                  > //rec.dwControlKe yState = ControlKeyState .????;
                  > rec.uChar.Ascii Char = bytes[i];
                  > rec.wRepeatCoun t = 1;
                  > //how do I get the value of the wVirtualKeyCode
                  > //rec.wVirtualKey Code = ????;
                  > //how do I get the value of the wVirtualScanCod e
                  > //rec.wVirtualSca nCode = ????;
                  > buffer[i].EventType = InputEventType. KeyEvent;
                  > buffer[i].Event = union;
                  > }
                  > ConsoleApi.Writ eConsoleInput(
                  > hConsoleInput,
                  > buffer[0],
                  > length,
                  > eventsWritten);
                  >
                  > }
                  >
                  >
                  > "Tom Shelton" <tom@mtogden.co m> wrote in message
                  > news:#NFVkN6uDH A.1764@TK2MSFTN GP10.phx.gbl...[color=green]
                  >>
                  >>
                  >> "Tom Shelton" <tom@mtogden.co m> wrote in message
                  >> news:uKZklS3uDH A.2444@TK2MSFTN GP12.phx.gbl...[color=darkred]
                  >> > In article <#rgGDr2uDHA.64 0@TK2MSFTNGP11. phx.gbl>, Doug Perkes wrote:
                  >> > > Tom,
                  >> > >
                  >> > > C# would be perfect! I'm need to do this project in C# anyway...but[/color]
                  >> the only[color=darkred]
                  >> > > examples I found were VB so I was using VB to see if I could get it[/color]
                  >> to work.[color=darkred]
                  >> > >
                  >> > > If you could post the C# code, I'd be very appreciative. Should I[/color]
                  >> make a[color=darkred]
                  >> > > post to the C# group so it would be relevant to the group?
                  >> > >
                  >> > > Thanks,
                  >> > >
                  >> > > Doug[/color]
                  >>
                  >> Doug,
                  >>
                  >> Well here you go. It is a whole lot of Console API functions. These
                  >> were from an early revision - I was really just defining them and
                  >> testing them. I hadn't even wrapped them in a class yet, but I can't
                  >> find a latter revision. Still, I'm sure that most of them are correct
                  >> or nearly so. If you have troubles with any individual call, then let
                  >> me know and I can help you out.
                  >>
                  >> I also included a class that was from the test project. It is a
                  >> modified version of one of the MSDN Console function examples ,
                  >> translated into C# and using several of the API calls.
                  >>
                  >> Anyway,
                  >> HTH
                  >> --
                  >> Tom Shelton
                  >> [MVP - Visual Basic]
                  >>
                  >>
                  >>[/color]
                  >
                  >[/color]
                  Doug,

                  I just saw this message. I'll see if I can play around with this
                  tonight....

                  --
                  Tom Shelton
                  MVP [Visual Basic]

                  Comment

                  • Tom Shelton

                    #10
                    Re: WriteConsoleInp ut [was WriteConsoleOut put]

                    In article <uK8uPhmvDHA.27 12@tk2msftngp13 .phx.gbl>, Doug Perkes wrote:[color=blue]
                    > Tom,
                    >
                    > Please forgive my ignorance again. I just can't seem to figure this out.
                    >
                    > I am trying to create a method called SendKeys that will write a string to
                    > the console input.
                    >
                    > Here are the questions I have:
                    > 1. Do I need to send both a key down and a key up event?
                    > 2. What do I put for the ControlKeyState value if no control key is pressed
                    > 3. How do I get the value of the wVirtualKeyCode ?
                    > 4. How do I get the value of the wVirtualScanCod e?
                    >
                    > Thanks again for all your help,
                    >
                    > Doug
                    >
                    > Here's what I have so far:
                    > private static void SendKeys(string text)
                    > {
                    > int length = text.Length;
                    > INPUT_RECORD[] buffer = new INPUT_RECORD[length];
                    > int eventsWritten = -1;
                    > System.Text.ASC IIEncoding encoder = new ASCIIEncoding() ;
                    > Byte[] bytes = encoder.GetByte s(text);
                    >
                    > for (int i=0;i<length;i+ +)
                    > {
                    > KEY_EVENT_RECOR D rec = new KEY_EVENT_RECOR D();
                    > EVENT_UNION union = new EVENT_UNION();
                    > union.KeyEvent = rec;
                    > //Do I need to send both a key down and a key up event?
                    > rec.bKeyDown = true;
                    > //What do I put for dwControlKeySta te if no control key is pressed
                    > //rec.dwControlKe yState = ControlKeyState .????;
                    > rec.uChar.Ascii Char = bytes[i];
                    > rec.wRepeatCoun t = 1;
                    > //how do I get the value of the wVirtualKeyCode
                    > //rec.wVirtualKey Code = ????;
                    > //how do I get the value of the wVirtualScanCod e
                    > //rec.wVirtualSca nCode = ????;
                    > buffer[i].EventType = InputEventType. KeyEvent;
                    > buffer[i].Event = union;
                    > }
                    > ConsoleApi.Writ eConsoleInput(
                    > hConsoleInput,
                    > buffer[0],
                    > length,
                    > eventsWritten);
                    >
                    > }
                    >
                    >
                    > "Tom Shelton" <tom@mtogden.co m> wrote in message
                    > news:#NFVkN6uDH A.1764@TK2MSFTN GP10.phx.gbl...[color=green]
                    >>
                    >>
                    >> "Tom Shelton" <tom@mtogden.co m> wrote in message
                    >> news:uKZklS3uDH A.2444@TK2MSFTN GP12.phx.gbl...[color=darkred]
                    >> > In article <#rgGDr2uDHA.64 0@TK2MSFTNGP11. phx.gbl>, Doug Perkes wrote:
                    >> > > Tom,
                    >> > >
                    >> > > C# would be perfect! I'm need to do this project in C# anyway...but[/color]
                    >> the only[color=darkred]
                    >> > > examples I found were VB so I was using VB to see if I could get it[/color]
                    >> to work.[color=darkred]
                    >> > >
                    >> > > If you could post the C# code, I'd be very appreciative. Should I[/color]
                    >> make a[color=darkred]
                    >> > > post to the C# group so it would be relevant to the group?
                    >> > >
                    >> > > Thanks,
                    >> > >
                    >> > > Doug[/color]
                    >>
                    >> Doug,
                    >>
                    >> Well here you go. It is a whole lot of Console API functions. These
                    >> were from an early revision - I was really just defining them and
                    >> testing them. I hadn't even wrapped them in a class yet, but I can't
                    >> find a latter revision. Still, I'm sure that most of them are correct
                    >> or nearly so. If you have troubles with any individual call, then let
                    >> me know and I can help you out.
                    >>
                    >> I also included a class that was from the test project. It is a
                    >> modified version of one of the MSDN Console function examples ,
                    >> translated into C# and using several of the API calls.
                    >>
                    >> Anyway,
                    >> HTH
                    >> --
                    >> Tom Shelton
                    >> [MVP - Visual Basic]
                    >>
                    >>
                    >>[/color]
                    >
                    >[/color]

                    Doug,

                    Forgive me... I haven't had time to look at this yet. I was wondering
                    if you actually got it working in the mean time? If not you may want to
                    move it over to the framework interop goup. Things are a little hectic
                    right now, and I'm not sure I can get to digging into this for a few
                    days.

                    --
                    Tom Shelton
                    MVP [Visual Basic]

                    Comment

                    • Doug Perkes

                      #11
                      Re: WriteConsoleInp ut [was WriteConsoleOut put]

                      I haven't got it working. I'm posting it to the Interop group now.

                      Thanks again for all your help,

                      Doug

                      "Tom Shelton" <tom@mtogden.co m> wrote in message
                      news:OzXry3NwDH A.1908@TK2MSFTN GP10.phx.gbl...[color=blue]
                      > In article <uK8uPhmvDHA.27 12@tk2msftngp13 .phx.gbl>, Doug Perkes wrote:[color=green]
                      > > Tom,
                      > >
                      > > Please forgive my ignorance again. I just can't seem to figure this out.
                      > >
                      > > I am trying to create a method called SendKeys that will write a string[/color][/color]
                      to[color=blue][color=green]
                      > > the console input.
                      > >
                      > > Here are the questions I have:
                      > > 1. Do I need to send both a key down and a key up event?
                      > > 2. What do I put for the ControlKeyState value if no control key is[/color][/color]
                      pressed[color=blue][color=green]
                      > > 3. How do I get the value of the wVirtualKeyCode ?
                      > > 4. How do I get the value of the wVirtualScanCod e?
                      > >
                      > > Thanks again for all your help,
                      > >
                      > > Doug
                      > >
                      > > Here's what I have so far:
                      > > private static void SendKeys(string text)
                      > > {
                      > > int length = text.Length;
                      > > INPUT_RECORD[] buffer = new INPUT_RECORD[length];
                      > > int eventsWritten = -1;
                      > > System.Text.ASC IIEncoding encoder = new ASCIIEncoding() ;
                      > > Byte[] bytes = encoder.GetByte s(text);
                      > >
                      > > for (int i=0;i<length;i+ +)
                      > > {
                      > > KEY_EVENT_RECOR D rec = new KEY_EVENT_RECOR D();
                      > > EVENT_UNION union = new EVENT_UNION();
                      > > union.KeyEvent = rec;
                      > > //Do I need to send both a key down and a key up event?
                      > > rec.bKeyDown = true;
                      > > //What do I put for dwControlKeySta te if no control key is pressed
                      > > //rec.dwControlKe yState = ControlKeyState .????;
                      > > rec.uChar.Ascii Char = bytes[i];
                      > > rec.wRepeatCoun t = 1;
                      > > //how do I get the value of the wVirtualKeyCode
                      > > //rec.wVirtualKey Code = ????;
                      > > //how do I get the value of the wVirtualScanCod e
                      > > //rec.wVirtualSca nCode = ????;
                      > > buffer[i].EventType = InputEventType. KeyEvent;
                      > > buffer[i].Event = union;
                      > > }
                      > > ConsoleApi.Writ eConsoleInput(
                      > > hConsoleInput,
                      > > buffer[0],
                      > > length,
                      > > eventsWritten);
                      > >
                      > > }
                      > >
                      > >
                      > > "Tom Shelton" <tom@mtogden.co m> wrote in message
                      > > news:#NFVkN6uDH A.1764@TK2MSFTN GP10.phx.gbl...[color=darkred]
                      > >>
                      > >>
                      > >> "Tom Shelton" <tom@mtogden.co m> wrote in message
                      > >> news:uKZklS3uDH A.2444@TK2MSFTN GP12.phx.gbl...
                      > >> > In article <#rgGDr2uDHA.64 0@TK2MSFTNGP11. phx.gbl>, Doug Perkes wrote:
                      > >> > > Tom,
                      > >> > >
                      > >> > > C# would be perfect! I'm need to do this project in C# anyway...but
                      > >> the only
                      > >> > > examples I found were VB so I was using VB to see if I could get it
                      > >> to work.
                      > >> > >
                      > >> > > If you could post the C# code, I'd be very appreciative. Should I
                      > >> make a
                      > >> > > post to the C# group so it would be relevant to the group?
                      > >> > >
                      > >> > > Thanks,
                      > >> > >
                      > >> > > Doug
                      > >>
                      > >> Doug,
                      > >>
                      > >> Well here you go. It is a whole lot of Console API functions. These
                      > >> were from an early revision - I was really just defining them and
                      > >> testing them. I hadn't even wrapped them in a class yet, but I can't
                      > >> find a latter revision. Still, I'm sure that most of them are correct
                      > >> or nearly so. If you have troubles with any individual call, then let
                      > >> me know and I can help you out.
                      > >>
                      > >> I also included a class that was from the test project. It is a
                      > >> modified version of one of the MSDN Console function examples ,
                      > >> translated into C# and using several of the API calls.
                      > >>
                      > >> Anyway,
                      > >> HTH
                      > >> --
                      > >> Tom Shelton
                      > >> [MVP - Visual Basic]
                      > >>
                      > >>
                      > >>[/color]
                      > >
                      > >[/color]
                      >
                      > Doug,
                      >
                      > Forgive me... I haven't had time to look at this yet. I was wondering
                      > if you actually got it working in the mean time? If not you may want to
                      > move it over to the framework interop goup. Things are a little hectic
                      > right now, and I'm not sure I can get to digging into this for a few
                      > days.
                      >
                      > --
                      > Tom Shelton
                      > MVP [Visual Basic][/color]


                      Comment

                      Working...