Typically in external hacks, I see people using tlhelp32 functions that are difficult for beginners to understand. I just wanted to share the way I do it using psapi functions. The functions I use are well documented on MSDN.
Code:
Code:
- #include <Windows.h>
- #include <psapi.h>
- #include <tchar.h>
- HANDLE getProcessHandleFromHWND(HWND hWindow)
- {
- HANDLE hThread = OpenThread(THREAD_QUERY_LIMITED_INFORMATION, false, GetWindowThreadProcessId(hWindow, NULL));
- if (hThread)
- {
- HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, GetProcessIdOfThread(hThread));
- CloseHandle(hThread);
- return hProcess;
- }
- else
- {
- return NULL;
- }
- }
- HMODULE getModuleHandle(HANDLE hProcess, LPCTSTR lpModuleName)
- {
- HMODULE hMods[1024];
- DWORD cbNeeded;
- if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
- {
- for (int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
- {
- TCHAR szModName[MAX_PATH];
- if (GetModuleBaseName(hProcess, hMods, szModName, sizeof(szModName) / sizeof(TCHAR)) && !_tcscmp(szModName, lpModuleName))
[*] {
[*] return hMods;
[*] }
[*] }
[*] }
[*]
[*] return NULL;
[*]}