title: Finding PID and Base Address in C++
body: <p>I'm trying to write a code in C++ which would display a process ID and base address of some application, PID seems to look correct but base address is 0, which is incorrect. My question is - what's wrong here?</p>
  
  <p>IDE: Code::Blocks</p>
  
  <p>Also to make it even run i had to set up "-lpsapi" in 
  Settings -> Compiler -> Linker settings -> Other linker options</p>
  
  <p>I was thinking about building this as admin but could't find such option in Code::Block (maybe build-in?)</p>
  
  <p><a href="https://i.stack.imgur.com/qJ04Z.png" rel="nofollow noreferrer">Console output</a></p>
  
  <p><a href="https://i.stack.imgur.com/mbjPF.png" rel="nofollow noreferrer">Build messages</a></p>
  
  <pre><code>#define WINVER 0x0501
  #include &lt;iostream&gt;
  #include &lt;cstdio&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;Psapi.h&gt;
  #include &lt;tlhelp32.h&gt;
  
  using namespace std;
  
  HANDLE GetHandle()
  {
      string name = "PathOfExile_x64.exe";
      DWORD pid = 0;
  
      HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      PROCESSENTRY32 process;
      ZeroMemory(&amp;process, sizeof(process));
      process.dwSize = sizeof(process);
  
      if (Process32First(snapshot, &amp;process))
      {
          do
          {
              if (string(process.szExeFile) == name)
              {
                 pid = process.th32ProcessID;
                 break;
              }
          } while (Process32Next(snapshot, &amp;process));
      }
  
      CloseHandle(snapshot);
  
      if (pid != 0)
      {
          cout &lt;&lt; "pid = " &lt;&lt; pid &lt;&lt; endl;
          return OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
      }
  
  
      return NULL;
  }
  
  HMODULE GetModule()
  {
      HMODULE hMods[1024];
      HANDLE pHandle = GetHandle();
      DWORD cbNeeded;
      unsigned int i;
  
      if (EnumProcessModules(pHandle, hMods, sizeof(hMods), &amp;cbNeeded))
          {
          for (i = 0; i &lt; (cbNeeded / sizeof(HMODULE)); i++)
          {
              TCHAR szModName[MAX_PATH];
              if (GetModuleFileNameEx(pHandle, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR)))
              {
                  string wstrModName = szModName;
  
                  string wstrModContain = "PathOfExile_x64.exe";
                  if (wstrModName.find(wstrModContain) != string::npos)
                  {
                      CloseHandle(pHandle);
                      return hMods[i];
                  }
              }
          }
      }
      return nullptr;
  }
  
  
  int main()
  {
      HWND WindowHandle = FindWindow(nullptr, "Path of Exile");
      DWORD PID;
      GetWindowThreadProcessId(WindowHandle, &amp;PID);
      PVOID hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, 0, PID);
  
      HMODULE Module = GetModule();
      DWORD BaseAddress = (DWORD)Module;
  
      cout &lt;&lt; BaseAddress &lt;&lt; endl;
  
  }
  </code></pre>