_get_osfhandle函数

标签: , , ,

_get_osfhandle函数可以获取文件描述符(file descriptor)对应的文件句柄(file handle)。

众所周知,系统中stdin、stdout、stderr的文件描述符分别是0、1、2,所以可以用_get_osfhandle函数来获取它们的句柄。

#include <io.h>
#include <tchar.h>
#include <windows.h>

int main()
{
    LPTSTR s = TEXT("https://demon.tw\n");
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    HANDLE _hStdOut = (HANDLE) _get_osfhandle(1);
    if (hStdOut == _hStdOut) {
        WriteConsole(hStdOut, s, lstrlen(s), NULL, NULL);
    }
    return 0;
}

可以看到,_get_osfhandle的返回值和GetStdHandle取得的标准输出句柄是一样的。

赞赏

微信赞赏支付宝赞赏

随机文章:

  1. 用VBS解析JSON格式数据之VbsJson类
  2. Chrome用 –proxy-server 设置代理服务器
  3. PHP没有ASP流行?
  4. VBS 获取外网 IP 并发送到指定邮箱
  5. PHP:小心urldecode引发的SQL注入漏洞

留下回复