Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using Win32 Console

Posted on 2001-06-14
8
Medium Priority
?
845 Views
Last Modified: 2012-06-27


I want to use the Win32 Console but when I call GetStdHandle, I get a handle that seems to be not valid.

The code should compile and show the following error after run:

Error: Could not receive buffer information.  --> The handle is invalid.


Did I forget any init step ... ?




#include <windows.h>
#include <iostream.h>

void error(string s) {
    LPVOID lpMsgBuf;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,
        SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,0);
    std::cout << "Error: " << s << "  --> " << (std::string)(LPCTSTR)lpMsgBuf;
};

HANDLE _V_consoleOutputHandle, _V_consoleInputHandle;

void main()
{
     CONSOLE_SCREEN_BUFFER_INFO __csbi;

     _V_consoleOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
     if (_V_consoleOutputHandle == INVALID_HANDLE_VALUE)
         error("Could not create output buffer handle.");
     _V_consoleInputHandle = GetStdHandle(STD_INPUT_HANDLE);
       if (_V_consoleInputHandle == INVALID_HANDLE_VALUE)
         error("Could not create input buffer handle.");

     if ( ! GetConsoleScreenBufferInfo(_V_consoleOutputHandle, &__csbi) )
         error("Could not receive buffer information.");

    // .......
}


0
Comment
Question by:bormuth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 3

Expert Comment

by:jlsjls
ID: 6190213
0
 
LVL 5

Accepted Solution

by:
proskig earned 150 total points
ID: 6190234
I have compiled your snippet on my machine and it worked. I had to change it to:

#include <windows.h>
#include <iostream>
#include <string>

void error(std::string s) {
   LPVOID lpMsgBuf;
   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
       FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL,
       SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0,0);
   std::cout << "Error: " << s << "  --> " << (std::string)(LPCTSTR)lpMsgBuf;
};


The rest was find. Try to debug and see actual value of _V_consoleOutputHandle. On my machine it was 7.
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6190239
Try to call GetConsoleScreenBufferInfo right after You get handle of output.
If it work it seems that your identifiers are to long for your compiler settings.
Make identifiers for handles shorter or set specific option to make them available to be longer then now.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bormuth
ID: 6190300


I used a shorter handle name nad even called GetConsoleScreenBufferInfo right after handle creation. The handle value is 0000044 but this still is NOT valid.

I use Borland C++ 5.5 and Windows 2000.

0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6190322
I made empty console application on Visual C with:

#include "stdafx.h"
#include <windows.h>
#include <iostream.h>

HANDLE _V_consoleOutputHandle, _V_consoleInputHandle;
void main()
{
   CONSOLE_SCREEN_BUFFER_INFO __csbi;
   int err=0;

   _V_consoleOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
   if (_V_consoleOutputHandle == INVALID_HANDLE_VALUE)
      err=1;
   _V_consoleInputHandle = GetStdHandle(STD_INPUT_HANDLE);
   if (_V_consoleInputHandle == INVALID_HANDLE_VALUE)
      err=1;
   if ( ! GetConsoleScreenBufferInfo(_V_consoleOutputHandle, &__csbi) )
      err=1;
   printf("%d", err);
   getchar();
}

and output is 0 (OK).
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6190326
May be You can try to change declaration for main function:

void main(void)

or

void main(int argc, char* argv[])
0
 

Author Comment

by:bormuth
ID: 6190347


OK

When I run the programm in a full screen 80*25 DOS window it works. But when I run it in a Console window (in my case 80*40) it doesn't.
0
 

Author Comment

by:bormuth
ID: 6190380

OK - it works


This code only runs on Win95 and following. My Console window was set to DOS mode ...

I gave th points to proskig because he gave me the idea that the code should work ..
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question