gpt4 book ai didi

c - 如何以编程方式从进程、HWND 获取句柄 ID?

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:43 25 4
gpt4 key购买 nike

我尝试获取打开的应用程序 (windows) 的句柄 ID。

我运行 Window detective 程序(如 spy++)来验证我是否获得了正确的值。

为了测试,我尝试只获取一个红色箭头指向的 Handle 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

从 Spy++ 我需要这个值,红色箭头:

enter image description here

最佳答案

这很容易,但对我来说有点棘手。

我只需要用 %p 打印指针 HWND hwnd

所以我在我的代码中添加了:

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

关于c - 如何以编程方式从进程、HWND 获取句柄 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18729137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com