gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:01:44 28 4
gpt4 key购买 nike

以下代码在尝试执行最后一行时出错

 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, 0, 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 的文档进一步指出,CreateFile 在创建控制台缓冲区句柄时会忽略文件标志。

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

<小时/>

请注意,在 Linux 上,到标准 IO 句柄的异步 IO 仅在某些情况下才可能 - 取决于重定向的输入/输出:Strange exception throw - assign: Operation not permitted

关于windows-7 - win7 boost::asio::windows::stream_handle 构造函数抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258190/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com