I'm working with WinApi in C++ to change console properties and get the segmentation fault error when calling GetConsoleScreenBufferInfo() in the last two functions.
It's strange that if I reproduce the same piece of code -just calling GetConsoleScreenBufferInfo() with same declarations of HANDLE and PCONSOLE_SCREEN_BUFFER_INFO- in main() I get no error, but if I do so inside getPosCursorX() I still get the error.
namespace consola{
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
PCONSOLE_SCREEN_BUFFER_INFO infoCon;
struct TamanoConsola
{
COORD buffer;
SMALL_RECT ventana;
} inicial;
void gotoxy(int x, int y)
{
COORD dwPos;
dwPos.X=x;
dwPos.Y=y;
SetConsoleCursorPosition(hCon, dwPos);
}
void setColor(int texto, int fondo)
{
SetConsoleTextAttribute(hCon, fondo*16+texto);
}
void setBufferTamano(int ancho, int alto)
{
COORD buffertamano = {ancho, alto};
if(SetConsoleScreenBufferSize(hCon, buffertamano) == 0)
std::cout << "ERROR de WinApi numero: " << GetLastError() << std::endl;
}
void setVentanaTamano(int ancho, int alto)
{
SMALL_RECT ventanatamano1 ={0, 0, ancho-1, alto-1};
SMALL_RECT* ventanatamano = &ventanatamano1;
if(SetConsoleWindowInfo(hCon, TRUE, ventanatamano) == 0)
std::cout << "ERROR de WinApi numero: " << GetLastError() << std::endl;
}
void setVentanaBufferTamano(int anchoVentana, int altoVentana, int anchoBuffer, int altoBuffer)
{
setBufferTamano(anchoBuffer, altoBuffer);
setVentanaTamano(anchoVentana, altoVentana);
}
int getPosCursorX()
{
GetConsoleScreenBufferInfo(hCon, infoCon);
return infoCon->dwCursorPosition.X;
}
int getPosCursorY()
{
GetConsoleScreenBufferInfo(hCon, infoCon);
return infoCon->dwCursorPosition.Y;
}
}
Thanks in advance!
infoCon.