title: Using Win32, GetModuleBaseName() and GetModuleFileNameEx() fail with GetLastError() = 6 when using valid handle
body: <p>First some simple code snippet:</p>
  
  <pre><code>m_hProcessHandle = ::OpenProcess((PROCESS_QUERY_INFORMATION | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION), FALSE, m_dwProcessIdentifier);
  if (NULL != m_hProcessHandle)
  {
      if (FALSE != ::OpenProcessToken(m_hProcessHandle, (TOKEN_QUERY | TOKEN_IMPERSONATE | TOKEN_DUPLICATE), &amp;m_hImpersonizationToken))
      {
          wchar_t wszFullExecutableFileName[MAX_PATH];
          if (0 == ::GetModuleBaseName(m_hProcessHandle, NULL, wszFullExecutableFileName, (sizeof(wszFullExecutableFileName)/sizeof(wchar_t))))
          {
              __DebugMessage(L"GetModuleBaseName() failed with GetLastError() = %d", ::GetLastError());
          }
          else
          {
              if (0 == ::GetModuleFileNameEx(m_hProcessHandle, NULL, wszFullExecutableFileName, (sizeof(wszFullExecutableFileName)/sizeof(wchar_t))))
              {
                  __DebugMessage(L"GetModuleFileNameEx() failed with GetLastError() = %d", ::GetLastError());
              }
              else
              {
                  m_strFullFileName = wszFullExecutableFileName;
              }
          }
      }
  }
  </code></pre>
  
  <p>The <a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess" rel="nofollow noreferrer">OpenProcess()</a> returns a valid handle as does the <a href="https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocesstoken" rel="nofollow noreferrer">OpenProcessToken()</a>, but when I call the subsequent <a href="https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulebasenamea" rel="nofollow noreferrer">GetModuleBaseName()</a> and <a href="https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulefilenameexa" rel="nofollow noreferrer">GetModuleFileNameEx()</a> functions, I get <a href="https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-getlasterror" rel="nofollow noreferrer">GetLastError()</a> = 6 (The handle is invalid). I am running that code as admin on Windows 7. What gives?</p>
  
  <p>cheers,
  GumbyTheBorg</p>