title: ERROR_INVALID_HANDLE when calling GetModuleFileNameEx after CreateProcess
body: <p>After a successful call to CreateProcess, I am trying to get the path of the created process using GetModuleFileNameEx (lpApplicationName and lpCommandLine parameters can vary or be null so they aren't reliable in this case).
  The problem is that GetModuleFileNameEx fails with error 6 (ERROR_INVALID_HANDLE), leaving its buffer with invalid data. I cannot understand the reason, since CreateProcess succeeds and process handle should have been saved correctly in pi.hProcess.</p>
  
  <p>Hope you can shed some light, thanks in advance!</p>
  
  <p><strong>EDIT:</strong> An update: I noticed that removing the CREATE_SUSPENDED removes this problem too, but I need that flag set. How can I do?</p>
  
  <pre><code>// Defining GetModuleFileNameExA function
  typedef DWORD (WINAPI *fGetModuleFileNameExA)
  (
      HANDLE hProcess,
      HMODULE hModule,
      LPSTR lpFilename,
      DWORD nSize
  );
  //Load dinamically DLL function on program startup:
  fGetModuleFileNameExA _GetModuleFileNameExA = (fGetModuleFileNameExA) GetProcAddress( LoadLibraryA("Psapi.dll"), "GetModuleFileNameExA");
  
  // **** OTHER UNRELATED CODE HERE ****
  
  
  PROCESS_INFORMATION pi;
  
  //This call succeeds
  if (!CreateProcessW( ApplicationName, 
                      CommandLine, 
                      NewProcess.lpProcessAttributes, 
                      NewProcess.lpThreadAttributes,
                      NewProcess.bInheritHandles,
                      CREATE_SUSPENDED | CREATE_NEW_CONSOLE,
                      NULL,
                      CurrentDirectory,
                      &amp;NewProcess.bufStartupInfo,
                      &amp;pi)
         ) MessageBoxA(0, "Error creating process", "", 0);
  
      char ProcessPath[MAX_PATH];
  
  //Problem here: call fails with error 6
  if (!_GetModuleFileNameExA(pi.hProcess, NULL, ProcessPath, MAX_PATH)) {GetLastError();}
  
  //Invalid data is displayed
  MessageBoxA(0, ProcessPath, "GetModuleFileNameEx",0);
  </code></pre>