-->

Retrieve the number of opened file descriptors usi

2020-07-26 14:13发布

问题:

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.