March 31, 2003
Driving win32 GUIs with Python, part 4

A correction...

Thomas Heller got in touch with me, and pointed out that my getMultipleWindowValues() function (see part 3) does A Very Bad Thing. It mutates a Python string. These are supposed to be immutable in Python. This didn't break anything of mine, or hasn't yet, but it certainly could. If, for example, a string being used as a key in a dictionary mutated, who knows what would happen? BANG!

Fixed code (using the array module) below. (Notice that I've also turned this function into a generator.)

def getMultipleWindowValues(hwnd, getCountMessage, getValueMessage):
bufferlength = struct.pack('i', 255)
count = win32gui.SendMessage(hwnd, getCountMessage, 0, 0)
for itemIndex in range(count):
value = array.array('c', bufferlength + str().ljust(253))
valueLength = win32gui.SendMessage(hwnd, getValueMessage, itemIndex, value)
yield value.tostring()[:valueLength]

Update: See the whole series: Driving win32 GUIs with Python, part 1, Driving win32 GUIs with Python, part 2, Driving win32 GUIs with Python, part 3, Driving win32 GUIs with Python, part 4 and 7 hours, one line of code.

Posted to Python by Simon Brunning at March 31, 2003 01:43 PM
Comments
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?