windows-7 – win7 boost :: asio :: windows :: stream_handle构造函数抛出错误

前端之家收集整理的这篇文章主要介绍了windows-7 – win7 boost :: asio :: windows :: stream_handle构造函数抛出错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试执行最后一行时,以下代码出错

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

错误是:

uncaught exception of type N5boost16exception_detail10clone_implINS0_19error_info_injectorINS_6system12system_errorEEEEE
- assign: The parameter is incorrect

感谢您的意见.

@sehe

我试过了

hstdhandle = GetStdHandle(STD_OUTPUT_HANDLE);

并得到了同样的错误

所以我试过了

HANDLE handle= 
CreateFile(
    "CONIN$",GENERIC_READ | GENERIC_WRITE,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
boost::asio::windows::stream_handle inputStream(*ioServicePtr,handle);

错误

-assign handle invalid

解决方法

您可以使用GetStdHandle,因此:

HANDLE isfhandle = GetStdHandle(STD_INPUT_HANDLE);

但是,我不认为控制台支持Windows中的异步IO:

>句柄必须是支持重叠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

但是,CreateFile的文档进一步说CreateCile在创建控制台缓冲区的句柄时会忽略文件标志.

因此,您需要模拟stdin / stdout异步IO.

请注意,在Linux上,只能在某些情况下使用标准IO句柄的异步IO – 具体取决于重定向的输入/输出Strange exception throw – assign: Operation not permitted

猜你在找的Windows相关文章