Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

lowlevel

Getting handle to mainwindow from process handle?

This topic is 5601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Like my topic says, I''m wondering how(if there is a way) to get the HWND to the mainwindow of an app if you have it''s process handle. My problem is really this: All I know is the name of the process and in the end I want to send a message to the processes mainwindow: //Psuedo-code processHandle = GetProcessHandle("lowlevel.exe"); mainWindowHandle = GetMainWindowHandle(processHandle); PostMessage(..., mainWindowHandle); Hope anyone can help me.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
i don''t know if there''s an easier way (probably is), but here''s what I do on NT-based OSs:

pid-of-interest = 0

EnumProcesses (gets pids)
- for each enumerated process
OpenProcess (gets process handle)
EnumProcessModules (gets module handles)
- for each enumerated module
GetModuleBaseName (gets base filename)
or
GetModuleFileNameEx (gets fullpath)
- if module name == exe-of-interest
CloseHandle for process
pid-of-interest = current pid
break out of for loops
- end if
- end for
CloseHandle for process
- end for

hwnd-of-interest = 0

- if pid-of-interest > 0
EnumWindows
- for each enumerated window
GetWindowThreadProcessId
- if pid == pid-of-interest
hwnd-of-interest = current hwnd
break out of enumeration
- end if
- end for

- if hwnd-of-interest > 0
do whatever here
- end if
- end if

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ahh, forgot about the html parsing. let''s see if this works:

  
pid-of-interest = 0

EnumProcesses (gets pids)

- for each enumerated process
OpenProcess (gets process handle)
EnumProcessModules (gets module handles)
- for each enumerated module
GetModuleBaseName (gets base filename)
or
GetModuleFileNameEx (gets fullpath)
- if module name == exe-of-interest
CloseHandle for process
pid-of-interest = current pid
break out of for loops
- end if
- end for
CloseHandle for process
- end for

hwnd-of-interest = 0

- if pid-of-interest > 0
EnumWindows (gets hwnds of all top-level windows)
- for each enumerated window
GetWindowThreadProcessId (gets pid)
- if pid == pid-of-interest
hwnd-of-interest = current hwnd
break out of enumeration
- end if
- end for

- if hwnd-of-interest > 0
do whatever here
- end if
- end if

Share this post


Link to post
Share on other sites
Just thought I''d post my code for future referense.

The EnumProcesses, EnumProcessModules are part of the PSAPI which I didn''t have for some reason. I used the "Tool help library" instead. Here is the code.


  
HWND theHwnd;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD processId;

GetWindowThreadProcessId(hwnd, &processId);
if( processId == (DWORD)lParam )
{
// Found the window

// set the current hwnd to some global variable or something.

theHwnd = hwnd;
return FALSE;
}
return TRUE;
}


void findWindowHandle(char *appName)
{
DWORD theProcessId = 0;
BOOL theProcessFound = FALSE;

HANDLE hProcessSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);

BOOL bMoreProcesses = Process32First(hProcessSnapShot, &processEntry);
while( bMoreProcesses && !theProcessFound)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processEntry.th32ProcessID);

MODULEENTRY32 module;
module.dwSize = sizeof(MODULEENTRY32);

HANDLE hModuleSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processEntry.th32ProcessID);
BOOL bMoreModules = Module32First(hModuleSnapShot, &module);
while( bMoreModules )
{
if( strcmp(module.szModule, "WE.exe") == 0 )
{
// Found the right process.

theProcessId = processEntry.th32ProcessID;
theProcessFound = TRUE;
break;
}
bMoreModules = Module32Next(hModuleSnapShot, &module);
}

CloseHandle(hProcess);
bMoreProcesses = Process32Next(hProcessSnapShot, &processEntry);
}

theHwnd = 0;
if( theProcessId > 0 )
{
EnumWindows(EnumWindowsProc, theProcessId);
}

Share this post


Link to post
Share on other sites
quote:
Original post by lowlevel
Just thought I''d post my code for future referense.

The EnumProcesses, EnumProcessModules are part of the PSAPI which I didn''t have for some reason. I used the "Tool help library" instead. Here is the code.



    
HWND theHwnd;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD processId;

GetWindowThreadProcessId(hwnd, &processId);
if( processId == (DWORD)lParam )
{
// Found the window

// set the current hwnd to some global variable or something.

theHwnd = hwnd;
return FALSE;
}
return TRUE;
}


void findWindowHandle(char *appName)
{
DWORD theProcessId = 0;
BOOL theProcessFound = FALSE;

HANDLE hProcessSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);

BOOL bMoreProcesses = Process32First(hProcessSnapShot, &processEntry);
while( bMoreProcesses && !theProcessFound)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processEntry.th32ProcessID);

MODULEENTRY32 module;
module.dwSize = sizeof(MODULEENTRY32);

HANDLE hModuleSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processEntry.th32ProcessID);
BOOL bMoreModules = Module32First(hModuleSnapShot, &module);
while( bMoreModules )
{
if( strcmp(module.szModule, "WE.exe") == 0 )
{
// Found the right process.

theProcessId = processEntry.th32ProcessID;
theProcessFound = TRUE;
break;
}
bMoreModules = Module32Next(hModuleSnapShot, &module);
}

CloseHandle(hProcess);
bMoreProcesses = Process32Next(hProcessSnapShot, &processEntry);
}

theHwnd = 0;
if( theProcessId > 0 )
{
EnumWindows(EnumWindowsProc, theProcessId);
}



edit: Sorry for the lack of comments in the code. It was just exerimental code.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!