Re: [python-win32] Trying to grab exe of foreground window

2009-07-23 Thread Tim Roberts
reehdus wrote:
>  
>   I see...I tried looking for documentation onw win32con to see what I
> needed but I couldn't muster up anything. All i got were
> win32con.PROCESS_TERMINATE and process query information so I assumed one of
> this was what I needed. Again...thanks so much for enlightening me.
>   

Remember that virtually all of the calls in the "win32" modules are
direct mappings to the Win32 APIs of the same name.  For the most part,
the best source of technical information is the MSDN pages for the
APIs.  You sometimes have to spend a few minutes trying to map the C
parameters into Python code, but once you get the pattern, that's not so
hard.

-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] trying to grab exe of foreground window

2009-07-23 Thread Eric Blade
I've done some pretty extensive work with determining name of
executeables belonging to a window by Window Handle .. and here's what
I've come up with ,that seems to work 100% of the time, even on Vista:

# Request privileges to enable "debug process", so we can
later use PROCESS_VM_READ, retardedly required to
GetModuleFileNameEx()
priv_flags = win32security.TOKEN_ADJUST_PRIVILEGES |
win32security.TOKEN_QUERY
hToken = win32security.OpenProcessToken
(win32api.GetCurrentProcess(), priv_flags)
# enable "debug process"
privilege_id = win32security.LookupPrivilegeValue (None,
win32security.SE_DEBUG_NAME)
old_privs = win32security.AdjustTokenPrivileges (hToken, 0,
[(privilege_id, win32security.SE_PRIVILEGE_ENABLED)])

# Open the process, and query it's filename
processid = win32process.GetWindowThreadProcessId(hwnd)
pshandle =
win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION |
win32con.PROCESS_VM_READ, False, processid[1])
exename = win32process.GetModuleFileNameEx(pshandle, 0)

# clean up
win32api.CloseHandle(pshandle)
win32api.CloseHandle(hToken)

The first block of code got it working in 100% of our users cases on
Vista (whereas previously, we could only get it if they were both
admin, and had the python executeable and the executeable we were
trying to query stored outside of the Program Files hierarchy).  Also,
without the two calls to CloseHandle, we would eventually leak
resources until the system was completely depleted of handles, and
then every other operation would fail, not just by our program, but by
every program, and even the Windows swapper would eventually crash and
bring the system down.

Lesson I've learned: Always clean up after yourself when you're
mucking about with the Windows API, Python won't do it for you.  (I
imagine others haven't mentioned to do this because others probably
don't need to run this routine bajillions of times, therefore don't
see their programs leaking like the proverbial sieves)



On Thu, Jul 23, 2009 at 6:00 AM,  wrote:
> Send python-win32 mailing list submissions to
>        python-wi...@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://mail.python.org/mailman/listinfo/python-win32
> or, via email, send a message with subject or body 'help' to
>        python-win32-requ...@python.org
>
> You can reach the person managing the list at
>        python-win32-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of python-win32 digest..."
>
>
> Today's Topics:
>
>   1. Re: handle is invalid? (reehdus)
>   2. Re: Trying to grab exe of foreground window (reehdus)
>
>
> --
>
> Message: 1
> Date: Wed, 22 Jul 2009 17:26:12 -0700 (PDT)
> From: reehdus 
> To: python-win32@python.org
> Subject: Re: [python-win32] handle is invalid?
> Message-ID: <24617252.p...@talk.nabble.com>
> Content-Type: text/plain; charset=us-ascii
>
>
> ahha...that would explain a lot...nope...roger's method was excellent...I was
> trying this out before I saw his recipe. I see...well...pardon my ignorance
> since I'm pretty new at Python...it didn't really occur to me that
> FindWindow was redundant. Well, now I know better...hahaha. Thanks!
>
>
> Tim Roberts wrote:
>>
>> sudheer sivathasan wrote:
>>> Hey guys,
>>>
>>> Another quick question. I'm still trying to do the thing in the email
>>> below. But this time im trying to use the win32api.GetModuleFileName
>>> function. It requires a PyHandle as an input which I think I've
>>> managed to get by using the function handle =
>>> w.FindWindow(0,str(w.GetWindowText(w.GetForegroundWindow( where
>>> handle would serve as the input to the win32api.GetModuleFileName
>>> function and w = win32gui. However I'm getting a 'the handle is
>>> invalid' error.
>>
>> Yes, because FindWindow returns a window handle, and GetModuleFileName
>> expects a module handle.  Handles are not all alike.  Roger Upole gave
>> you exactly the recipe you need.  Was there something about his reply
>> that you didn't like?
>>
>> By the way, your code there is somewhat silly: the FindWindow call will
>> return exactly the same handle that GetForegroundWindow already gave you.
>>
>> --
>> Tim Roberts, t...@probo.com
>> Providenza & Boekelheide, Inc.
>>
>> ___
>> python-win32 mailing list
>> python-win32@python.org
>> http://mail.python.org/mailman/listinfo/python-win32
>>
>>
>
> --
> View this message in context: 
> http://www.nabble.com/handle-is-invalid--tp24599116p24617252.html
> Sent from the Python - python-win32 mailing list archive at Nabble.com.
>
>
>
> --
>
> Message: 2
> Date: Wed, 22 Jul 2009 17:36:39 -0700 (PDT)
> From: reehdus 
> To: python-win32@python.org
> Subject: Re: [python-win32] Trying to grab exe of foreground window
> Me