title: Buffer Allocation With GetModuleFileNameEx
body: <p>I am currently trying to get the full filepath from my executable by providing a process ID, and this works &quot;fine&quot; in the since that its able to return some of the information needed, however for some reason it becomes corrupted and this can be seen when trying to return it as a plain string: C and if you iterate over each char and print each letter you get even more of a mess. Where is my current coding wrong when attempting allocate my buffer?</p>
  <pre><code>std::string User::getFullPath() {
      HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, GetCurrentProcessId());
      char buffer[MAX_PATH];
      if (hProcess != NULL)
      {
          GetModuleFileNameEx(hProcess, NULL, (LPWSTR)buffer, MAX_PATH);
          CloseHandle(hProcess);
      }
      for (char i : buffer) {
          std::cout &lt;&lt; i&lt;&lt;std::endl;
      }
      return buffer;
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/RZKjR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RZKjR.png" alt="enter image description here" /></a></p>
  <p>How do I properly allocate the memory in this case to prevent corruption</p>