[C++] CHAR_INFO.Char.AsciiChar

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

In an attempt to create some graphical functionality in the console I'm reading Ben Ryves' tutorial. I'm having some trouble with the code. For example, at one point in the tutorial the following program is given:
#include <stdlib.h>
#include <Windows.h>
#include <stdio.h>

HANDLE wHnd;    // Handle to write to the console.
HANDLE rHnd;    // Handle to read from the console.

int main(int argc, char* argv[]) {

// Set up the handles for reading/writing:
wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
rHnd = GetStdHandle(STD_INPUT_HANDLE);

// Change the window title:
SetConsoleTitle("Win32 Console Control Demo");

// Set up the required window size:
SMALL_RECT windowSize = {0, 0, 79, 49};

// Change the console window size:
SetConsoleWindowInfo(wHnd, TRUE, &windowSize);

// Create a COORD to hold the buffer size:
COORD bufferSize = {80, 50};

// Change the internal buffer size:
SetConsoleScreenBufferSize(wHnd, bufferSize);

// Set up the character:
CHAR_INFO letterA;
letterA.Char.AsciiChar = 'A';
letterA.Attributes =
FOREGROUND_RED
|FOREGROUND_INTENSITY
|BACKGROUND_RED
|BACKGROUND_GREEN
|BACKGROUND_INTENSITY;

// Set up the positions:
COORD charBufSize = {1,1};
COORD characterPos = {0,0};
SMALL_RECT writeArea = {0,0,0,0};

// Write the character:
WriteConsoleOutput(wHnd, &letterA, charBufSize, characterPos, &writeArea);

// Move down a line so that we don't overwrite the character:
printf("\n");

// Display a prompt for the user to press a key.
system("pause");

// Exit
return 0;
}

I'm having two problems with that code: SetConsoleTitle("Win32 Console Control Demo"); That line gives me the following error: error C2664: 'SetConsoleTitleW' : cannot convert parameter 1 from 'const char [27]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Actually, I fixed this one by replacing the line with SetConsoleTitle(TEXT("Win32 Console Control Demo")) I know it works, but really, this isn't how it's supposed to work, right? The second troublemaker is letterA.Char.AsciiChar = 'A' When I run the program I get an '?' instead of an 'A'. I tried some other characters, but the results are the same. I could use the UnicodeChar member instead of AsciiChar, which does work for most of the characters, but then I can't use the extended part of the ASCII-chart. So what's up with CHAR_INFO? Am I doing something wrong here? I'm using VS C++ 8.0, in the case that might have something to do with the problem.

Share on other sites
Actually, that is how it's supposed to work. The tutorial was written before VS 8.0 and so it assumes that by default you have UNICODE undefined. VS 8.0 changed this and by default defines UNICODE. You can either switch to using Unicode (you can use the extended ASCII characters if you use their UTF16 representations) or you can go into your project properties and switch your build character set to either No Character Set or Multibyte Character Set (I forget which).