Probléme de de gestion de memoir

cs_xtravagon Messages postés 4 Date d'inscription jeudi 4 novembre 2004 Statut Membre Dernière intervention 17 octobre 2009 - 15 oct. 2009 à 02:10
cs_xtravagon Messages postés 4 Date d'inscription jeudi 4 novembre 2004 Statut Membre Dernière intervention 17 octobre 2009 - 17 oct. 2009 à 00:05
Salut tt le monde
je suis débutant en programmation , j'essaye de faire un programme qui récupère le contenu d'une listeview d'un autre programme avec api,
le petit programme fonction mais le problème c'est que a certain tempe, le programme contenons la listview se bloc.
voila mon programme:
je croix que le problème viens du conflit dans la zone mémoire
merci de m'aider

  '******************** Get all Item string by giving the list view handl ****************************

    Public Function GetItemList(ByVal lstviewhwnd As Integer) As Array
        Dim result As Integer
        Dim myItem As LV_ITEMA
        Dim pHandle As Integer
        Dim pStrBufferMemory As Integer
        Dim pMyItemMemory As Integer
        Dim strBuffer() As Byte
        Dim strBuffer2() As Byte
        Dim index As Integer
        Dim tmpString As String = String.Empty
        Dim ProcessID As Integer
        Dim ItemCount As Integer
        Dim i As Short
        Dim ItemsList() As String
        ' Create two different encodings.
        Dim ascii As Encoding = Encoding.ASCII
        Dim [unicode] As Encoding = Encoding.Default

        ItemCount = SendMessage(lstviewhwnd, LVM_GETITEMCOUNT, 0, 0)
        ReDim ItemsList(ItemCount)
        '********************** 
        'init the string buffer 
        '********************** 

        '*********************************************************** 
        'open a handle to the process and allocate the string buffer 
        '*********************************************************** 
        Call GetWindowThreadProcessId(lstviewhwnd, ProcessID)
        pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_WRITE Or PROCESS_VM_READ, False, ProcessID)
        pStrBufferMemory = VirtualAllocEx(pHandle, 0, StringBufferLength, MEM_COMMIT, PAGE_READWRITE)
        '************************************************************************************ 
        'initialize the local LV_ITEM structure 
        'The myItem.iSubItem member is set to the index of the column that is being retrieved 
        '************************************************************************************ 
        myItem.mask = LVIF_TEXT
        myItem.iSubItem = 2
        myItem.pszText = pStrBufferMemory
        myItem.cchTextMax = StringBufferLength
        '********************************************************** 
        'write the structure into the remote process's memory space 
        '********************************************************** 
        pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
        result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)

        '************************************************************* 
        '************************************************************* 
        '************************************************************* 

        For i = 0 To ItemCount - 1
            ReDim strBuffer(StringBufferLength)
            ReDim strBuffer2(StringBufferLength)
            '************************************************************* 
            'send the get the item message and write back the memory space 
            '************************************************************* 
            result = SendMessage(lstviewhwnd, LVM_GETITEMTEXT, i, pMyItemMemory)
            result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), StringBufferLength, 0)
            result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
            '************************************************** 
            'turn the byte array into a string and send it back 
            '**************************************************
            For index = LBound(strBuffer) To UBound(strBuffer)
                If Chr(strBuffer(index)) = vbNullChar Then Exit For
                strBuffer2(index) = strBuffer(index)
            Next
            tmpString = [unicode].GetString(strBuffer2).Trim(Chr(0))
            ItemsList(i) = tmpString
            tmpString = String.Empty
            strBuffer = Nothing
            strBuffer2 = Nothing
        Next

        '************************************************** 
        'deallocate the memory and close the process handle 
        '************************************************** 
        result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
        result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
        result = CloseHandle(pHandle)
        result = Nothing
       
        Return ItemsList
    End Function

3 réponses

Profil bloqué
15 oct. 2009 à 21:47
après chacun des For etc.... mets un Doevents()

for i = 0 to 100000
Doevents()
instruction
etc.....
.....
Next i

La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi.

GRENIER Alain
0
Profil bloqué
15 oct. 2009 à 21:50
Pardon en VB net c'est Application.Doevents()

La théorie, c'est quand on sait tout et que rien ne fonctionne. La pratique, c'est quand tout fonctionne et que personne ne sait pourquoi.

GRENIER Alain
0
cs_xtravagon Messages postés 4 Date d'inscription jeudi 4 novembre 2004 Statut Membre Dernière intervention 17 octobre 2009
17 oct. 2009 à 00:05
Merci merci infiniment Galain, en fin j'ai trouver la solution, le problème était une mauvais déclaration de certain constants,


Const PAGE_READWRITE As Short = &H4S
  Const MEM_RELEASE As Short = &H8000S
 Const MEM_RESERVE As Short = &H2000S


le changement était comme suite


Const PAGE_READWRITE = &H4
    Const MEM_RELEASE = &H8000
    Const MEM_RESERVE = &H2000
0
Rejoignez-nous