_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 For Next循环的陷阱
  2. VbsEdit正版序列号
  3. 在线VBScript代码格式化工具VbsBeautifier
  4. VB6拾遗:调用函数指针
  5. Raspberry Pi树莓派新系统SSH连接被拒绝的解决方法

留下回复