_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. eXeScope注册码算法
  2. 批处理技术内幕:重定向与管道
  3. 用C语言实现PHP的addslashes函数
  4. VB6.0中的“取消 Pentium(tm) FDIV 安全性检查”
  5. 可以用CreateObject创建的WMI对象

留下回复