2013-09-10 92 views
2

我嘗試獲取打開的應用程序(窗口)的句柄ID。如何通過編程方式從進程HWND獲取句柄ID?

我運行Window detective程序(如spy ++)來驗證我得到了正確的值。

爲了測試我試圖讓只有一個句柄ID由紅色箭頭指向(見圖片):

enter image description here

所以我有計劃,讓我的進程ID和線程ID,但不是第一個孩子把手ID。

在我來說,我把calc.exe,但實際上我需要做的所有exe應用:

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; 
} 

運行從minGW

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

輸出:

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

我怎樣才能從流程的處理?

我確定它應該是一些1-2行的代碼。

DWORD dwValue ..... 

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

應該0x007B137C

從諜照++我需要這個值,紅色箭頭:

enter image description here

+1

句柄不是唯一的值/標識符。如果兩個應用程序有某種處理方式,它們將不具有相同的十六進制值,因此將輸出與您的程序進行比較以及將輸出與第三方應用程序進行比較是沒有意義的。 –

+0

但是'CalcFrame'有'text' - 'Calculator',但是當我使用'Window Detective'搜索時,我只有一個窗口,看到我的編輯 –

+0

「句柄ID」不是Windows術語。你是什​​麼意思? – Medinoc

回答

1

這是很容易,但對我來說麻煩一些。

我只需要打印指針HWND hwnd%p

所以我加入到我的代碼:

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

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

,並得到我所需要的:

Found .... hWnd: 007B137C 

[編輯]

工作的代碼示例:

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; 
} 
+0

請解釋當你downvote –

+3

我必須同意,如何正確使用printf()似乎不是很多答案「我怎樣才能得到處理的句柄?」如果你對這個答案感到滿意,爲什麼你在地球上給這個問題留下了恩賜?你還在試圖解決什麼問題? –

+0

我有屏幕共享應用程序,可以流特定窗口或全屏。屏幕共享應用程序通過註冊表使用唯一的API,我需要把特定窗口的句柄ID(如六進制)。我可以從hwnd獲取進程ID或線程,但花了我一整天的時間來查找如何獲取句柄ID。直到現在沒有人告訴我,所以我發現我可以從指針中獲取處理程序ID。我不是「C」程序員,所以如果你知道正確的方法做到這一點,50點+ V是你的。謝謝 –