By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

trying to find path/directory of currently open window

P: 2
Hi guys! I'm pretty new at python (well...who isn't actually?) and I;ve been trying to do a little something for a project of mine.
Whenever a user plays a computer game (like fallout or whatever), that game becomes the foreground window doesn't it? What I'm trying to do is locate the path of this game and search for a file inside the .exe folder...I know it sounds ambitious...but I really hope you guys can help me on this: Here's a bit of code I've been trying to use but I keep getting the 'Invalid handle error"
__________________________________________________ ________________
import win32gui
import win32api
from time import sleep

sleep(0)
w = win32gui
handle = w.FindWindow(0,str(w.GetWindowText(w.GetForeground Window())))

lala = win32api.GetModuleFileName(handle)
print lala
__________________________________________________ ________________

Any help you guys can offer would be much appreciated!! Thanks!
Jul 22 '09 #1
Share this Question
Share on Google+
1 Reply


YarrOfDoom
Expert 100+
P: 1,247
I believe your code didn't work because win32gui.FindWindow returns a handle to a window, and win32api.GetModuleFileName requires a handle to a module.

I've come quite close to what you wanted by using processes, but got stuck on access permissions. However I think it is possible if one were to look a bit longer for a way around this.

I've used this page as a reference on PyWin32, thought it might be of use to you. Certainly take a look at the win32process module.
Jul 29 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.