WriteConsoleOutput

D

Doug Perkes

Does anyone have an example of declaring and using the WriteConsoleOutput
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
 
M

Mattias Sjögren

Doug,
Does anyone have an example of declaring and using the WriteConsoleOutput
Win32 API function?

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

Declare Auto Function WriteConsoleOutput Lib "kernel32.dll" (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
 
D

Doug Perkes

Sorry, I meant to type WriteConsoleInput. ;-(

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

Thanks,

Doug
 
T

Tom Shelton

Sorry, I meant to type WriteConsoleInput. ;-(

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

Thanks,

Doug

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

<StructLayout(LayoutKind.Explicit)> _
StructLayout CharUnion
<FieldOffset(0)> _
Public UnicodeChar As Short
<FieldOffset(0)> _
Public AsciiChar As Byte
End Structure


<StructLayout(LayoutKind.Sequential)> _
Structure KEY_EVENT_RECORD
Public bKeyDown As Boolean
Public wReapeatCount As Short
Public wVirtualKeyCode As Short
Public wVirtualScanCode As Short
Public uChar As CharUnion

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

<StructLayout(LayoutKind.Explicit)> _
Structure EventUnion
<FieldOffset(0)> _
Public KeyEvent As KEY_EVENT_RECORD
<FieldOffset(0)> _
Public MouseEvent As MOUSE_EVENT_RECORD
....
End Sturcture

<StructLayout(LayoutKind.Sequential)>
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.
 
D

Doug Perkes

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
 
T

Tom Shelton

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 said:
Probably the unions... I would do something like this...

<StructLayout(LayoutKind.Explicit)> _
StructLayout CharUnion
<FieldOffset(0)> _
Public UnicodeChar As Short
<FieldOffset(0)> _
Public AsciiChar As Byte
End Structure


<StructLayout(LayoutKind.Sequential)> _
Structure KEY_EVENT_RECORD
Public bKeyDown As Boolean
Public wReapeatCount As Short
Public wVirtualKeyCode As Short
Public wVirtualScanCode As Short
Public uChar As CharUnion

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

<StructLayout(LayoutKind.Explicit)> _
Structure EventUnion
<FieldOffset(0)> _
Public KeyEvent As KEY_EVENT_RECORD
<FieldOffset(0)> _
Public MouseEvent As MOUSE_EVENT_RECORD
....
End Sturcture

<StructLayout(LayoutKind.Sequential)>
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.


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

Doug Perkes

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

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.ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] bytes = encoder.GetBytes(text);

for (int i=0;i<length;i++)
{
KEY_EVENT_RECORD rec = new KEY_EVENT_RECORD();
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 dwControlKeyState if no control key is pressed
//rec.dwControlKeyState = ControlKeyState.????;
rec.uChar.AsciiChar = bytes;
rec.wRepeatCount = 1;
//how do I get the value of the wVirtualKeyCode
//rec.wVirtualKeyCode = ????;
//how do I get the value of the wVirtualScanCode
//rec.wVirtualScanCode = ????;
buffer.EventType = InputEventType.KeyEvent;
buffer.Event = union;
}
ConsoleApi.WriteConsoleInput(
hConsoleInput,
buffer[0],
length,
eventsWritten);

}
 
T

Tom Shelton

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

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.ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] bytes = encoder.GetBytes(text);

for (int i=0;i<length;i++)
{
KEY_EVENT_RECORD rec = new KEY_EVENT_RECORD();
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 dwControlKeyState if no control key is pressed
//rec.dwControlKeyState = ControlKeyState.????;
rec.uChar.AsciiChar = bytes;
rec.wRepeatCount = 1;
//how do I get the value of the wVirtualKeyCode
//rec.wVirtualKeyCode = ????;
//how do I get the value of the wVirtualScanCode
//rec.wVirtualScanCode = ????;
buffer.EventType = InputEventType.KeyEvent;
buffer.Event = union;
}
ConsoleApi.WriteConsoleInput(
hConsoleInput,
buffer[0],
length,
eventsWritten);

}


Tom Shelton said:
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

Doug,

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

Tom Shelton

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

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.ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] bytes = encoder.GetBytes(text);

for (int i=0;i<length;i++)
{
KEY_EVENT_RECORD rec = new KEY_EVENT_RECORD();
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 dwControlKeyState if no control key is pressed
//rec.dwControlKeyState = ControlKeyState.????;
rec.uChar.AsciiChar = bytes;
rec.wRepeatCount = 1;
//how do I get the value of the wVirtualKeyCode
//rec.wVirtualKeyCode = ????;
//how do I get the value of the wVirtualScanCode
//rec.wVirtualScanCode = ????;
buffer.EventType = InputEventType.KeyEvent;
buffer.Event = union;
}
ConsoleApi.WriteConsoleInput(
hConsoleInput,
buffer[0],
length,
eventsWritten);

}


Tom Shelton said:
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


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

Doug Perkes

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

Thanks again for all your help,

Doug

Tom Shelton said:
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 wVirtualScanCode?

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.ASCIIEncoding encoder = new ASCIIEncoding();
Byte[] bytes = encoder.GetBytes(text);

for (int i=0;i<length;i++)
{
KEY_EVENT_RECORD rec = new KEY_EVENT_RECORD();
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 dwControlKeyState if no control key is pressed
//rec.dwControlKeyState = ControlKeyState.????;
rec.uChar.AsciiChar = bytes;
rec.wRepeatCount = 1;
//how do I get the value of the wVirtualKeyCode
//rec.wVirtualKeyCode = ????;
//how do I get the value of the wVirtualScanCode
//rec.wVirtualScanCode = ????;
buffer.EventType = InputEventType.KeyEvent;
buffer.Event = union;
}
ConsoleApi.WriteConsoleInput(
hConsoleInput,
buffer[0],
length,
eventsWritten);

}


Tom Shelton said:
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


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top