在win32上使用python和ctypes获取列表框内容时出现问题
多亏了python和ctypes,我想获得列表框的内容在win32上使用python和ctypes获取列表框内容时出现问题,python,winapi,ctypes,Python,Winapi,Ctypes,多亏了python和ctypes,我想获得列表框的内容 item_count = ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETCOUNT, 0, 0) items = [] for i in xrange(item_count): text_len = ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETTEXTLEN, i, 0) buffer = cty
item_count = ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETCOUNT, 0, 0)
items = []
for i in xrange(item_count):
text_len = ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETTEXTLEN, i, 0)
buffer = ctypes.create_string_buffer("", text_len+1)
ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETTEXT, i, buffer)
items.append(buffer.value)
print items
项目数量正确,但文本错误。所有文本长度均为4,文本值类似于“0\xd9\xee\x02\x90”
我曾尝试使用unicode缓冲区,结果类似
我没有发现我的错误。有什么想法吗 看起来您需要使用压缩结构来获得结果。我在网上找到了一个例子,也许这会对您有所帮助:
如果所述列表框由业主绘制,则文件中的这一段可能与以下内容相关: 如果创建具有所有者绘制样式但没有LBS_HASSTRINGS样式的列表框,则lParam参数指向的缓冲区将接收与项(项数据)关联的值
您收到的四个字节看起来可能是指针,这是存储在每项数据中的典型值。我认为您是对的。它是一个所有者绘制的列表框。我不知道数据结构是什么,因此无法获取文本:-(
# Programmer : Simon Brunning - simon@brunningonline.net
# Date : 25 June 2003
def _getMultipleWindowValues(hwnd, getCountMessage, getValueMessage):
'''A common pattern in the Win32 API is that in order to retrieve a
series of values, you use one message to get a count of available
items, and another to retrieve them. This internal utility function
performs the common processing for this pattern.
Arguments:
hwnd Window handle for the window for which items should be
retrieved.
getCountMessage Item count message.
getValueMessage Value retrieval message.
Returns: Retrieved items.'''
result = []
VALUE_LENGTH = 256
bufferlength_int = struct.pack('i', VALUE_LENGTH) # This is a C style int.
valuecount = win32gui.SendMessage(hwnd, getCountMessage, 0, 0)
for itemIndex in range(valuecount):
valuebuffer = array.array('c',
bufferlength_int +
" " * (VALUE_LENGTH - len(bufferlength_int)))
valueLength = win32gui.SendMessage(hwnd,
getValueMessage,
itemIndex,
valuebuffer)
result.append(valuebuffer.tostring()[:valueLength])
return result
def getListboxItems(hwnd):
'''Returns the items in a list box control.
Arguments:
hwnd Window handle for the list box.
Returns: List box items.
Usage example: TODO
'''
return _getMultipleWindowValues(hwnd,
getCountMessage=win32con.LB_GETCOUNT,
getValueMessage=win32con.LB_GETTEXT)