[python-win32] WMI troubles!
Tim Roberts
timr at probo.com
Fri Jan 15 19:33:26 CET 2010
Alex Hall wrote:
>
> Anyway, onto the problem. I am working on a simple resource monitor (I
> have never found one that works well with screen readers, so I am
> writing one). I am using python2.6 with all the win32 libs installed.
> My monitor will use wmi to get all of its information, and it was
> going pretty well until a few hours ago, when I started receiving
> seemingly random errors. I can call my functions, such as getFreeRam
> or getLoad, with no problem as long as the call is hard-coded into the
> script. However, when I try to call said functions from a function
> monitoring keyboard input (using pyHooks), I get an error and the
> program crashes (at least it did until I put a try/except in there,
> but hitting the hotkey over and over always gives me an error).
Windows hooks run in a unique environment. When you install a Windows
hook, you are actually injecting a DLL into every process in the
system. (It's not a low overhead operation!) The code in the keyboard
hook runs as part of another processs.
Now, I have not looked into the pyHooks code to see if they are managing
that, by using some kind of inter-process communication. If they are
not, then you are very limited in what you can do in your callback.
"print" wouldn't work, because that process has a different standard
output. You would need to send a signal back to your own process.
Further, COM will not have been initialized in that other thread, so you
wouldn't be able to use a COM object, like "Say.Tools". You can try
calling pythoncom.CoInitialize() in every callback, I suppose.
> I will paste the entire file below. It is not very commented yet, so
> if something does not make sense, please let me know. My questions are:
>
> 1. Why am I getting these errors?
What errors do you get? I don't think you told us that.
> 2. The getLoad function only returns one number (the counter is there
> to double check that the loop really only runs once). I have a dual
> core AMD, so I expected two numbers, a LoadPercentage for each core,
> not just one number. How do I get the load per core, not per physical
> processor?
Well, it only returns one value because you overwrite the value of
"load" every time through the loop. If you want to return multiple
values, you should add each new value to a list, and return the list.
> 3. Regarding pyHook: I press my win-` hotkey on the desktop and get
> something to happen in my script, which is good. However, I also get a
> Windows error sound. Is there a way to grab that keystroke before
> Windows can get it to avoid what seems to be passing the keystroke to
> the active app before my script can get it? I tried returning False in
> my keyUp function but that made no difference.
This shouldn't be an issue. The hotkeys are handled by Explorer, which
sucks up the key after it launches your program. I don't understand who
would trigger the sound.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list