I have the following function (from windows.h) which I am trying to use the Python C API to allow python to call it. This is the function:
BOOL ReadConsoleOutputCharacter(
HANDLE hConsoleOutput, (Type Handle to Change (use STD_OUTPUT))
LPTSTR lpCharacter, (char pointer to store Characters read)
DWORD nLength, (length to read)
COORD dwReadCoord, (Type COORD to start reading at)
LPDWORD lpNumberOfCharsRead (length actually read)
This is a function that uses it correctly:
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
HANDLE hOut;
char Letters[5];
COORD Where;
DWORD NumRead;
int i;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "A line of little consequence." << endl;
Where.X = 4;
ReadConsoleOutputCharacter(hOut,
Letters,
5,
Where,
&NumRead);
cout << "5 letters starting from (4,0) ";
cout << endl;
cin.get(); //To see output
return 0;
}
This is the function that I have written to try to allow python to access it:
static PyObject *
WindowsH_ReadCharacters(PyObject *self, PyObject *args)
{
int length;
length = 1;
int x;
int y;
if (!PyArg_ParseTuple(args, "iii", &length, &x, &y))
return PyInt_FromLong(1);
char read[60000]; //I know this is a large number, I'm not sure how much the user will need.
char *_string;
COORD position;
position.X = x;
position.Y = y;
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
ReadConsoleOutputCharacter(
hOut,
read,
length,
position,
NULL);
for(x = 0; x <= length; x++)
{
_string[x] = read[x];
}
return Py_BuildValue("s", _string);
}
Can anybody help me? Python crashes as soon as I call this. If anyone has a way that could make it better, please share, it'll help =]
And if a mod thinks this will do better in C/C++, please move it there