_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("http://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实现PHP的basename函数
  2. VBS获取文件的SDDL字符串
  3. Switch 无法启动软件。 请在HOME菜单中再试一次。
  4. C/C++ void main()
  5. VBS监控网络流量

留下回复