문제

I try to get Handle ID of opened applications (windows).

I run Window detective program, (like spy++) to verify that I get proper values.

For testing I try to get only one Handle Id pointed by red arrow (see the image):

enter image description here

So I have program that gives me process id and thread Id but not 1st Child Handle ID.

In my case I took calc.exe, but actually I need to do that for all exe applications:

readWindow.c

#include <windows.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <tchar.h> 
#include <psapi.h> 

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), &cbNeeded))
    {
    unsigned int k;
    for(k = 0; k < (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: &#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), &cbNeeded ) ) return NULL;
    cProcesses = cbNeeded / sizeof(DWORD);

    HMODULE hmodule;
    unsigned int i;
    for (i = 0; i < 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, &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;
} 

Run from minGW:

$ gcc -L/local/lib -I/local/include -o readWindow readWindow.c -lpsapi

Output:

find by title ...
wndtitle: Calculator
hwnd tid: 33364
hwnd pid: 25440
Done

How can I get the Handle from Process?

I'm sure it should be some 1-2 rows of code.

DWORD dwValue .....

printf("The value in hexa: 0X%.8X(%d).\n", dwValue);

it should be 0x007B137C

From Spy++ I need this value, red arrow:

enter image description here

도움이 되었습니까?

해결책

It was pretty easy but for me tricky a bit.

I just need to print pointer HWND hwnd with %p.

So I added to my code:

char szBuff[512];
sprintf(szBuff, "%p", hwnd);

printf( "Found .... hWnd: %s\n", szBuff); 

And got what I need:

Found .... hWnd: 007B137C

[EDIT]

Working code example:

readWindow.c

#include <windows.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <tchar.h> 
#include <psapi.h> 

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), &cbNeeded))
    {
    unsigned int k;
    for(k = 0; k < (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: &#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), &cbNeeded ) ) return NULL;
    cProcesses = cbNeeded / sizeof(DWORD);

    HMODULE hmodule;
    unsigned int i;
    for (i = 0; i < 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, &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);

        char szBuff[512];
       sprintf(szBuff, "%p", hwnd);

       printf( "Found .... hWnd: %s\n", szBuff); 

    }

    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;
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top