C++ WinAPI - GetConsoleScreenBufferInfo always fails by invalid handle (Returns 0)












0














I'm writing a simple program to output the correct (and current) width and height of the console in chars/lines.



#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbiInfo);
cout << csbiInfo.dwSize.X;
cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
system("PAUSE");
}


However, while GetStdHandle doesn't return INVALID_HANDLE_VALUE, GetConsoleScreenBufferInfo fails (returning 0), with error code 0x6 (invalid handle)



I've also tried using GetConsoleWindow(); in place of GetStdHandle to no avail.



I'm using windows 10 and VS2017. I get the feeling I might be either in over my head, or pushing to do something too system-specific.



Thanks in advance for any solutions/alternatives.










share|improve this question


















  • 3




    What happens if you use STD_OUTPUT_HANDLE instead of the input handle?
    – 1201ProgramAlarm
    Nov 12 at 5:38






  • 1




    docs.microsoft.com/en-us/windows/console/console-handles
    – Jonathan Potter
    Nov 12 at 6:18






  • 3




    "Initially, STDIN is a handle to the console's input buffer, and STDOUT and STDERR are handles of the console's active screen buffer"
    – Jonathan Potter
    Nov 12 at 6:18






  • 2




    "Input" and "screen buffer" have no overlap. If you want to know, how many characters you can output to a line in the screen buffer, you're going to have to use an output handle.
    – IInspectable
    Nov 12 at 10:14
















0














I'm writing a simple program to output the correct (and current) width and height of the console in chars/lines.



#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbiInfo);
cout << csbiInfo.dwSize.X;
cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
system("PAUSE");
}


However, while GetStdHandle doesn't return INVALID_HANDLE_VALUE, GetConsoleScreenBufferInfo fails (returning 0), with error code 0x6 (invalid handle)



I've also tried using GetConsoleWindow(); in place of GetStdHandle to no avail.



I'm using windows 10 and VS2017. I get the feeling I might be either in over my head, or pushing to do something too system-specific.



Thanks in advance for any solutions/alternatives.










share|improve this question


















  • 3




    What happens if you use STD_OUTPUT_HANDLE instead of the input handle?
    – 1201ProgramAlarm
    Nov 12 at 5:38






  • 1




    docs.microsoft.com/en-us/windows/console/console-handles
    – Jonathan Potter
    Nov 12 at 6:18






  • 3




    "Initially, STDIN is a handle to the console's input buffer, and STDOUT and STDERR are handles of the console's active screen buffer"
    – Jonathan Potter
    Nov 12 at 6:18






  • 2




    "Input" and "screen buffer" have no overlap. If you want to know, how many characters you can output to a line in the screen buffer, you're going to have to use an output handle.
    – IInspectable
    Nov 12 at 10:14














0












0








0







I'm writing a simple program to output the correct (and current) width and height of the console in chars/lines.



#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbiInfo);
cout << csbiInfo.dwSize.X;
cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
system("PAUSE");
}


However, while GetStdHandle doesn't return INVALID_HANDLE_VALUE, GetConsoleScreenBufferInfo fails (returning 0), with error code 0x6 (invalid handle)



I've also tried using GetConsoleWindow(); in place of GetStdHandle to no avail.



I'm using windows 10 and VS2017. I get the feeling I might be either in over my head, or pushing to do something too system-specific.



Thanks in advance for any solutions/alternatives.










share|improve this question













I'm writing a simple program to output the correct (and current) width and height of the console in chars/lines.



#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_INPUT_HANDLE), &csbiInfo);
cout << csbiInfo.dwSize.X;
cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
system("PAUSE");
}


However, while GetStdHandle doesn't return INVALID_HANDLE_VALUE, GetConsoleScreenBufferInfo fails (returning 0), with error code 0x6 (invalid handle)



I've also tried using GetConsoleWindow(); in place of GetStdHandle to no avail.



I'm using windows 10 and VS2017. I get the feeling I might be either in over my head, or pushing to do something too system-specific.



Thanks in advance for any solutions/alternatives.







c++ windows winapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 5:31









Pseudo Nym

174




174








  • 3




    What happens if you use STD_OUTPUT_HANDLE instead of the input handle?
    – 1201ProgramAlarm
    Nov 12 at 5:38






  • 1




    docs.microsoft.com/en-us/windows/console/console-handles
    – Jonathan Potter
    Nov 12 at 6:18






  • 3




    "Initially, STDIN is a handle to the console's input buffer, and STDOUT and STDERR are handles of the console's active screen buffer"
    – Jonathan Potter
    Nov 12 at 6:18






  • 2




    "Input" and "screen buffer" have no overlap. If you want to know, how many characters you can output to a line in the screen buffer, you're going to have to use an output handle.
    – IInspectable
    Nov 12 at 10:14














  • 3




    What happens if you use STD_OUTPUT_HANDLE instead of the input handle?
    – 1201ProgramAlarm
    Nov 12 at 5:38






  • 1




    docs.microsoft.com/en-us/windows/console/console-handles
    – Jonathan Potter
    Nov 12 at 6:18






  • 3




    "Initially, STDIN is a handle to the console's input buffer, and STDOUT and STDERR are handles of the console's active screen buffer"
    – Jonathan Potter
    Nov 12 at 6:18






  • 2




    "Input" and "screen buffer" have no overlap. If you want to know, how many characters you can output to a line in the screen buffer, you're going to have to use an output handle.
    – IInspectable
    Nov 12 at 10:14








3




3




What happens if you use STD_OUTPUT_HANDLE instead of the input handle?
– 1201ProgramAlarm
Nov 12 at 5:38




What happens if you use STD_OUTPUT_HANDLE instead of the input handle?
– 1201ProgramAlarm
Nov 12 at 5:38




1




1




docs.microsoft.com/en-us/windows/console/console-handles
– Jonathan Potter
Nov 12 at 6:18




docs.microsoft.com/en-us/windows/console/console-handles
– Jonathan Potter
Nov 12 at 6:18




3




3




"Initially, STDIN is a handle to the console's input buffer, and STDOUT and STDERR are handles of the console's active screen buffer"
– Jonathan Potter
Nov 12 at 6:18




"Initially, STDIN is a handle to the console's input buffer, and STDOUT and STDERR are handles of the console's active screen buffer"
– Jonathan Potter
Nov 12 at 6:18




2




2




"Input" and "screen buffer" have no overlap. If you want to know, how many characters you can output to a line in the screen buffer, you're going to have to use an output handle.
– IInspectable
Nov 12 at 10:14




"Input" and "screen buffer" have no overlap. If you want to know, how many characters you can output to a line in the screen buffer, you're going to have to use an output handle.
– IInspectable
Nov 12 at 10:14












1 Answer
1






active

oldest

votes


















1














STD_INPUT_HANDLE is usually connected to the terminal keyboard.
STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are usually used as console.So let's say the STD_INPUT_HANDLE Can't be identified as a "console handle" by "GetConsoleScreenBufferInfo()". You can use "STD_OUTPUT_HANDLE" to get the console handle.



  #include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
cout << csbiInfo.dwSize.X;
cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
system("PAUSE");
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256389%2fc-winapi-getconsolescreenbufferinfo-always-fails-by-invalid-handle-returns%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    STD_INPUT_HANDLE is usually connected to the terminal keyboard.
    STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are usually used as console.So let's say the STD_INPUT_HANDLE Can't be identified as a "console handle" by "GetConsoleScreenBufferInfo()". You can use "STD_OUTPUT_HANDLE" to get the console handle.



      #include <iostream>
    #include <Windows.h>
    using namespace std;

    int main()
    {
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
    cout << csbiInfo.dwSize.X;
    cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
    system("PAUSE");
    }





    share|improve this answer


























      1














      STD_INPUT_HANDLE is usually connected to the terminal keyboard.
      STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are usually used as console.So let's say the STD_INPUT_HANDLE Can't be identified as a "console handle" by "GetConsoleScreenBufferInfo()". You can use "STD_OUTPUT_HANDLE" to get the console handle.



        #include <iostream>
      #include <Windows.h>
      using namespace std;

      int main()
      {
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
      GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
      cout << csbiInfo.dwSize.X;
      cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
      system("PAUSE");
      }





      share|improve this answer
























        1












        1








        1






        STD_INPUT_HANDLE is usually connected to the terminal keyboard.
        STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are usually used as console.So let's say the STD_INPUT_HANDLE Can't be identified as a "console handle" by "GetConsoleScreenBufferInfo()". You can use "STD_OUTPUT_HANDLE" to get the console handle.



          #include <iostream>
        #include <Windows.h>
        using namespace std;

        int main()
        {
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
        cout << csbiInfo.dwSize.X;
        cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
        system("PAUSE");
        }





        share|improve this answer












        STD_INPUT_HANDLE is usually connected to the terminal keyboard.
        STD_OUTPUT_HANDLE and STD_ERROR_HANDLE are usually used as console.So let's say the STD_INPUT_HANDLE Can't be identified as a "console handle" by "GetConsoleScreenBufferInfo()". You can use "STD_OUTPUT_HANDLE" to get the console handle.



          #include <iostream>
        #include <Windows.h>
        using namespace std;

        int main()
        {
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbiInfo);
        cout << csbiInfo.dwSize.X;
        cout << csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top;
        system("PAUSE");
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 8:22









        Drake Wu

        1274




        1274






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256389%2fc-winapi-getconsolescreenbufferinfo-always-fails-by-invalid-handle-returns%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values