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

2009-07-22 Thread Roger Upole

reehdus wrote:
I tried that but I get stuck at EnumProcessModules. It tells me my access 
is

denied. I'm not sure if I'm doing it right. My code is below:

import win32gui
import win32api
import win32con
from time import sleep
import win32process

sleep(2)
name = win32gui.GetForegroundWindow()
t,p = win32process.GetWindowThreadProcessId(name)
handle = win32api.OpenProcess(1,False,p)
sleep(1)
whee = win32process.EnumProcessModules(handle)
nama = win32process.GetModuleFileNameEx(whee, 0)
print nama

Thanks!

Roger Upole wrote:


A sequence like this should get you there.

win32gui.GetForegroundWindow
win32process.GetWindowThreadProcessId
win32api.OpenProcess
win32process.EnumProcessModules
win32process.GetModuleFileNameEx

   hth
 Roger


You'll need to use a different access mask for OpenProcess.

handle = 
win32api.OpenProcess(win32con.PROCESS_VM_READ|win32con.PROCESS_QUERY_INFORMATION,False,p)


Also, GetModuleFileNameEx needs the process handle:

nama = win32process.GetModuleFileNameEx(handle, whee[0])


 Roger

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


[python-win32] Jonathan K Shelley is out of the office.

2009-07-22 Thread Jonathan K Shelley

I will be out of the office starting  07/21/2009 and will not return until
07/27/2009.

I will respond to your message when I return. For  assistance please
contact Blaik Beattie (blaik.beat...@inl.gov)___
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] handle is invalid?

2009-07-22 Thread Tim Roberts
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


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

2009-07-22 Thread Tim Roberts
reehdus wrote:
> I tried that but I get stuck at EnumProcessModules. It tells me my access is
> denied. I'm not sure if I'm doing it right. My code is below:
>
> import win32gui
> import win32api
> import win32con
> from time import sleep
> import win32process
>
> sleep(2)
> name = win32gui.GetForegroundWindow()
> t,p = win32process.GetWindowThreadProcessId(name)
> handle = win32api.OpenProcess(1,False,p)
>   

The 1 there means PROCESS_TERMINATE.  That means you are only asking for
permission to terminate the process, not to enumerate its contents.  For
EnumProcessModules, you need PROCESS_QUERY_INFORMATION and
PROCESS_VM_READ, which would be 0x0410.

> sleep(1)
> whee = win32process.EnumProcessModules(handle)
> nama = win32process.GetModuleFileNameEx(whee, 0)
> print nama
>   

Why are those "sleeps" in there?

EnumProcessModules returns a tuple, containing all of the modules
currently loaded in the process.  You would need to choose one. 
However, in this particular case, you don't need that call at all. 
GetModuleFileNameEx takes a process handle, plus a module handle, or "0"
to mean the processes original exe.  So, you can replace those last four
lines with:
print win32process.GetModuleFileNameEx( handle, 0 )

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

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


[python-win32] (no subject)

2009-07-22 Thread Gary Smith
Hi All,

I built a Python com server to solve regular expressions for use in a  VBA
application.  Here's the code:

 

import pythoncom

import re

import string

class reObj:

_public_methods_ = ["re"]

_reg_progid_ = "Python.reObj"

_reg_clsid_ = pythoncom.CreateGuid()

def __init__ (self):

pass

def re(self, pattern, str):

result=''

matchGroup=re.search(pattern,str)

while matchGroup<>None:

(start,end)=matchGroup.span()

result = result + ", " + str[start:end]

str=str.replace(str[start:end],'',1)

matchGroup=re.search(pattern,str)

return result[2:]

def listem(self, list):

for item in list:

print item, r.re(p,item)

 

if __name__ == '__main__':

import win32com.server.register

win32com.server.register.UseCommandLine(reObj)

 

The last few lines register the com server, so all that is necessary is to
execute the code.

 

Calling from VBA is:

Dim re As Object

Set reObj = CreateObject("Python.reObj")

Result = reObj(pattern, string)

_

I got the basic idea from something I found on the web.

Cheers, Gary

__

"Even for practical purposes theory generally turns out the most important
thing in the end."  Oliver Wendell Holmes.

 

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


Re: [python-win32] handle is invalid?

2009-07-22 Thread reehdus

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.

___
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-22 Thread reehdus

  Excellent...it works! Thanks...I put in the first sleep so I could test
the program with various other applications, so it'd give me enough time to
open up internet explorer or something...the second sleep was a
typo...leftover from a bit of earlier code i did. 
  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.

Sudheer


Tim Roberts wrote:
> 
> reehdus wrote:
>> I tried that but I get stuck at EnumProcessModules. It tells me my access
>> is
>> denied. I'm not sure if I'm doing it right. My code is below:
>>
>> import win32gui
>> import win32api
>> import win32con
>> from time import sleep
>> import win32process
>>
>> sleep(2)
>> name = win32gui.GetForegroundWindow()
>> t,p = win32process.GetWindowThreadProcessId(name)
>> handle = win32api.OpenProcess(1,False,p)
>>   
> 
> The 1 there means PROCESS_TERMINATE.  That means you are only asking for
> permission to terminate the process, not to enumerate its contents.  For
> EnumProcessModules, you need PROCESS_QUERY_INFORMATION and
> PROCESS_VM_READ, which would be 0x0410.
> 
>> sleep(1)
>> whee = win32process.EnumProcessModules(handle)
>> nama = win32process.GetModuleFileNameEx(whee, 0)
>> print nama
>>   
> 
> Why are those "sleeps" in there?
> 
> EnumProcessModules returns a tuple, containing all of the modules
> currently loaded in the process.  You would need to choose one. 
> However, in this particular case, you don't need that call at all. 
> GetModuleFileNameEx takes a process handle, plus a module handle, or "0"
> to mean the processes original exe.  So, you can replace those last four
> lines with:
> print win32process.GetModuleFileNameEx( handle, 0 )
> 
> -- 
> 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/Trying-to-grab-exe-of-foreground-window-tp24582049p24617345.html
Sent from the Python - python-win32 mailing list archive at Nabble.com.

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