Kevin Dong
Kevin Dong

Reputation: 5349

Windows API doesn't work in GetStdHandle()

I renew my code, but the cursor was still visible. Help me, Thanks. (I am a newbie in C and WinAPI)

#define WINVER 0x0500
#include<windows.h>

int main() 
{
 //Get a STD OUTPUT handle
 HWND StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);

 //Set cursor invisible
 CONSOLE_CURSOR_INFO CURSOR;
 CURSOR.bVisible = FALSE;
 SetConsoleCursorInfo(StdHandle, &CURSOR);
 system ("pause>nul");
 return 0;
}

Upvotes: 0

Views: 269

Answers (1)

simonc
simonc

Reputation: 42165

You need to initialise the dwSize member of CONSOLE_CURSOR_INFO:

CONSOLE_CURSOR_INFO CURSOR;
CURSOR.dwSize = 1;
CURSOR.bVisible = FALSE;

From the docs for SetConsoleCursorInfo:

The dwSize member of the CONSOLE_CURSOR_INFO structure specifies the percentage of a character cell that is filled by the cursor. If this member is less than 1 or greater than 100, SetConsoleCursorInfo fails.

Note also from the same page

Return value

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Checking the return value would have helped you figure out what was going wrong.

Upvotes: 2

Related Questions