Thread: wrong pid with GetWindowThreadProcessId?

  1. #1
    Registered User
    Join Date
    Nov 2008

    wrong pid with GetWindowThreadProcessId?

    hi i am using the following functions to get a module handler by a name or by a window title:
    HMODULE getModule(DWORD processID, char* searchStr){ // gets the module by the modul 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), &cbNeeded))
        for(unsigned int k = 0; k < (cbNeeded / sizeof(HMODULE)); ++k )
        	if (GetModuleFileNameEx(hProcess, hMods[k], szModName,  sizeof(szModName)/sizeof(TCHAR)))
        	if(strstr(szModName, searchStr)){
                printf( "pid: &#37;u modname: %s\n", processID, szModName );
                CloseHandle( hProcess );
                return hMods[k];
        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), &cbNeeded ) ) return NULL;
        cProcesses = cbNeeded / sizeof(DWORD);
        HMODULE hmodule;
        for (unsigned int i = 0; i < cProcesses; ++i )
        	if(hmodule = getModule(aProcesses[i], searchStr)) return hmodule;
        return NULL;
    HMODULE getModule(HWND hwnd){ // gets the module from a window
       DWORD pid = GetWindowThreadProcessId(hwnd, NULL ); // !!??!!
       printf( "hwnd pid: %u\n", pid  );
       return getModule(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 = getModule(hwnd);
        return true;
    HMODULE getModuleByWndTitle(char* searchStr){ // gets the module from a window title
        searchStrT = searchStr;
        EnumWindows(shownWindow, 0);
        return hModuleT;
    but if i run:
    printf("find by name ... \n");
    printf("\nfind by title ... \n");
    i get this output:
    find by name ...
    pid: 3564 modname: d:\programme\proton\proton.exe

    find by title ...
    wndtitle: Proton
    hwnd pid: 3568
    what does GetWindowThreadProcessId return? is that not the correct process id?
    Last edited by electrohippi; 11-11-2008 at 08:41 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    The return value is a thread id. Try this for the process id.
        DWORD pid;
        DWORD tid = GetWindowThreadProcessId( hwnd, &pid);

  3. #3
    Registered User
    Join Date
    Nov 2008
    it works. thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong cast (atof)
    By BianConiglio in forum C Programming
    Replies: 13
    Last Post: 05-24-2004, 03:19 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM