_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. 在Windows下编译zlib 1.25
  2. jQuery图片延迟加载插件——Lazy Load
  3. jQuery ajax返回parsererror错误的解决方法
  4. C++ TR1正则表达式库
  5. VBS图片处理之用VBS缩放图片大小

留下回复