|
-
Nov 16th, 2001, 02:51 AM
#1
Thread Starter
New Member
Console API Problems
I'm having a problem with the console API functions:
ReadConsoleOutput() and WriteConsoleOutput()
I would be extremely appreciative if anyone had some code that demonstrated their proper use.
Thanks,
John
-
Nov 16th, 2001, 12:49 PM
#2
Thread Starter
New Member
Thanks Jim, but I'm looking to read and write blocks of data from/to a screen buffer. Right now, I'm using ReadConsole() and WriteConsole() but they're a little slow in fullscreen mode.
The problem that I'm having is that (besides the fact that the VB Declares were inaccurate - as usual) the api calls to ReadConsoleOutput() and WriteConsoleOutput() are successful and the lpWriteRegion param returns the proper values but the lpBuffer array is always empty.
I have a C code example from the Microsoft SDK but direct translation to VB doesn't work. My opinion is that either the VB Declare is still inaccurate or the VB CHAR_INFO structure is inaccurate.
If you would like to see the code I'm working with, just let me know.
Thanks again for your help.
John
-
Nov 16th, 2001, 12:53 PM
#3
The C code and your VB version would be better. I'm assuming the C is an MSDN example or some known good code.
-
Nov 16th, 2001, 12:55 PM
#4
Oh. I also forgot. THis site has a set of VB examples:
http://www.vb-world.net/api/console/
-
Nov 16th, 2001, 01:31 PM
#5
Thread Starter
New Member
I'm using VB6 SP5. The C example below is from the October 2000 version of the MS Platform SDK.
This is the declare that I'm using:
Private Declare Function ReadConsoleOutput Lib "kernel32" Alias "ReadConsoleOutputA" (ByVal hConsoleOutput As Long, lpBuffer As CHAR_INFO, dwBufferSize As COORD, dwBufferCoord As COORD, lpReadRegion As Long) As Long
It seems that you have to pass the lpReadRegion param as a long instead of a SMALL_RECT. You might also have to pass the COORDs as longs. Here is the function that I'm using to convert the COORDs to longs:
Private Function pGetCoord(ByVal x As Long, ByVal y As Long) As Long
'
Dim crd As Long
CopyMemory crd, x, 2
CopyMemory ByVal (VarPtr(crd) + 2), y, 2
pGetCoord = crd
End Function
There might be a problem with the CHAR_INFO structure. In C, it includes a Union. This is what VB offers:
Private Type CHAR_INFO
iChar As Integer
iAttributes As Integer
End Type
Here is part of the VB code that I was using. All I end up getting from Debug.Print is zeros.
Dim i As Long
Dim ret As Long
Dim crdc As COORD 'Long
Dim crds As COORD 'Long
Dim srr As SMALL_RECT
Dim srw As SMALL_RECT
'Set the source rectangle
srr.top = 0
srr.Left = 0
srr.Bottom = 1
srr.Right = 79
'The temporary buffer size is 2 rows x 80 columns
'crds = pGetCoord(80, 2)
crds.x = 80
crds.y = 2
'The top left destination cell of the temporary buffer is row 0, col 0
'crdc = pGetCoord(0,0)
crdc.x = 0
crdc.y = 0
'Copy the block from the screen buffer to the temp buffer
ReDim ci(160) As CHAR_INFO
ret = ReadConsoleOutput(ml_StdOutput, ci(0), crds, crdc, VarPtr(srr))
For i = 0 To 160
Debug.Print ci(i).iChar, ci(i).iAttributes
Next i
---
#include <windows.h>
VOID main(void)
{
HANDLE hStdout, hNewScreenBuffer;
SMALL_RECT srctReadRect;
SMALL_RECT srctWriteRect;
CHAR_INFO chiBuffer[160]; // [2][80];
COORD coordBufSize;
COORD coordBufCoord;
BOOL fSuccess;
// Get a handle to the STDOUT screen buffer to copy from and
// create a new screen buffer to copy to.
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
hNewScreenBuffer = CreateConsoleScreenBuffer(
GENERIC_READ | // read/write access
GENERIC_WRITE,
0, // not shared
NULL, // no security attributes
CONSOLE_TEXTMODE_BUFFER, // must be TEXTMODE
NULL); // reserved; must be NULL
if (hStdout == INVALID_HANDLE_VALUE ||
hNewScreenBuffer == INVALID_HANDLE_VALUE)
{
MyErrorExit("CreateConsoleScreenBuffer");
}
// Make the new screen buffer the active screen buffer.
if (! SetConsoleActiveScreenBuffer(hNewScreenBuffer) )
MyErrorExit("SetConsoleActiveScreenBuffer");
// Set the source rectangle.
srctReadRect.Top = 0; // top left: row 0, col 0
srctReadRect.Left = 0;
srctReadRect.Bottom = 1; // bot. right: row 1, col 79
srctReadRect.Right = 79;
// The temporary buffer size is 2 rows x 80 columns.
coordBufSize.Y = 2;
coordBufSize.X = 80;
// The top left destination cell of the temporary buffer is
// row 0, col 0.
coordBufCoord.X = 0;
coordBufCoord.Y = 0;
// Copy the block from the screen buffer to the temp. buffer.
fSuccess = ReadConsoleOutput(
hStdout, // screen buffer to read from
chiBuffer, // buffer to copy into
coordBufSize, // col-row size of chiBuffer
coordBufCoord, // top left dest. cell in chiBuffer
&srctReadRect); // screen buffer source rectangle
if (! fSuccess)
MyErrorExit("ReadConsoleOutput");
// Set the destination rectangle.
srctWriteRect.Top = 10; // top lt: row 10, col 0
srctWriteRect.Left = 0;
srctWriteRect.Bottom = 11; // bot. rt: row 11, col 79
srctWriteRect.Right = 79;
// Copy from the temporary buffer to the new screen buffer.
fSuccess = WriteConsoleOutput(
hNewScreenBuffer, // screen buffer to write to
chiBuffer, // buffer to copy from
coordBufSize, // col-row size of chiBuffer
coordBufCoord, // top left src cell in chiBuffer
&srctWriteRect); // dest. screen buffer rectangle
if (! fSuccess)
MyErrorExit("WriteConsoleOutput");
Sleep(10000);
// Restore the original active screen buffer.
if (! SetConsoleActiveScreenBuffer(hStdout))
MyErrorExit("SetConsoleActiveScreenBuffer");
}
-
Nov 16th, 2001, 02:22 PM
#6
I already see what I think are problems, but I don't have VB here. I'll get you an answer in a while.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|