Click to See Complete Forum and Search --> : Get Executable Path using GetModuleFileName


flynny1st
July 6th, 2007, 07:05 AM
Hi,

I'm having trouble finding a windows executable string from the HWND.

It seems to work ok if i try and get the string of the process but is i try this on another window (for example on internet explorer window). it returns nonsense.

this is how i'm doing it


char path[500];
HINSTANCE__* foundWindowH = (HINSTANCE) GetWindowLong(hwndFoundWindow,GWL_HINSTANCE);
GetModuleFileName(foundWindowH, path, 500);


with hwndFoundWindow being the HWND__* i'm interested in.
any ideas,

thanks Matt.

golanshahar
July 6th, 2007, 10:35 AM
Try this:


HWND h= /*put handle here*/

DWORD dwPid=0;
::GetWindowThreadProcessId(h,&dwPid);
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS ,0,dwPid);
if ( hProcess == 0 )
return;

typedef DWORD (WINAPI *tGetModuleFileNameExA)( HANDLE, HMODULE,LPTSTR,DWORD);

tGetModuleFileNameExA pGetModuleFileNameExA =0;

HINSTANCE hIns = ::LoadLibrary("Psapi.dll");
if (hIns)
pGetModuleFileNameExA = (tGetModuleFileNameExA)::GetProcAddress(hIns,"GetModuleFileNameExA");


char szFileName[MAX_PATH]={0};
if( pGetModuleFileNameExA)
pGetModuleFileNameExA(hProcess,0,szFileName,sizeof(szFileName));

::FreeLibrary(hIns);


Cheers

flynny1st
July 9th, 2007, 06:05 AM
brilliant, that works!! many thanks for the help.

Matt.

golanshahar
July 9th, 2007, 03:58 PM
You are welcome :wave:

Cheers