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 10 2459
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
--
Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
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
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OU*************@TK2MSFTNGP11.phx.gbl... 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
-- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
In article <#K**************@TK2MSFTNGP09.phx.gbl>, Doug Perkes wrote: 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.
--
Tom Shelton
MVP [Visual Basic]
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" <to*@mtogden.com> wrote in message
news:O%****************@TK2MSFTNGP11.phx.gbl... In article <#K**************@TK2MSFTNGP09.phx.gbl>, Doug Perkes wrote: 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. -- Tom Shelton MVP [Visual Basic]
In article <#r*************@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
"Tom Shelton" <to*@mtogden.com> wrote in message news:O%****************@TK2MSFTNGP11.phx.gbl... In article <#K**************@TK2MSFTNGP09.phx.gbl>, Doug Perkes wrote: > 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. -- Tom Shelton MVP [Visual Basic]
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]
Tom,
Thank you! You have been more than helpful.
-- Doug
"Tom Shelton" <to*@mtogden.com> wrote in message
news:#N**************@TK2MSFTNGP10.phx.gbl...
"Tom Shelton" <to*@mtogden.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... In article <#r*************@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]
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[i];
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[i].EventType = InputEventType.KeyEvent;
buffer[i].Event = union;
}
ConsoleApi.WriteConsoleInput(
hConsoleInput,
buffer[0],
length,
eventsWritten);
}
"Tom Shelton" <to*@mtogden.com> wrote in message
news:#N**************@TK2MSFTNGP10.phx.gbl...
"Tom Shelton" <to*@mtogden.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... In article <#r*************@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]
In article <uK**************@tk2msftngp13.phx.gbl>, Doug Perkes wrote: 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[i]; 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[i].EventType = InputEventType.KeyEvent; buffer[i].Event = union; } ConsoleApi.WriteConsoleInput( hConsoleInput, buffer[0], length, eventsWritten);
}
"Tom Shelton" <to*@mtogden.com> wrote in message news:#N**************@TK2MSFTNGP10.phx.gbl...
"Tom Shelton" <to*@mtogden.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... > In article <#r*************@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]
Doug,
I just saw this message. I'll see if I can play around with this
tonight....
--
Tom Shelton
MVP [Visual Basic]
In article <uK**************@tk2msftngp13.phx.gbl>, Doug Perkes wrote: 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[i]; 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[i].EventType = InputEventType.KeyEvent; buffer[i].Event = union; } ConsoleApi.WriteConsoleInput( hConsoleInput, buffer[0], length, eventsWritten);
}
"Tom Shelton" <to*@mtogden.com> wrote in message news:#N**************@TK2MSFTNGP10.phx.gbl...
"Tom Shelton" <to*@mtogden.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... > In article <#r*************@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]
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]
I haven't got it working. I'm posting it to the Interop group now.
Thanks again for all your help,
Doug
"Tom Shelton" <to*@mtogden.com> wrote in message
news:Oz**************@TK2MSFTNGP10.phx.gbl... In article <uK**************@tk2msftngp13.phx.gbl>, Doug Perkes wrote: 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[i]; 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[i].EventType = InputEventType.KeyEvent; buffer[i].Event = union; } ConsoleApi.WriteConsoleInput( hConsoleInput, buffer[0], length, eventsWritten);
}
"Tom Shelton" <to*@mtogden.com> wrote in message news:#N**************@TK2MSFTNGP10.phx.gbl...
"Tom Shelton" <to*@mtogden.com> wrote in message news:uK**************@TK2MSFTNGP12.phx.gbl... > In article <#r*************@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]
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] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ken VdB |
last post by:
Hi everyone,
One of the things that really interested me about VS.net was that I can now
create a console based app using VB. I have a number of QuickBASIC 4.5 apps
which we are still using and...
|
by: jas |
last post by:
Hi,
I would like to start a new process and be able to read/write from/to
it. I have tried things like...
import subprocess as sp
p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
|
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...
|
by: Jarod_24 |
last post by:
I got a project called "Forms" that hold some forms and stuff in my
solution.
A argument at startup defines wether to use a From or Console.
My plan was to create a myConsole class that would...
|
by: mastermagrath |
last post by:
Hi,
Sorry for the possibly silly question.
Am i right in saying that the C library i use on a windows machine
really utilises the windows API for all its operations e.g. opening
files, printing...
|
by: poopsy |
last post by:
hi all
i wud like to know wat is the equivalent of gotoxy(x,y) in C++?? i found that in many codes on the net
wat's the function of gotoxy..can sumbody plz explain
i've never done C, ive always...
|
by: Atemporal |
last post by:
When using cout, I can use \b to go back one character in the same
line,
How can I go back to last line to output something in a easy way?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |