[python-win32] GetModuleFileName requires Process or Thread handle?

Tim Roberts timr at probo.com
Mon Aug 18 21:20:12 CEST 2008


Walter Decker wrote:
> Hello,
>  
> Can someone help me diagnose why the following fails to retrieve the 
> 'exe' name KillByName?
> ...
>   for pid in processList:
>     try:
>       print "KillByName pid: " + str(pid)
>       handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, 
> False, pid)
>       print "KillByName handle: " + str(handle)
>       exe = win32api.GetModuleFileName(handle)
> ...
> Is a handle a handle a handle? The win32api.CreateProcess that started 
> the process returns two (2) handles (hProcess and hThread) and the 
> OpenProcess
> call above returns the second of those two handles (the hThread). Is 
> it that I need the 'other' handle as in:
> "The result is a tuple of (hProcess, hThread, dwProcessId, dwThreadId)"
> to send to win32api.GetModuleFileName(handle)?

No, handles are not interchangable.  Window handles, process handles, 
thread handles, file handles, event handles, registry handles, and 
module handles are all completely different spaces.  GetModuleFileName 
requires an HMODULE -- a handle to a module.  In fact, an HMODULE is 
actually just the virtual address where the module is loaded *within 
your own process*.  (The module handle of your main executable is 
0x00400000).  You cannot call GetModuleFileName across processes.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list