Retrieve the number of opened file descriptors using the Windows API

与世无争的帅哥 提交于 2019-12-12 08:59:25

问题


I would like to know how many file descriptors have I opened in my C++ application. Can this be done using Windows API function?


回答1:


You can ask each handle in the process using GetFileType.

      DWORD type_char = 0, 
      type_disk = 0, 
      type_pipe = 0, 
      type_remote = 0, 
      type_unknown = 0,
      handles_count = 0;

GetProcessHandleCount(GetCurrentProcess(), &handles_count);
handles_count *= 4;
for (DWORD handle = 0x4; handle < handles_count; handle += 4) {
    switch (GetFileType((HANDLE)handle)){
        case FILE_TYPE_CHAR:
            type_char++;
            break;
        case FILE_TYPE_DISK:
            type_disk++;
            break;
        case FILE_TYPE_PIPE: 
            type_pipe++;
            break;
        case FILE_TYPE_REMOTE: 
            type_remote++;
            break;
        case FILE_TYPE_UNKNOWN:
            if (GetLastError() == NO_ERROR) type_unknown++;
            break;

    }

}



回答2:


If you are after checking open handles then you can the Handle utility from SysInternals.



来源:https://stackoverflow.com/questions/15357961/retrieve-the-number-of-opened-file-descriptors-using-the-windows-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!