From: Steve on
Alf P. Steinbach wrote:
> * Steve:
>> Alf P. Steinbach wrote:
>>> * Steve:
>>>> There is an API called GetModuleFileNameEx which can retrieve a
>>>> filename from a process ID which can in turn be retrieved from a HWND.
>>>
>>> No, GetModuleFileNameEx requires a process handle + a module handle
>>> (the latter can be retrieved from a HWND). A process ID is not a
>>> process handle.
>>>
>> No. GetModuleFileNameEx does not need a module handle.
>
> That doesn't make sense to me.
>
> DWORD GetModuleFileNameEx(
> HANDLE hProcess,
> HMODULE hModule,
> LPTSTR lpFilename,
> DWORD nSize
> );
>
> "hModule [in] Handle to the module."
>
>
>> And you retrieve the process handle from the HWND and not the module
>> handle.
>
> That doesn't make sense to me.
>
> Perhaps you mean that /you/ are retrieving the process handle and not
> the module handle?
>
> To retrieve the module handle from a HWND w, call GetWindowLongPtr( w,
> GWLP_HINSTANCE ).
>
> To retrieve a process id from a HWND w, call GetWindowThreadProcessId(
> w, &processId ).
>
> I don't know how to directly retrieve a process handle from a HWND w. If
> you know a simple way, as it seems you indicate, please describe it.
>

My current code:

int GetFileName(HWND hwnd,LPTSTR fn)
{
DWORD procID;
GetWindowThreadProcessId(hwnd,&procID);
HINSTANCE Pr=(HINSTANCE)OpenProcess(
PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,NULL,procID);
int FNlen=GetModuleFileNameEx(Pr,0,fn,MAX_PATH);
CloseHandle(Pr);
return FNlen;
}

>
>>>> GetModuleFileNameEx is for NT based machines, so for Windows
>>>> 95/98/Me, I need to use GetModuleFileName. But this version requires
>>>> a module handle
>>>
>>> No, the difference is that GetModuleFileName doesn't take a process
>>> handle argument. So you can't specify the process.
>>>
>> GetModuleFileNameEx is only available in NT based machines. Therefor,
>> for NON NT based machines I need to use GetModuleFileName.
>>>
>>>> , and it seems to get a module handle using GetModuleHandle which
>>>> requires a filename (!?).
>>>
>>> No, GetModuleFileName doesn't call GetModuleHandle.
>>
>> I didn't say it did.
>
> That doesn't make sense to me.
>
> Quoted above, you wrote "it seems to get a module handle using
> GetModuleHandle".
>

Oh, for Christs sake man! A typing error!
It was meant to say: "It would seem I need to get a module handle ..."

>
>>> And no, GetModuleHandle doesn't require a filename: just pass 0 to
>>> GetModuleHandle to get a module handle for the file used to create
>>> the current (calling) process.
>>>
>> So what about other processes?
>>>
>>>> So, how do I retrieve a filename from a HWND in Windows 98?
>>>
>>> Depends which file name you want.
>>>
>>> Since most of the information you've given is incorrect, there's no
>>> way to know what your current NT-specific code does: the only thing
>>> that's for sure is that it doesn't do what you've implicitly described.
>>>
>>> Which filename do you want?
>>>
>> Have a look at the subject again: Filename from HWND
>>
>> I want to retrieve a filename for a given HWND (regardless of what
>> process the HWND is attached to). I know how to do this using
>> GetModuleFileNameEx but this cannot be used under Windows 95,98,Me.
>>
>> So, is there a way that works under all versions of Windows?
>
> Possibly, but /which/ filename is it you want?
>
> Is it a filename for the executable for the window's process?
>
> Then I suggest follow Olof Lagerkvist's advice in this thread, namely
> using the ToolHelp API. One way could be (1) GetWindowThreadProcessId,
> (2) CreateToolhelp32Snapshot, (3) Module32First. And if that sometimes
> happens to find a module other than the original executable, also
> retrieve the module handle in step 1, using GetWindowLongPtr, and use a
> loop checking for that module handle in step 3.
>
> There may be some easier and/or more efficient way, I don't know.
>

So what are you saying then? You don't know? Then why answer?
From: r_z_aret on
On Fri, 12 May 2006 10:30:44 +0100, Steve <me(a)home.com> wrote:

>There is an API called GetModuleFileNameEx which can retrieve a filename
>from a process ID which can in turn be retrieved from a HWND.
>GetModuleFileNameEx is for NT based machines, so for Windows 95/98/Me, I
>need to use GetModuleFileName. But this version requires a module handle
>, and it seems to get a module handle using GetModuleHandle which
>requires a filename (!?). So, how do I retrieve a filename from a HWND
>in Windows 98?

I just used google (http://groups.google.com/advanced_group_search) to
look up
getmodulefilename hwnd
and got 928 hits. I took a quick look and think a 6 Mar 02
contribution by Remy Lebeau [TeamB] to a thread called "How to get
filename form HWND or PID?" in borland.public.cppbuilder.winapi looks
particularly promising.


-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 478
Boston, MA 02116
www.penfact.com
From: Alf P. Steinbach on
* Steve:
> Alf P. Steinbach wrote:
>>
>> Is it a filename for the executable for the window's process?
>>
>> Then I suggest follow Olof Lagerkvist's advice in this thread, namely
>> using the ToolHelp API. One way could be (1)
>> GetWindowThreadProcessId, (2) CreateToolhelp32Snapshot, (3)
>> Module32First. And if that sometimes happens to find a module other
>> than the original executable, also retrieve the module handle in step
>> 1, using GetWindowLongPtr, and use a loop checking for that module
>> handle in step 3.
>>
>> There may be some easier and/or more efficient way, I don't know.
>>
>
> So what are you saying then? You don't know? Then why answer?

You've been given a step by step cookbok recipe.

You'll have to translate that cookbook recipe into working code yourself.

At least make an attempt.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Lucian Wischik on
Steve <me(a)home.com> wrote:
>There is an API called GetModuleFileNameEx which can retrieve a filename
>from a process ID which can in turn be retrieved from a HWND.

A while ago I wrote this. It's not how I'd do things now that I'm
older and wiser... But you seemed to want compatability with older
systems, so maybe it's useful to look at. NB. in this code I test
OSVERSIONINFO, which is bad. You should instead test whether given
functionality is available on the system, rather than presuming to
know which functionality is present on which version of windows. Also
I use Borland's AnsiString and related classes; it would be better to
use stl's string.


#include <vcl.h>
#pragma hdrstop
#include <tlhelp32.h>
#include <winperf.h>


#include "getmod.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------


// MODULENAMEFROMHANDLE
// eg. AnsiString mod=ModuleNameFromHandle(hwnd);
// Windows NT and 95/98 use different code to get the current module
name.
// For Win95 we use the ToolHelp functions from inside KERNEL32
// For NT there are two possibilities: either using PSAPI.DLL or do it
// yourself by querying the performance-data in the registry.
// But PSAPI.DLL only comes with the Platform SDK and some people
(me!)
// might not have it. I've therefore preferred the second option.
// The Win95 ToolHelp functions return a full path name
(c:\apps\bcb\bcb.exe)
// The NT registry ones return only the bare module name (BCB)
// I've therefore abbreviated win95 ones down as well to the lowest
common
// denominator.
// The Win95 and NT/PSAPI code is from a newsgroup article by Bertrand
Le Guern/
// The NT code comes from KB article Q119163
// I've modified them a bit to make them more concise and C++/VCL like

AnsiString __fastcall ModuleNameFromProcessId95(DWORD pid)
{ HINSTANCE hLib=LoadLibrary("KERNEL32.DLL"); if (hLib==NULL) return
"";
typedef HANDLE (WINAPI *PFN_SNAPSHOT)(DWORD dwFlags, DWORD
th32ProcessID);
typedef BOOL (WINAPI *PFN_PROCESS32WALK)(HANDLE hSnapshot,
LPPROCESSENTRY32 lppe);
typedef BOOL (WINAPI *PFN_MODULE32WALK)(HANDLE hSnapshot,
LPMODULEENTRY32 lpme);
PFN_SNAPSHOT
pfnCreateToolhelp32Snapshot=(PFN_SNAPSHOT)GetProcAddress(hLib,"CreateToolhelp32Snapshot");
PFN_PROCESS32WALK
pfnProcess32First=(PFN_PROCESS32WALK)GetProcAddress(hLib,"Process32First");
PFN_PROCESS32WALK
pfnProcess32Next=(PFN_PROCESS32WALK)GetProcAddress(hLib,"Process32Next");
if (pfnCreateToolhelp32Snapshot==NULL || pfnProcess32First==NULL ||
pfnProcess32Next==NULL) {FreeLibrary(hLib); return "";}
// create a process snapshot and walk through the process list,
adding
// each process to the top listbox
HANDLE hSnapshot =
pfnCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (hSnapshot==NULL) {FreeLibrary(hLib);return "";}
PROCESSENTRY32 pe32; ZeroMemory(&pe32,sizeof(pe32)); pe32.dwSize =
sizeof(pe32);
AnsiString s="";
bool keepgoing=pfnProcess32First(hSnapshot,&pe32);
while (keepgoing)
{ if (pe32.th32ProcessID==pid) {s=pe32.szExeFile;keepgoing=false;}
else keepgoing=pfnProcess32Next(hSnapshot,&pe32);
}
CloseHandle(hSnapshot);
FreeLibrary(hLib);
s=s.UpperCase();
return ChangeFileExt(ExtractFileName(s),"");
}



int __fastcall GetIndex(AnsiString title);
AnsiString __fastcall ModuleNameFromProcessIdNT(DWORD dwProcessId)
{ int ProcessIndex=GetIndex("Process"); if (ProcessIndex==-1) return
"";
int IdIndex=GetIndex( "ID Process"); if (IdIndex==-1) return "";
// Get memory for PPERF_DATA_BLOCK, and get data. We do it in a loop
for some reason
// to get the right size. The initial 12000 is just a guess.
DWORD dwBytes=12000; PPERF_DATA_BLOCK pdb = (PPERF_DATA_BLOCK)
HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBytes);
while
(RegQueryValueEx(HKEY_PERFORMANCE_DATA,(AnsiString(ProcessIndex)).c_str(),NULL,NULL,(LPBYTE)pdb,&dwBytes)
== ERROR_MORE_DATA )
{ dwBytes += 1000;
pdb = (PPERF_DATA_BLOCK)
HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(LPVOID)pdb,dwBytes);
}
PPERF_OBJECT_TYPE pot = (PPERF_OBJECT_TYPE)((PBYTE)pdb +
pdb->HeaderLength);
// Get the first counter definition.
PPERF_COUNTER_DEFINITION pcd =
(PPERF_COUNTER_DEFINITION)((PBYTE)pot + pot->HeaderLength);
DWORD dwProcessIdOffset;
for (int i=0; i< (int)pot->NumCounters; i++ )
{ if (pcd->CounterNameTitleIndex == (DWORD)IdIndex)
{ dwProcessIdOffset = pcd->CounterOffset;
break;
}
pcd = ((PPERF_COUNTER_DEFINITION)((PBYTE)pcd + pcd->ByteLength));
}
// Get the first instance of the object.
PPERF_INSTANCE_DEFINITION pid =
(PPERF_INSTANCE_DEFINITION)((PBYTE)pot + pot->DefinitionLength);
// Get the name of the first process.
PPERF_COUNTER_BLOCK pcb = (PPERF_COUNTER_BLOCK) ((PBYTE)pid +
pid->ByteLength );
DWORD CurrentProcessId = *((DWORD *) ((PBYTE)pcb +
dwProcessIdOffset));
// Find the process object for PID passed in, then print its
filename.
wchar_t *sres=L""; BOOL bContinue = TRUE;
for(int i = 1; i < pot->NumInstances && bContinue; i++ )
{ if( CurrentProcessId == dwProcessId )
{ sres=(wchar_t*) ((PBYTE)pid + pid->NameOffset) ;
bContinue = FALSE;
}
else
{ pid = (PPERF_INSTANCE_DEFINITION) ((PBYTE)pcb +
pcb->ByteLength);
pcb = (PPERF_COUNTER_BLOCK) ((PBYTE)pid + pid->ByteLength);
CurrentProcessId = *((DWORD *)((PBYTE)pcb + dwProcessIdOffset));
}
}
AnsiString s=WideCharToString(sres);
HeapFree(GetProcessHeap(), 0, (LPVOID)pdb) ;
RegCloseKey( HKEY_PERFORMANCE_DATA );
return ChangeFileExt(s.UpperCase(),"");
}
int __fastcall GetIndex(AnsiString title)
{ // We're going to get a buffer from this particular key.
HANDLE hKeyIndex; LONG res=RegOpenKeyEx(
HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows
NT\\CurrentVersion\\Perflib\\009",0, KEY_READ,&hKeyIndex); if
(res!=ERROR_SUCCESS) return -1;
DWORD CountersSize; RegQueryValueEx(hKeyIndex,"Counters",NULL, NULL,
NULL,&CountersSize );
char *CountersBuf=new char[CountersSize];
ZeroMemory(CountersBuf,CountersSize);

RegQueryValueEx(hKeyIndex,"Counters",NULL,NULL,(LPBYTE)CountersBuf,&CountersSize);
// Find the index value for whatever.
int index=-1;
unsigned int i=0;
while (i<CountersSize && index==-1)
{ AnsiString curindex=""; while (CountersBuf[i]!=0)
{curindex=curindex+CountersBuf[i]; i++;}
i++; AnsiString curobj="";
while (CountersBuf[i]!=0) {curobj=curobj+CountersBuf[i]; i++;}
i++; if (CountersBuf[i]==0) i++;
if (curobj==title) index=curindex.ToInt();
}
delete[] CountersBuf;
RegCloseKey(hKeyIndex);
return index;
}



AnsiString __fastcall ModuleNameFromProcessIdNT2(DWORD pid)
{ HINSTANCE hLib=LoadLibrary( "PSAPI.DLL"); if (hLib==NULL) return "";
typedef BOOL (WINAPI *PFN_ENUMPROC)(DWORD * lpidProcess,DWORD
cb,DWORD *cbNeeded);
typedef BOOL (WINAPI *PFN_ENUMMOD)( HANDLE hProcess,HMODULE
*lphModule,DWORD cb,LPDWORD lpcbNeeded);
typedef DWORD (WINAPI *PFN_GETMODNAME)( HANDLE hProcess,HMODULE
hModule,LPSTR lpFilename,DWORD nSize);
PFN_ENUMPROC
pfnEnumProcesses=(PFN_ENUMPROC)GetProcAddress(hLib,"EnumProcesses");
PFN_ENUMMOD
pfnEnumProcessModules=(PFN_ENUMMOD)GetProcAddress(hLib,"EnumProcessModules");
PFN_GETMODNAME
pfnGetModuleBaseNameA=(PFN_GETMODNAME)GetProcAddress(hLib,"GetModuleBaseNameA");
PFN_GETMODNAME
pfnGetModuleFileNameExA=(PFN_GETMODNAME)GetProcAddress(hLib,"GetModuleFileNameExA");
if (pfnEnumProcesses==NULL || pfnEnumProcessModules==NULL ||
pfnGetModuleBaseNameA==NULL || pfnGetModuleFileNameExA==NULL)
{ FreeLibrary(hLib); return "";
}
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ, FALSE, pid);
if (hProcess==NULL) {FreeLibrary(hLib);return "";}
AnsiString s="";
HMODULE hMod; DWORD cbNeeded; bool
isit=pfnEnumProcessModules(hProcess,&hMod,sizeof(hMod),&cbNeeded);
if (isit)
{ char *c=new char[cbNeeded];
pfnGetModuleFileNameExA(hProcess,hMod,c,cbNeeded);
s=AnsiString(c);
delete[] c;
}
CloseHandle( hProcess );
FreeLibrary(hLib);
return s;
}









AnsiString __fastcall ModuleNameFromProcessId(DWORD pid)
{ OSVERSIONINFO osvi; ZeroMemory(&osvi,sizeof(osvi));
osvi.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
if (osvi.dwPlatformId==VER_PLATFORM_WIN32_NT) return
ModuleNameFromProcessIdNT(pid);
else return ModuleNameFromProcessId95(pid);
}

AnsiString __fastcall ModuleNameFromHWnd(HWND hwnd)
{ DWORD pid; GetWindowThreadProcessId(hwnd,&pid);
return ModuleNameFromProcessId(pid);
}


--
Lucian
From: Steve on
Alf P. Steinbach wrote:
> * Steve:
>> Alf P. Steinbach wrote:
>>>
>>> Is it a filename for the executable for the window's process?
>>>
>>> Then I suggest follow Olof Lagerkvist's advice in this thread, namely
>>> using the ToolHelp API. One way could be (1)
>>> GetWindowThreadProcessId, (2) CreateToolhelp32Snapshot, (3)
>>> Module32First. And if that sometimes happens to find a module other
>>> than the original executable, also retrieve the module handle in step
>>> 1, using GetWindowLongPtr, and use a loop checking for that module
>>> handle in step 3.
>>>
>>> There may be some easier and/or more efficient way, I don't know.
>>>
>>
>> So what are you saying then? You don't know? Then why answer?
>
> You've been given a step by step cookbok recipe.
>
> You'll have to translate that cookbook recipe into working code yourself.
>
> At least make an attempt.
>
>
No Alf. You've managed to take a snippet of your post to make me look
stupid, when all you've really done is quote another poster. All you
have done so far is question my questions which is becoming infuriating.

If you don't know or don't have an answer, they don't post.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: Get Firefox URL?
Next: FindFirstUrlCacheEntry (WinInet)