#Console IO with WinAPI

23 messages · Page 1 of 1 (latest)

small patrol
#

Hey there!

I've started to learn C and i'd like to create a little Game in the Console.
The Goal is to have a Frontbuffer and a Backbuffer which can be swapped, so i can build the hole Screen at once instead of messing with a bunch of fprintf or similar calls.

I kinda figured out how to do the swapping of Buffers (although there may be problems i'm not aware of), but what's giving me a headache is receiving Input!

I've read the Microsoft Documentation for a bunch of stuff, i've searched on StackOverflow, i tried asking ChatGPT, but i am seriously stuck. Depending on what i try, there's 3 different outcomes, being

  • the Program crashes silently when reaching the relevant portion of the Code
  • the Program continues to run, but the assertion fails due to an Invalid Handle
  • the Program runs up to that point and allows for Input, but terminates after a single keypress

what i actually want to happen is what functions like fgets or fscanf provide (which i also tried, both, with the same amount of success), basically blocking the Thread, allowing the User to enter some text and continue after Enter is pressed.

Code: https://pastebin.com/HnMHaWs1
The Input 'Logic' is between Lines 60 and 70, any insights are highly appreciated (and feel free to roast my code for everything that's wrong), i've been literally trying to get this working for 2 hours 🙂

(Current Behaviour:

  • first Buffer shows
  • sleeps 1 second
  • second Buffer shows
  • sleeps 1 second
  • Program either crashes silently or assertion fails.

'Expected' Behaviour:

  • first Buffer shows
  • sleeps 1 second
  • second Buffer shows
  • sleeps 1 second
  • User can enter Input
  • Program terminates on Enter)
drowsy needleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

misty wing
#

bufferContent[index].Attributes = 7 | 0;
Any expression x | 0 will evaluate to x btw

#

assert(ReadConsole(consoleInput, _readBuffer, _readBufferSize, &readCharsCount, NULL) == TRUE);
Never put code with side effects inside an assert as asserts can (and usually will) be disabled for release builds, i.e. if your code has statements with side effects in asserts then your code can (and likely will) behave different depending on if you compile for a debugging or a release build.

#

And this isn't the only scenario where you did this, but there are several of these assert mistakes throughout your code

#

Also a check like so:

(consoleFrontbuffer == INVALID_HANDLE_VALUE) == 0
is kinda hard to read. Why not just use inequality like so:
consoleFrontbuffer != INVALID_HANDLE_VALUE

#

Now I don't know shit about C on Windows, but these are the things I noticed.
Maybe try getting a debugger and stepping through the lines to see where exactly it crashes if you didn't do so already.

small patrol
small patrol
small patrol
#

i did not try using a Debugger yet, will try that! tysm

drowsy needleBOT
#

@small patrol Has your question been resolved? If so, type !solved :)

small patrol
#

No, it hasn't been solved yet!
I'll try to fix it and/or write the minimal Code needed to reproduce the problem and make sure to use that Command on success 🙂 (i know it's a bot, just wanted to mention it anyways)

misty wing
#

;compile

#include <stdio.h>
#include <assert.h>

int main() {
    assert(puts("Hi mom"));
}
crisp nymphBOT
#
Program Output
Hi mom
misty wing
#

;compile -DNDEBUG

#include <stdio.h>
#include <assert.h>

int main() {
    assert(puts("Hi mom"));
}
crisp nymphBOT
#
Compilation successful

No output.

misty wing
#

As you can see: With asserts disabled, the puts never gets called

#

This means that the code inside the assert doesn't exist anymore

small patrol
#

O_O

Thank you so much for the instant clarification!!

#

i think the problem is just wrong use of the handles and a bunch of skill issues, i'll figure it out

EDIT:
the function CreateConsoleScreenBuffer receives a dwShareMode, and that one needs to be FILE_SHARE_READ | FILE_SHARE_WRITE to make ReadConsole work. https://pastebin.com/TSs2uesa

#

!solved