尝试执行最后一行时,以下代码出错
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 theFILE_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