Search code examples
windowswinapivisual-c++win32comwin32-process

GetProcessIoCounters errors out with code 998


I am trying to get the PIO_COUNTERS for the current process by doing:

DWORD pid = GetCurrentProcessId();
auto handle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
PIO_COUNTERS ctr = nullptr;
if (!GetProcessIoCounters(handle, ctr)) {
  DWORD dw = GetLastError();
}

I get value of dw as 998 which is "Invalid access of the memory location". This essentially means that the handle I am using does not have enough privileges, but this is the flag with the max access control privileges. I also tried using the handle given by "GetCurrentProcess" (which is different from the one I got above) but that also gave error code 998 after passing it to GetProcessIoCounters.

Can somebody please help me out here?

Thanks in advance.


Solution

  • The 'invalid access' error is occurring because you are passing a nullptr value for the address of the IO_COUNTERS structure into which to write the information you are retrieving. You need to give the address of an actual structure for this:

    DWORD pid = GetCurrentProcessId();
    auto handle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
    IO_COUNTERS info;
    if (!GetProcessIoCounters(handle, &info)) { // Pass the address of your structure!
      DWORD dw = GetLastError();
    }
    

    You can then access the various members of the info structure to get the information you want.