title: How to get the Handle Id from Process, HWND programmatically?
body: <p>I try to get Handle ID of opened applications (windows).</p>
  
  <p>I run <code>Window detective</code> program, (like spy++) to verify that I get proper values.</p>
  
  <p>For testing I try to get only one Handle Id pointed by red arrow (see the image):</p>
  
  <p><img src="https://i.stack.imgur.com/tM2vB.png" alt="enter image description here"></p>
  
  <p>So I have program that gives me process id and thread Id but not 1st Child Handle ID.</p>
  
  <p>In my case I took <code>calc.exe</code>, but actually I need to do that for all <code>exe</code> applications:</p>
  
  <p><strong>readWindow.c</strong></p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stddef.h&gt;
  #include &lt;inttypes.h&gt;
  #include &lt;tchar.h&gt; 
  #include &lt;psapi.h&gt; 
  
  HMODULE getModulePid(DWORD processID, char* searchStr){ // gets the module by the module name from an explicit process
  
     HANDLE hProcess;
     HMODULE hMods[1024];
     TCHAR szModName[MAX_PATH];
     DWORD cbNeeded;
  
     if(hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ))
     {
      if(EnumProcessModules(hProcess, hMods, sizeof(hMods), &amp;cbNeeded))
      {
      unsigned int k;
      for(k = 0; k &lt; (cbNeeded / sizeof(HMODULE)); ++k )
      {
          if (GetModuleFileNameEx(hProcess, hMods[k], szModName,  sizeof(szModName)/sizeof(TCHAR)))
          {
  
          //printf( "fess pid: %u modname: %s\n", processID, szModName );
  
          if(strstr(szModName, searchStr))
          {
              printf( "pid: &amp;#37;u modname: %s\n", processID, szModName );
              CloseHandle( hProcess );
              return hMods[k];
          }
         }
      }//for
    }     
  }  
      CloseHandle( hProcess );
      return NULL;
  }
  
  HMODULE getModule(char* searchStr){ // gets the module by the modul name from all processes
     DWORD aProcesses[1024], cbNeeded, cProcesses;
  
      if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &amp;cbNeeded ) ) return NULL;
      cProcesses = cbNeeded / sizeof(DWORD);
  
      HMODULE hmodule;
      unsigned int i;
      for (i = 0; i &lt; cProcesses; ++i )
      {
          if(hmodule = getModulePid(aProcesses[i], searchStr))
           {
           return hmodule;
            }
           }
      return NULL;
  }
  
  
  HMODULE getModuleHwnd(HWND hwnd){ // gets the module from a window
     DWORD pid;
     DWORD tid = GetWindowThreadProcessId(hwnd, &amp;pid ); // !!??!!
     printf( "hwnd tid: %u\n", tid  );
      printf( "hwnd pid: %u\n", pid  );
     return getModulePid(pid, ".exe");
  }
  
  HMODULE hModuleT;
  char* searchStrT;
  
  BOOL CALLBACK shownWindow(HWND hwnd, LPARAM lParam){ // EnumWindows callback
     if(hModuleT) return TRUE;
  
      char pcWinTitle[256];
  
      if(GetWindow(hwnd, GW_OWNER)) return TRUE; // whats that?
      GetWindowText(hwnd, pcWinTitle, 1024);
      if(strstr(pcWinTitle, searchStrT)){  
          printf( "wndtitle: %s\n", pcWinTitle);                                      
          hModuleT = getModuleHwnd(hwnd);
      }
  
      return TRUE;
  }
  
  HMODULE getModuleByWndTitle(char* searchStr){ // gets the module from a window title
      searchStrT = searchStr;
      EnumWindows(shownWindow, 0);
      return hModuleT;
  }
  
  
  int main()
  {
  
      //EnumWindows(EnumWindowsProc, 0);
  
      printf("find by name ... \n");
       getModule("calc.exe");
       printf("\nfind by title ... \n");
     getModuleByWndTitle("Calculator");
  
      printf("Done");
  
  
      return 0;
  } 
  </code></pre>
  
  <p>Run from <code>minGW</code>:</p>
  
  <blockquote>
    <p>$ gcc -L/local/lib -I/local/include -o readWindow readWindow.c -lpsapi</p>
  </blockquote>
  
  <p>Output:</p>
  
  <pre><code>find by title ...
  wndtitle: Calculator
  hwnd tid: 33364
  hwnd pid: 25440
  Done
  </code></pre>
  
  <p><strong>How can I get the Handle from Process?</strong></p>
  
  <p>I'm sure it should be some 1-2 rows of code.</p>
  
  <pre><code>DWORD dwValue .....
  
  printf("The value in hexa: 0X%.8X(%d).\n", dwValue);
  </code></pre>
  
  <p>it should be <code>0x007B137C</code></p>
  
  <p>From Spy++ I need this value, red arrow:</p>
  
  <p><img src="https://i.stack.imgur.com/ZDaEz.png" alt="enter image description here"></p>