cwindowsdrivervisual-studio-2019kernel-mode

A variable of type HANDLE can be compared with NULL in C?


A variable declared as HANDLE can be compared with NULL in C? Thank you.

Edition:

For example:

HANDLE hProcess = NULL;

status = ZwOpenProcess(&hProcess, PROCESS_DUP_HANDLE, &ob, &Cid);
if (hProcess != NULL)
{
  ZwClose(hProcess);
  hProcess = NULL;
}

The goal is check if hProcess is != 0. Then if i'm checking != NULL, means the same thing?


Solution

  • (Too long for a comment.)

    The goal is check if hProcess is != 0.

    You can check that with if(hProcess != NULL) { /*...*/ } as explained in the other answers.

    However, in the given example what must be checked is the return value of the API call, instead.

      HANDLE hProcess;
      if(NT_SUCCESS(ZwOpenProcess(&hProcess, PROCESS_DUP_HANDLE, &ob, &Cid)))
      {
        /*... use hProcess ...*/
    
        ZwClose(hProcess);
      }
      else
      {
        /*... handle error ...*/
      }