win7 boost::asio::windows::stream_handle constructor throws error

boost-asio, windows-7

Solution

You might use `GetStdHandle`, so:

HANDLE isfhandle = GetStdHandle(STD_INPUT_HANDLE);

However, I don't think consoles support asynchronous IO in windows:

The handle must be to an object that supports overlapped I/O.

If a handle is provided, it has to have been opened for overlapped I/O completion. For example, you must specify the `FILE_FLAG_OVERLAPPED` flag when using the CreateFile function to obtain the handle

But further the docs for CreateFile say that CreateFile ignores file flags when creating a handle to a console buffer.

So, you will need to emulate stdin/stdout async IO.

Note that on Linux, asynchronous IO to the standard IO handles is only possible in certain situations anyway - depending on the input/output being redirected: Strange exception throw - assign: Operation not permitted

Problem

The following code gets an error when trying to execute the last line ``` boost::shared_ptr<boost::asio::io_service> ioServicePtr(new boost::asio::io_service()); //setup the terminal with stdin and stdout int inFD = ::dup(STDIN_FILENO); int outFD = ::dup(STDOUT_FILENO); HANDLE osfhandle = (HANDLE)_get_osfhandle(inFD);//osfhandle is valid boost::asio::windows::stream_handle inputStream(*ioServicePtr, osfhandle); //error ``` the error is: ``` uncaught exception of type N5boost16exception_detail10clone_implINS0_19error_info_injectorINS_6system12system_errorEEEEE - assign: The parameter is incorrect ``` Appreciate your input. @sehe I tried ``` hstdhandle = GetStdHandle(STD_OUTPUT_HANDLE); ``` and got the same error So then I tried ``` HANDLE handle= CreateFile( "CONIN$", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); boost::asio::windows::stream_handle inputStream(*ioServicePtr, handle); ``` and the error was ``` -assign handle invalid ```

Original source

Related problems