简体   繁体   English

将HANDLE转换为文件描述符

[英]Convert a HANDLE to File Descriptor

I have a HANDLE like that: 我有一个类似的句柄:

HANDLE hPipe = CreateNamedPipe(...)

Is possible to convert the hPipe to a File Descriptor (int)? 可以将hPipe转换为文件描述 (INT)?

I need this because I have implemented in C++ a code to work with TLS using the OpenSSL. 我需要这样做,因为我已经在C ++中实现了使用OpenSSL使用TLS的代码。 This is working like a charm with TCP sockets, but I really need use it over Namedpipe. 这与TCP套接字的工作原理一样,但是我确实需要在Namedpipe上使用它。

The OpenSSL's function SSL_set_fd(SSL *ssl, int fd) accepts just FileDescriptor that is a int , and not a HANDLE . OpenSSL的函数SSL_set_fd(SSL * ssl,int fd)仅接受作为int的 FileDescriptor ,而不接受HANDLE

Note: I also tried use the below function, but doesn't worked (return 3): 注意:我也尝试使用以下功能,但不起作用(返回3):

int fd = _open_osfhandle(reinterpret_cast<intptr_t>(hPipe), 0);    

The 3 you're getting back isn't an error, it's the file descriptor handle. 您要返回的3不是错误,它是文件描述符句柄。 If you look at the documentation you'll see that it returns -1 for failure, otherwise the return value is a file descriptor. 如果查看文档,您会发现它为失败返回-1 ,否则返回值为文件描述符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM