2013-09-10 61 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是你的。谢谢 –