ปัญหาเมื่อรับเนื้อหาของกล่องรายการด้วย python และ ctypes บน win32

ฉันต้องการรับเนื้อหาของกล่องรายการด้วย 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 = ctypes.create_string_buffer("", text_len+1)
    ctypes.windll.user32.SendMessageA(hwnd, win32con.LB_GETTEXT, i, buffer)
    items.append(buffer.value)
print items

จำนวนรายการถูกต้องแต่ข้อความไม่ถูกต้อง text_len ทั้งหมดคือ 4 และค่าข้อความจะเป็นเช่น '0\xd9\xee\x02\x90'

ฉันได้ลองใช้บัฟเฟอร์ยูนิโค้ดด้วยผลลัพธ์ที่คล้ายกัน

ฉันไม่พบข้อผิดพลาดของฉัน มีความคิดอะไรบ้าง?


person luc    schedule 23.09.2009    source แหล่งที่มา


คำตอบ (2)


ถ้ากล่องรายการที่เป็นปัญหาถูกวาดโดยเจ้าของ ข้อความนี้จาก LB_GETTEXT อาจเกี่ยวข้อง:

ถ้าคุณสร้างกล่องรายการ มีลักษณะที่เจ้าของวาด แต่ไม่มีลักษณะ LBS_HASSTRINGS บัฟเฟอร์ที่ชี้ไป โดยพารามิเตอร์lParam จะได้รับค่าที่เกี่ยวข้องกับสินค้า (ข้อมูลสินค้า)

ไบต์สี่ไบต์ที่คุณได้รับนั้นดูเหมือนว่าจะเป็นตัวชี้ ซึ่งเป็นค่าทั่วไปที่จะจัดเก็บไว้ในข้อมูลต่อรายการ

person Matthew Xavier    schedule 23.09.2009
comment
ฉันคิดว่าคุณถูก. มันเป็นกล่องรายการที่เจ้าของวาด ฉันไม่รู้ว่าโครงสร้างข้อมูลคืออะไรดังนั้นฉันจึงไม่สามารถรับข้อความได้ :-( - person luc; 23.09.2009

ดูเหมือนว่าคุณจะต้องใช้โครงสร้างแบบแพ็กเพื่อให้ได้ผลลัพธ์ ฉันพบตัวอย่างทางออนไลน์ บางทีนี่อาจช่วยคุณได้:

http://www.brunningonline.net/simon/blog/archives/winGuiAuto.py.html

# Programmer : Simon Brunning - [email protected]
# 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)
person Andre Miller    schedule 23.09.2009